Capstone: Design a Chat Application
Overview
A chat app needs instant delivery, no lost messages and correct ordering for hundreds of millions of users. It combines persistent connections, queues, partitioning and heartbeats from earlier chapters in one design.
Detailed Notes
What is the task
Design a WhatsApp-like service: one-to-one and group chat, delivery receipts, online status, media sharing and message history.
The problem it solves
Users expect messages to arrive in a blink, in order, exactly once from their point of view, even when phones go offline. The system must hold millions of open connections and survive server failures without losing anything.
How it works: follow these steps
- Requirements. Low latency, no lost messages, ordering within a conversation, high availability.
- Estimate. 500 million daily users sending 40 messages each is 20 billion messages a day, about 230,000 a second on average. At 100 bytes of text, about 2 TB a day of text. Media is far larger and goes to object storage behind a CDN.
- Connections. Clients keep a WebSocket open to a connection gateway. A registry (a fast key-value store) records which gateway holds each user's connection. Gateways scale horizontally, and users can be placed using consistent hashing.
- Sending. The chat service saves the message first, then routes it to the recipient's gateway. If the recipient is offline, it stays stored and a push notification is sent.
- Ordering. Give messages per-conversation sequence numbers and partition the message store by conversation id, so one conversation is handled in order.
- Delivery guarantee. Use at-least-once delivery with client acknowledgements. Message ids make processing idempotent, so duplicates are ignored.
- Presence. Clients send heartbeats; a missed heartbeat after a short timeout marks the user offline.
- Security. TLS in transit and end-to-end encryption so servers only see ciphertext.
A simple example
Asha sends "Hi" to Ben. Her phone sends it over her WebSocket to gateway A. The chat service stores it with sequence number 42 in their conversation, looks up Ben's gateway B in the registry, and forwards it. Ben's phone acknowledges; the service marks it delivered. If Ben were offline, the message waits in the store and a push notification goes out.
System design example
Client <== WebSocket ==> Gateways -> Chat service -> Queue -> Delivery workers
Chat service -> Message store (partitioned by conversation id)
Presence service (heartbeats) ; Push notifications ; Media -> object storage + CDN
Bottlenecks: open connections per gateway (add gateways), very large groups (fan out through queue workers, as in the news feed lesson) and a backlog when many users reconnect. Trade-offs: end-to-end encryption blocks server-side search; ordering is guaranteed per conversation only, because global ordering is too costly.
Summary
- Persistent WebSocket connections give instant delivery.
- Save the message first, then deliver; acknowledge and deduplicate.
- Partition by conversation id for ordering and scale.
- Presence uses heartbeats; offline users get push notifications.
Architecture Diagram
Messages are stored first, then delivered over open connections.
- 1The sender's message reaches a gateway over a WebSocket.
- 2The chat service saves it, with a sequence number per conversation.
- 3The registry finds the recipient's gateway and forwards the message.
- 4Offline recipients get a push notification and fetch it later.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

