Detailed Design of WhatsApp

Lesson 158 of 25610 min readPremium lesson preview
In this lesson2 sections

Detailed Design of WhatsApp

Trace one message from a sender’s device to durable storage and then to its recipients. WebSockets, Redis, Mnesia, MySQL, and Kafka are candidate components in this interview design. Their names do not by themselves establish ordering, durability, or end-to-end encryption.

Detailed design

The high-level design leaves several key questions unanswered:

  • How is a communication channel created between clients and servers?

  • How can the design scale to support billions of users?

  • How is user data stored?

  • How is the receiver identified for message delivery?

Let’s explore each component in detail, starting with how users connect to chat servers.

Connection with a WebSocket server

Each active device establishes a WebSocket connection to the messaging infrastructure. The WebSocket servers maintain persistent connections with online users. To support billions of users, connections are distributed across a cluster of WebSocket servers. Each server maintains a session for every active connection. A WebSocket manager maintains a versioned mapping from user and device IDs to active sessions and gateways, with Redis as a possible directory store. One user can have several device sessions. Heartbeats and session expiry detect abandoned connections; a reconnect creates a new generation so stale routing can be recognized.

The rest of this lesson is for Premium members

Premium opens every lesson in this course, along with all questions and solutions on PracHub.