Design WhatsApp: Real-Time Chat System Design
Step 1 β The NaΓ―ve System
Start simple: User A POSTs a message, the server stores it in the Database, and User B polls with GET to check for new messages.
Request Flow
User A sends a message
POST /message β API validates β INSERT into Database β returns 200 OK to User A.
POST /message
{ to: "userB", text: "Hey!" }
β INSERT INTO messages (from, to, text, ts)
β 200 OK { messageId: "msg_001" }User B polls for messages
User B's client asks every 3 seconds: "Any new messages?". Most requests return nothing. Average delivery latency: 1.5 seconds.
// Every 3 seconds from User B:
GET /messages?since=last_ts
β SELECT * FROM messages WHERE to="B" AND ts > last
β 200 OK { messages: ["Hey!"] }Why We Need This
At 10,000 users polling every 3 seconds β 200K DB reads/min from idle connections. At 1M users the Database collapses.
The latency is also terrible: average 1.5s delivery. WhatsApp delivers in 200ms.
Key Insight
Polling works in demos. It fails at scale because idle traffic (99% empty responses) overwhelms infrastructure and average latency is 1β3s. The fix: flip who initiates communication.
Step 1 of 7
