πŸ’¬
βœ‰οΈ
πŸ“Έ
▢️

Loading…

Design WhatsApp: Real-Time Chat System Design

WhatsAppGuided Mode

Step 5 β€” Track Presence with Cache

The Message Service needs to know: is User B online right now? Querying the Database on every delivery attempt adds 10ms and hammers the DB. A Cache (Redis) answers in 0.1ms, and a dedicated delivery worker handles the actual fan-out from the queue.

Request Flow

1

WebSocket writes presence to Cache on connect

When User B connects, the server writes to the Cache: 'userB is online on server-2' with a 60s TTL. Heartbeats refresh the TTL.

// On WS connect:
redis.set("presence:userB", "server-2", EX=60)
// On heartbeat:
redis.expire("presence:userB", 60)
// On WS disconnect:
redis.del("presence:userB")
2

Message Service checks Cache, Delivery Worker pushes

Before pushing, the Message Service checks the Cache. If online, it hands the job to a delivery worker for immediate push via WS. If offline, the message stays in Queue for reconnect.

server = redis.get("presence:userB")
if server:
  route_to_ws_server(server, msg)  # 0.1ms
else:
  # offline: stays in queue until reconnect

Why We Need This

At 1B users you cannot query the Database for presence on every delivery. Redis handles 1M presence lookups/second from a single instance, while delivery workers drain the queue independently of the message service.

Key Insight

Presence is ephemeral state (right now), not truth state (history). Redis is ideal: in-memory, TTL-based, sub-millisecond. The DB stores messages, Redis stores who is currently alive, and delivery workers turn queued messages into actual pushes.

Step 5 of 7