WhatsAppDifficulty: Advanced
How WhatsApp Works: Architecture & Database Design
WhatsApp handles over 100 billion messages per day. To do this, it requires a highly scalable architecture focused on low latency and high availability. This guide breaks down the core concepts behind WhatsApp's real-time messaging system.
High-Level Architecture
[User A] -> (WebSocket) -> [Chat Server] -> (WebSocket) -> [User B]
|
[Message Queue]
|
[Cassandra DB]Database Design
WhatsApp uses NoSQL databases (historically Erlang's Mnesia, now heavily relying on Cassandra) to store messages temporarily until they are delivered. Once delivered, messages are deleted from the server to ensure privacy and save storage.
Caching Strategy
Redis is used extensively to cache user session data (e.g., which chat server User B is currently connected to) to ensure messages can be routed instantly without hitting the main database.
Scaling & APIs
Scaling: WhatsApp uses Erlang and the actor model. Each user connection is managed by a lightweight Erlang process. A single server can handle millions of concurrent WebSocket connections.
API Design: Unlike traditional REST APIs, real-time messaging heavily relies on persistent WebSocket connections for two-way communication.
Real-World Challenges
- •Handling message ordering when users have bad network connections.
- •Ensuring end-to-end encryption without the server knowing the private keys.
- •Syncing read receipts (blue ticks) efficiently across millions of active chats.