System Design CourseSystem Design Course1

Design WhatsApp - System Design for Messaging Applications



Introduction

In this module, we will learn how to design WhatsApp — a real-time, scalable, fault-tolerant messaging platform. WhatsApp is used by billions to send and receive instant messages, images, and voice notes. Designing such a system requires careful attention to latency, consistency, and scalability.

Functional Requirements

Non-Functional Requirements

Step-by-Step Architecture

Client-Server Communication

WhatsApp uses a mobile-first approach where the client (mobile app) maintains a persistent connection with the backend using a protocol like XMPP or custom WebSockets over TCP. This enables instant message delivery.

Example:

When User A sends a message to User B:

  1. User A writes a message and hits "send".
  2. The message is encrypted and sent to the WhatsApp server.
  3. The server routes it to User B (if online), or stores it in a message queue (if offline).

Question:

How can we ensure User B receives the message even if their phone is off?

Answer:

We use a Message Queue to store undelivered messages. When User B reconnects, the server pushes the queued messages to the client.

Message Queue and Storage

Messages are stored temporarily in a distributed message queue (e.g., Kafka or RabbitMQ). Once a message is delivered and acknowledged, it can be deleted or archived.

Database Design

Scalability Strategy

Horizontal Scaling

Different microservices handle different tasks — message service, media service, notification service, etc. We scale each service independently based on traffic.

Sharding

To handle billions of messages, we shard the message storage by user ID or region to distribute load across databases.

Media Sharing Design

WhatsApp supports image, video, document sharing. Media files are not sent directly through the messaging queue. Instead, they are uploaded to cloud storage (e.g., Amazon S3), and a download link is sent through the message.

Example:

User A sends a video:

  1. Client uploads video to cloud storage and gets a secure URL.
  2. Client sends the video URL as a message to User B.
  3. User B downloads the media using the URL.

Message Status Updates

WhatsApp shows three status levels for messages: Sent ✓, Delivered ✓✓, and Read ✓✓ in blue. Each transition is acknowledged back to the sender:

Question:

What if the recipient is offline?

Answer:

The server will hold the message and status updates until the recipient comes online and acknowledges it.

End-to-End Encryption

WhatsApp uses the Signal Protocol for end-to-end encryption. Each message is encrypted on the sender’s device and decrypted only on the receiver’s device.

Key Concepts:

Group Messaging Design

Group chats are trickier as messages must be delivered to multiple recipients.

Broadcast Model:

  1. Sender sends one message to server.
  2. Server fans out that message to all group members via their respective message queues.

Optimization:

Store message once and reference it in each user's inbox to reduce duplication.

Notifications and Offline Handling

When users are offline, messages are queued, and the server sends push notifications via FCM/APNs. Upon reconnect, the app syncs new messages.

Monitoring and Reliability

Monitor systems using tools like Prometheus, Grafana, and Sentry for real-time alerts and performance tracking. Deploy failover systems and replication to ensure high availability.

Conclusion

Designing WhatsApp involves balancing real-time delivery, storage efficiency, reliability, and encryption. By splitting the architecture into microservices and separating message content from media, we can design a scalable and robust system.

Key Takeaways



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M