Real-Time Recommendation Systems with Streaming SQL

Real-Time Recommendation Systems with Streaming SQL

Model Context Protocol (MCP) and Streaming Databases

Real-time recommendation systems suggest products, content, or actions based on user behavior happening right now — not yesterday's batch data. A streaming database computes recommendation signals as SQL materialized views: collaborative filtering, content-based features, and popularity metrics, all updating with every user interaction.

Recommendation Signals with SQL

-- Item popularity (real-time trending)
CREATE MATERIALIZED VIEW trending_items AS
SELECT product_id, COUNT(*) as views_1h,
  COUNT(*) FILTER (WHERE action='purchase') as purchases_1h,
  COUNT(*) FILTER (WHERE action='purchase')::DECIMAL / NULLIF(COUNT(*), 0) as conversion_rate
FROM product_events WHERE ts > NOW()-INTERVAL '1 hour'
GROUP BY product_id ORDER BY views_1h DESC;

-- User-item affinity (collaborative signal)
CREATE MATERIALIZED VIEW user_item_affinity AS
SELECT a.user_id, b.product_id,
  COUNT(*) as co_interaction_count
FROM product_events a
JOIN product_events b ON a.user_id != b.user_id
  AND a.product_id = b.product_id
  AND ABS(EXTRACT(EPOCH FROM a.ts - b.ts)) < 3600
GROUP BY a.user_id, b.product_id;

Batch vs Real-Time Recommendations

AspectBatch (daily)Real-time (streaming)
Session awareness❌ Uses yesterday's profile✅ Current session behavior
Trending itemsHours-oldMinutes-fresh
New user cold startWait for next batchRecommendations after 3-5 clicks

Frequently Asked Questions

Can SQL replace a recommendation engine like Algolia?

For real-time signals (trending, recently viewed, co-viewed), SQL materialized views are sufficient. For advanced ML-based recommendations (deep learning, transformer models), use a dedicated recommendation service powered by streaming SQL features.

Best-in-Class Event Streaming
for Agents, Apps, and Analytics
GitHubXLinkedInSlackYouTube
Sign up for our to stay updated.