Case Studies/Messaging

WhatsApp

End-to-end encrypted messaging at planetary scale

messagingreal-timeencryptionerlangXMPP

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

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

Loading diagram...
WhatsApp — High-level system 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

Loading diagram...
WhatsApp — Entity relationship diagram

Message Delivery Flow

When User A sends a message to User B, the following sequence occurs:

  1. Client encrypts the message using the Signal Protocol (Double Ratchet Algorithm)
  2. WebSocket connection transmits the encrypted payload to the gateway
  3. Message Service persists the message and checks recipient status
  4. If online: message is pushed through B’s WebSocket connection immediately
  5. If offline: message is queued and a push notification is sent via APNs/FCM
  6. Delivery receipt flows back to sender when B receives the message
  7. 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.

Loading diagram...
Message Delivery Flow

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.

Explore More Case Studies