DEV Community

Cover image for How WhatsApp Handles 1:1 Messaging at Scale (System Design Explained)
NOEMA
NOEMA

Posted on

How WhatsApp Handles 1:1 Messaging at Scale (System Design Explained)

At first glance, sending a message on WhatsApp feels instant and simple.

But behind that single action lies a highly optimized distributed system designed for low latency, high reliability, and massive scale.

In this article, we break down how WhatsApp handles 1:1 messaging step by step.


1. High Concurrency with Erlang

WhatsApp uses Erlang as its backend language.

Erlang is designed for:

  • Handling millions of concurrent processes
  • Fault tolerance
  • Distributed systems

This makes it ideal for real-time messaging at scale.


2. Persistent Connections via Gateway Servers

Each user maintains a long-lived connection with WhatsApp servers using TCP.

These are called gateway or socket servers.

Why this matters:

  • No need to reconnect for every message
  • Lower latency
  • Instant delivery

3. Session Service (User-to-Server Mapping)

A session service tracks which user is connected to which server.

So when User A sends a message to User B:

  • The system routes it directly to the correct server
  • No unnecessary broadcasting

This improves efficiency and scalability.


4. Message Flow (End-to-End Path)

User A → Gateway → Chat Server → Session Service → Receiver Server → Gateway → User B

Each component ensures fast and reliable delivery.


5. Messaging Protocol

WhatsApp uses a modified version of XMPP with persistent communication.

This enables:

  • Lightweight communication
  • Low overhead
  • Real-time delivery

6. Message Queue

Messages are placed into a queue before delivery.

Benefits:

  • Handles traffic spikes
  • Prevents message loss
  • Decouples sender and receiver

7. Delivery Guarantees

WhatsApp uses acknowledgements:

  • Sent → server received
  • Delivered → reached recipient device
  • Read → opened by user

This ensures reliability and transparency.


8. End-to-End Encryption

Messages are encrypted on the sender’s device and decrypted only on the receiver’s device.

This ensures privacy and security.


9. Offline Handling

If the receiver is offline:

  • Message is stored temporarily
  • Push notification is triggered
  • Delivered when user reconnects

10. Local Storage

Messages are stored locally using SQLite.

Advantages:

  • Fast access
  • Offline availability
  • Smooth user experience

11. Scalability

Most backend services are stateless.

This allows:

  • Horizontal scaling
  • Load distribution
  • Handling billions of messages

Conclusion

What seems simple is actually a system involving gateways, queues, session mapping, and distributed services.

Understanding this is key to mastering system design.


If you found this helpful, follow for more real-world system design breakdowns.

Top comments (0)