Case Study: Real-Time Chat (WhatsApp / Slack)
Persistent WebSocket gateways, Redis Pub/Sub routing, Cassandra message history, and offline push.
In plain words
Every online phone keeps one live WebSocket connection open to a chat gateway. When Alice texts Bob, the gateway saves the message in an append-fast DB, checks Redis for Bob’s gateway, and pushes it live - or wakes Bob’s phone via Apple/Google push if offline.
Think of it like…
Live switchboard connecting open phone lines, backed by an answering machine (DB + push notification) when someone’s phone is off.
Theory & how it works
Group Chat Fan-out & Delivery Ticks
For small groups (≤500 members), fan out message notifications to each online member’s gateway via Redis Pub/Sub. Store messages with monotonic Snowflake IDs per chat_id so ordering is deterministic, and update per-user read watermarks for Sent ✓ / Delivered ✓✓ / Read ✓✓.
Key points & practical guide
Summary checklist
- Use WebSockets + Redis Pub/Sub for cross-server live routing
- Partition messages by
chat_idordered by Snowflakemsg_id(LSM store) - Use APNs / FCM mobile push when recipient has no active socket
When to use it
- 1:1 messaging, group chat, live support widgets
Watch out for
- Storing user presence "online/offline" directly in SQL - use Redis keys with 30s heartbeat TTLs