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

Loading…

Design WhatsApp: Real-Time Chat System Design

WhatsAppGuided Mode

Step 6 β€” Store and Retry for Offline Users

User B is offline. Messages must be safely stored and reliably flushed when they reconnect. This is the 'store and forward' pattern β€” what gives WhatsApp its βœ“βœ“ delivery receipts.

Request Flow

1

Cache miss = offline, park in Queue

The Message Service does a Cache lookup. Cache miss = offline. Message goes into the Queue with 'pending' status.

server = redis.get("presence:userB")  # β†’ nil (offline)
# Park message:
queue.push({ msgId, to: "userB", status: "pending" })
2

User B reconnects β€” flush pending

When User B's app opens, the WebSocket server sets presence in Cache, then pulls all pending messages from the Queue and sends them in order.

// On WS reconnect from User B:
redis.set("presence:userB", "server-3")
pending = db.query('SELECT * FROM msgs WHERE to=B AND status=pending ORDER BY ts')
for msg in pending:
  ws.send(msg); db.mark_delivered(msg)

Why We Need This

This is why WhatsApp shows the clock icon (sent, not delivered). The message is safely persisted β€” it just hasn't been pushed yet. The double-tick appears only when User B receives it.

Key Insight

Store-and-forward is the offline contract: any message persisted before the sender's ACK MUST eventually be delivered. The Queue makes this promise maintainable under failures.

Step 6 of 7