WhatsApp System Design (High-Level)

 WhatsApp System Design (High-Level)

1️⃣ What Problem Does WhatsApp Solve?

WhatsApp enables:

  • Real-time messaging

  • 1:1 and group chats

  • Low latency delivery

  • Offline message sync

  • Strong privacy (End-to-End Encryption)

The system must work reliably for billions of users, across:

  • Mobile networks

  • Intermittent connectivity

  • Different devices and regions


2️⃣ Core Requirements

Functional

  • Send and receive messages

  • Support 1:1 and group chats

  • Message ordering

  • Delivery & read receipts

  • Presence (online/offline) and typing indicators

  • Media sharing (images, videos, documents)

Non-Functional

  • Very low latency

  • High availability

  • Massive scalability

  • Message durability

  • Privacy and security


3️⃣ High-Level Architecture

Mobile Client | v WebSocket Gateway | v Message Service | +--> Message Store (durable) | +--> Fan-out to Online Users | +--> Push Notifications (offline users)

Supporting systems:

  • Redis → presence & typing

  • Object Storage (S3/GCS) → media

  • APNs / FCM → mobile push notifications


4️⃣ Message Flow (Send → Receive)

Step 1: Send

  • User sends a message

  • App sends message to server via WebSocket

  • Server stores message first (durability)

Step 2: Deliver

  • If recipient is online → push instantly

  • If recipient is offline → send push notification

Step 3: Sync

  • When offline user opens app:

    • Client fetches missing messages

    • Uses last seen message id or sequence


5️⃣ Message Storage & Ordering

How messages are stored

  • Messages are stored once per chat (conversation)

  • Each message gets a sequence number

  • Sequence numbers guarantee ordered delivery

Conversation A: seq 1 → "Hi" seq 2 → "Hello" seq 3 → "How are you?"

Why not global ordering?

  • Global ordering does not scale

  • Ordering is required only inside a chat


6️⃣ Group Chats (Important)

How group messages work

  • Message stored once in group chat

  • Server sends to:

    • All online members immediately

    • Offline members via pull on reconnect

Messages are not synced continuously to all phones.

This avoids:

  • Battery drain

  • Network waste

  • Write amplification


7️⃣ Fan-out Explained (Simply)

Fan-out = deliver one message to many users

In WhatsApp:

  • Small groups → push to all online members

  • Large groups → push online, pull for offline

WhatsApp uses a hybrid fan-out model.


8️⃣ Offline Messaging

  • Messages always stored on server

  • Offline users receive:

    • Push notification (wake-up)

  • On reconnect:

    • Client fetches messages after last seen id

GET /history?after=last_message_id

9️⃣ Presence & Typing Indicators

These are ephemeral states:

  • Stored in Redis

  • Have short TTL (seconds)

Examples:

  • Online / Offline

  • “Typing…”

They are not persisted in message storage.


🔟 Media (Images, Videos, Files)

Media is handled separately from messages:

  1. Client uploads media to object storage using a pre-signed URL

  2. Message contains only media reference

  3. Media is downloaded directly from storage/CDN

This keeps messaging fast and scalable.


1️⃣1️⃣ End-to-End Encryption (E2E)

WhatsApp uses E2E encryption:

  • Messages encrypted on sender’s device

  • Decrypted only on recipient’s device

  • Server stores encrypted content only

Trade-offs:

  • Server cannot read messages

  • Searching messages is harder

  • Abuse detection is limited


1️⃣2️⃣ Reliability & Safety

  • Messages are acknowledged after persistence

  • Retries handled by client using message IDs

  • Duplicate messages avoided using idempotency

  • Abuse handled via metadata & rate limiting


1️⃣3️⃣ Why This Design Scales

  • WebSockets for real-time delivery

  • Store once, deliver many

  • Pull-based offline sync

  • Separation of:

    • messages

    • presence

    • media

  • Eventual consistency is acceptable


1️⃣4️⃣ Interview Talking Points (Key)

  • Store message before delivery

  • Per-chat ordering, not global

  • Push for online, pull for offline

  • Fan-out trade-offs in group chats

  • Redis for ephemeral state

  • Object storage for media

  • E2E encryption trade-offs


1️⃣5️⃣ One-Line Summary (Interview Gold)

WhatsApp stores each message once, pushes to online users in real time, and lets offline users fetch messages when they reconnect, all while preserving per-chat ordering and strong privacy.

No comments:

Post a Comment

Online Food Delivery Platform — System Design

  Online Food Delivery Platform — System Design  1) Use Case & Problem Context Users should be able to: Browse restaurants near them...

Featured Posts