Case study boundary
Use this as a learning model.
This page explains architectural patterns and constraints inspired by WhatsApp. It should not be read as the exact private production architecture or as a one-size-fits-all recommendation.
Evidence
Public info + inference
Learn first
Concepts behind this architecture
WebSocket
Keep a bidirectional connection open for low-latency client and server messages.
TLS
Encrypt and authenticate network connections between clients and services.
Load Balancer
Distribute traffic across healthy servers without making clients know where work runs.
Message Queue
Buffer work between producers and consumers so systems can absorb spikes and retry safely.
Overview
WhatsApp is the world’s most popular messaging application, serving over 2 billion users across 180+ countries. Its architecture prioritizes message delivery reliability, end-to-end encryption, and minimal latency. Built on an Erlang/FreeBSD stack, WhatsApp achieves remarkable efficiency — famously supporting 900 million users with just 50 engineers.
Scale
2B+
Monthly active users
100B+
Messages per day
~1.15M messages/second
Peak throughput
Petabytes
Media storage
Requirements
Functional
- One-on-one and group messaging
- Media sharing (images, video, documents)
- Read receipts and online presence
- End-to-end encryption for all messages
- Message synchronization across devices
- Voice and video calling
Non-Functional
- Ultra-low latency (<200ms message delivery)
- High availability (99.99% uptime)
- Strong consistency for message ordering
- Data privacy and encryption
- Minimal bandwidth usage
- Offline message queuing and delivery
High-Level Architecture
Key Components
WebSocket Gateway
Maintains persistent connections with clients using a custom XMPP-based protocol. Each server handles ~1M concurrent connections using Erlang lightweight processes.
Message Service
Core service handling message routing, storage, and delivery confirmation. Uses a store-and-forward pattern for offline users.
Presence Service
Tracks user online/offline status and last-seen timestamps. Uses an in-memory cache for sub-millisecond lookups.
Group Service
Manages group metadata, membership, and fan-out of messages to group participants. Groups support up to 1024 members.
Media Service
Handles upload, encryption, compression, and CDN distribution of media files. Media is encrypted client-side before upload.
Notification Service
Delivers push notifications via APNs/FCM when recipients are offline or the app is backgrounded.
Data Model
Message Delivery Flow
When User A sends a message to User B, the following sequence occurs:
- Client encrypts the message using the Signal Protocol (Double Ratchet Algorithm)
- WebSocket connection transmits the encrypted payload to the gateway
- Message Service persists the message and checks recipient status
- If online: message is pushed through B’s WebSocket connection immediately
- If offline: message is queued and a push notification is sent via APNs/FCM
- Delivery receipt flows back to sender when B receives the message
- Read receipt flows back when B opens the conversation
Messages are stored on the server only until delivered (transient storage). Once the recipient acknowledges receipt, messages are deleted from server storage, ensuring privacy.
End-to-End Encryption
WhatsApp uses the Signal Protocol for end-to-end encryption, ensuring that only the communicating users can read messages. The server never has access to plaintext content.
Key Exchange: Uses the X3DH (Extended Triple Diffie-Hellman) key agreement protocol. Each user generates an identity key pair, a signed pre-key, and a set of one-time pre-keys.
Message Encryption: The Double Ratchet Algorithm provides forward secrecy and break-in recovery. Each message uses a unique encryption key, so compromising one key doesn’t expose other messages.
Group Encryption: Uses the Sender Keys protocol. Each group member generates a sender key, distributes it via pairwise encrypted channels, then uses it for efficient group message encryption.
Erlang/OTP Architecture
WhatsApp’s backend is built on Erlang/OTP, chosen for its exceptional concurrency model and fault tolerance.
Why Erlang?
- Lightweight processes: Each connection is an Erlang process (~2KB memory), enabling millions of concurrent connections per server
- Fault isolation: Process crashes don’t affect other connections (let-it-crash philosophy)
- Hot code loading: Deployments happen without disconnecting users
- Built-in distribution: Erlang nodes communicate seamlessly in a cluster
Mnesia Database: WhatsApp uses Mnesia, Erlang’s built-in distributed database, for storing message queues and user metadata. It provides in-memory speed with optional disk persistence.
A single WhatsApp server can handle ~2 million concurrent connections, making it one of the most efficient messaging architectures ever built.
Architectural Tradeoffs
Erlang/OTP over Java/C++
Pros
- +Exceptional concurrency with lightweight processes
- +Built-in fault tolerance
- +Hot code swapping for zero-downtime deploys
Cons
- −Smaller talent pool
- −Limited ecosystem compared to JVM
- −Performance ceiling for CPU-intensive tasks
Transient message storage
Pros
- +Enhanced privacy — messages deleted after delivery
- +Reduced storage costs
- +Compliance with privacy regulations
Cons
- −No server-side message history
- −Multi-device sync is more complex
- −Backup responsibility shifts to client
Custom XMPP-based protocol
Pros
- +Optimized for mobile bandwidth
- +Fine-grained control over message semantics
- +Efficient binary encoding
Cons
- −Not standards-compliant
- −Higher development cost
- −Client library maintenance burden
Learn From WhatsApp
Copy a learning context prompt for an AI assistant. It keeps assumptions visible and asks for tradeoffs, cost drivers, failure modes, and what to measure before adding complexity.