Design WhatsApp: Real-Time Chat System Design
Step 4 β Add a Queue for Async Delivery
What if the Message Service tries to deliver but User B's WebSocket is temporarily broken? Without a Queue, the message is lost. A dedicated delivery worker drains the queue and handles retries so delivery is no longer tied to the request path.
Request Flow
Enqueue first, deliver second
Instead of delivering directly, the Message Service writes to a Queue first. The message survives even if delivery fails.
// Producer:
queue.push({ msgId, to: "userB", payload, attempts: 0 })
// User A gets ACK immediately β delivery is asyncDelivery Worker pushes with retries
The delivery worker reads from the Queue, attempts WebSocket delivery, ACKs on success, and re-queues failures with exponential backoff. This worker isolates retry logic from the Message Service so spikes do not block incoming messages.
msg = queue.pop() try: deliver(msg) // push via WS queue.ack(msg) except: queue.nack(msg, backoff=2^msg.attempts)
Dead Letter Queue for permanent failures
After N failed delivery attempts, the message moves to a Dead Letter Queue. Engineers replay DLQ messages manually after investigating.
if msg.attempts >= 3:
dlq.push(msg) // Dead Letter Queue
alert.fire("delivery_failure", msg)Why We Need This
The Queue decouples 'message received' from 'message delivered'. Sender gets ACK in <10ms. The delivery worker then owns delivery, survives crashes, and retries automatically without holding up the Message Service.
Key Insight
Queue's contract: a message written to the queue will eventually be delivered, even if the delivery worker crashes and restarts. The worker is the execution engine; the queue is the durability buffer. Together they make async messaging reliable.
Step 4 of 7
