Lab 20: Capstone — Multi-Model App

Time: 60 minutes | Level: Practitioner | DB: PostgreSQL 15 + MongoDB 7 + Redis 7

Build a production-grade social media analytics platform using three databases in concert: PostgreSQL for structured relational data, MongoDB for flexible event logs, and Redis for real-time caching and counters.


Architecture Overview

┌─────────────────────────────────────────────────────────────────┐
│                    Social Media Analytics Platform               │
├─────────────────┬──────────────────┬──────────────────────────  │
│  PostgreSQL 15  │   MongoDB 7       │   Redis 7                  │
│  ─────────────  │  ────────────    │  ──────────────            │
│  • Users        │  • Raw events    │  • Session cache           │
│  • Posts        │  • Event logs    │  • Trending posts (ZSet)   │
│  • Analytics    │  • User activity │  • Real-time counters      │
│  • Aggregates   │  • Device info   │  • Rate limiting           │
│                 │                  │  • Pub/Sub notifications    │
└─────────────────┴──────────────────┴──────────────────────────  ┘

Data Flow:
User Action → Redis counter INCR
           → MongoDB event INSERT
           → PostgreSQL analytics UPDATE (batch)
           → Redis trending ZINCRBY

Step 1 — PostgreSQL: Relational Schema


Step 2 — PostgreSQL: Seed Data

📸 Verified Output:


Step 3 — PostgreSQL: Analytics Queries


Step 4 — MongoDB: Raw Event Log


Step 5 — MongoDB: Event Analytics

📸 Verified Output:


Step 6 — Redis: Session Cache and Real-Time Counters

📸 Verified ZRANGE REV:

📸 Verified MGET counters:


Step 7 — Data Flow: Connecting All Three Systems


Step 8 — Capstone Queries: Cross-Database Analytics Report


Architecture Summary

Data Type
Storage
Why

User profiles, posts, follows

PostgreSQL

Relational integrity, complex analytics queries

Aggregated analytics

PostgreSQL

Window functions, JOIN with user data

Raw event logs

MongoDB

Flexible schema (events have different fields per type)

Activity by device/country

MongoDB

Aggregation pipeline for flexible grouping

User sessions

Redis

Sub-millisecond lookup, auto-expiry

Trending posts

Redis Sorted Set

O(log N) update + O(log N + M) range query

Real-time counters

Redis

Atomic INCR, no lock contention

Rate limiting

Redis

INCR + EXPIRE for sliding window

Notifications

Redis Pub/Sub

Fan-out to connected websocket servers

What You Built

  • PostgreSQL: Schema with indexes, window function analytics, referential integrity

  • MongoDB: Flexible event log, aggregation pipeline for behavioral analytics

  • Redis: Session cache (Hash), trending (Sorted Set), counters (String INCR), rate limiting

  • Data flow: Real-time counters in Redis → async event log in MongoDB → batch aggregate update in PostgreSQL

Congratulations — you've completed the Database Practitioner series! 🎉

Last updated