DEV Community

Harshit Singh
Harshit Singh

Posted on • Updated on

WhatsApp System Design: A Humorous Journey Through High-Level and Low-Level Architecture

Welcome aboard, fellow traveler, to the wonderful and chaotic world of WhatsApp System Design! This article will not only demystify the high-level (HLD) and low-level (LLD) architecture of WhatsApp but also throw in some humor (because system design doesn’t have to be boring!) and draw you some diagrams (because we all love flowcharts).

So buckle up, grab a cup of coffee, and let’s embark on a journey where servers, databases, and messaging protocols unite to bring billions of messages to your phone.

Table of Contents:

  1. High-Level Architecture (HLD)
  2. Low-Level Architecture (LLD)
  3. Flowcharts: The Design Heroes
  4. Core Components Breakdown
  5. Why These Components?
  6. Fun Facts and WhatsApp Specific Optimizations

1. High-Level Architecture (HLD): The Big Picture

Imagine WhatsApp as a well-orchestrated symphony, but instead of violins, we have servers, and instead of cellos, we have databases. At a high level, we are designing a system that supports:

  • Billions of users
  • Real-time messaging
  • Multimedia sharing
  • End-to-end encryption
  • High availability and low latency (nobody likes waiting for "typing...")

HLD Overview:

In HLD, we think like an architect. You’re not concerned about the shape of each window yet—you just want to make sure the house doesn’t collapse.

At a 30,000-foot view, the architecture of WhatsApp consists of:

  • Client Applications (iOS, Android, Web)
  • API Gateway
  • Load Balancers (balancing the chaos of millions of messages)
  • Application Servers (where the magic happens)
  • Database Layer (because data has to live somewhere)
  • File Storage (for those cat GIFs)
  • Message Queues (Kafka-like systems for real-time messaging)
  • Notification Servers (gotta let you know when your crush replies)

Core Elements of HLD:

  1. Client Applications: WhatsApp works on mobile (iOS/Android), web, and desktop apps, all of which connect to the same backend servers. The client is responsible for UI/UX, sending/receiving messages, encryption/decryption (we’ll get to that), and reconnecting when your Wi-Fi decides to take a coffee break.
  2. API Gateway: This is the middleman that handles requests from clients and forwards them to the appropriate backend service. The API Gateway checks if you’re properly authenticated, logs your message requests, and sends you to the right server.
  3. Load Balancers: With millions of users online, you need an orchestra conductor (or two) to make sure that no one server gets swamped. Load balancers distribute requests across many application servers, which prevents overload and makes things super fast.
  4. Application Servers: These bad boys are the brain of WhatsApp. They process messages, manage user sessions, and perform encryption. The key here is scalability; if more users join, we add more servers.
  5. Database Layer: Where do all your messages and media go? This is where databases come in:
    • NoSQL Databases (Cassandra): For storing large-scale data like user profiles, message history, etc.
    • SQL Databases: In rare cases, where relational data is required (such as financial records).
  6. File Storage: All your photos, videos, and voice notes are stored in scalable distributed file storage systems. Think S3 (but WhatsApp probably built something customized—because they’re cool like that).
  7. Message Queues (Kafka/Redis): For real-time messaging, WhatsApp uses message queues to handle message delivery across different servers. If a user is offline, the message is stored in a queue until they come back.
  8. Notification Servers: When your phone's screen is off, WhatsApp sends a notification through services like APNs (Apple Push Notification Service) and Firebase Cloud Messaging for Android.

HLD Flowchart:

Here’s a basic flowchart to visualize the interaction between clients, backend services, and databases in WhatsApp:

                   +---------------+               +--------------+
Client (Mobile) -->| API Gateway    |---> LB --->   | Application  |
(Client (Web)) --> | (Rate limiting)|               | Servers      |
                   +---------------+               +--------------+
                           |                             |
                           |                             |
                           V                             V
                    +-------------+               +--------------+
                    | Message     |               | Notification |
                    | Queues      |               | Servers      |
                    +-------------+               +--------------+
                           |
                           V
                   +---------------+
                   |   Databases    | (Cassandra, File Storage)
                   +---------------+

Enter fullscreen mode Exit fullscreen mode

2. Low-Level Architecture (LLD): The Nitty-Gritty Details

In LLD, we focus on the implementation and technical details of individual components. This is where we dive deep into algorithms, database sharding, encryption methods, and network protocols.

Key Concepts in LLD:

  1. Message Delivery System:
    • WhatsApp uses an XMPP protocol for real-time message delivery. This is a lightweight, efficient protocol that handles both one-on-one and group messaging.
    • Messages are stored temporarily if the recipient is offline and delivered once they come online.
  2. End-to-End Encryption:
    WhatsApp’s end-to-end encryption is based on the Signal Protocol. The idea is simple but genius:

    • Every message has its own unique encryption key.
    • Neither WhatsApp nor any third party can read your messages because only the sender and recipient hold the necessary keys.

    If WhatsApp were a spy novel, the message would self-destruct if the wrong person tries to read it!

  3. Data Storage and Replication:

    • Cassandra (NoSQL database) is used for storing chat messages. Why? It’s distributed, highly available, and can handle replication across multiple data centers. Cassandra makes sure that even if a server crashes (which happens when servers decide they need a nap), the data is not lost.
    • Media files (like pictures and videos) are stored separately from text messages, often in a cloud-based file storage system.
  4. Handling Offline Users:

    • If you send a message and the recipient is offline, the message is queued using a system like Kafka/Redis. It’s like leaving a note on someone’s door when they’re not home, except the note is safely tucked away in a queue.
  5. Database Sharding:

    • With millions of users, WhatsApp can’t store everyone’s data in one giant database. That would be like trying to fit all the people in the world into a single elevator.
    • Instead, WhatsApp shards the database. Sharding is like splitting the elevator into a hundred smaller ones, each responsible for its own group of users.

LLD Flowchart:

Here’s a simplified LLD flowchart focusing on real-time messaging and message queues:


      +------------------+   Send Message   +-------------------+
      | Client App        |---------------->| API Gateway        |
      +------------------+                  +-------------------+
                  |                                  |
                  |         Authenticate User        |
                  V                                  V
          +----------------+                 +------------------+
          | Message Queue   |  <--Store Msg---| Application      |
          | (Kafka/Redis)   |                 | Servers (XMPP)   |
          +----------------+                 +------------------+
                       |                               |
                       |   Offline/Store Msg            |
                       |------------------------------->
                       V
                +-------------+
                |   Database   | (Sharded Cassandra, File Storage)
                +-------------+

Enter fullscreen mode Exit fullscreen mode

3. Flowcharts: Our Design Heroes

Let's add some diagrams to make things super clear. Imagine flowcharts as the architects' blueprints; they help us understand the system visually.

Message Sending Flow:


Client (Mobile App)
     |
     V
API Gateway  --->  Authenticate ---> Forward to Application Server
     |
     V
Load Balancer ---> Routes to Least Busy Server
     |
     V
Message Queue ---> Holds the message if the user is offline
     |
     V
Database ---> Saves the message for future retrieval

Enter fullscreen mode Exit fullscreen mode

Message Retrieval Flow:


Client (Mobile App) ---> Sends Request to API Gateway
     |
     V
API Gateway ---> Validates Request ---> Passes to Application Server
     |
     V
Message Queue ---> Delivers the queued messages if the user was offline
     |
     V
Database ---> Fetches Message History
     |
     V
Client ---> Displays Messages

Enter fullscreen mode Exit fullscreen mode

4. Core Components Breakdown

Let’s break down some of the critical components in more detail:

  • XMPP: The messaging protocol used to send and receive real-time messages.
  • Kafka/Redis: Responsible for queuing messages when the recipient is offline.
  • Cassandra: NoSQL database that stores messages, user data, and chat history.
  • Signal Protocol: Powers the end-to-end encryption for message privacy.
  • Sharding: Divides the database into smaller, more manageable pieces to handle millions of users.

5. Why These Components?

Why Cassandra?

  • Because WhatsApp needs high availability, low latency, and the ability to handle distributed databases over multiple locations, Cassandra is the perfect fit. Plus, it’s designed to never go down, and WhatsApp loves that reliability.

Why XMPP?

  • XMPP is lightweight, efficient, and designed for real-time messaging. Perfect for a system where you can’t afford to be late—like telling your friends about the movie starting in 5 minutes.

Why Kafka/Redis?

  • Message queues make sure that no message is lost, even if the recipient is on vacation in a no-Wi-Fi zone. Kafka and Redis are reliable, fast, and scalable.

Why Signal Protocol for Encryption?

  • WhatsApp doesn’t want to read your messages (I promise). With end-to-end encryption, they physically can’t read them because only you and your recipient have the keys.

6. Fun Facts and WhatsApp Specific Optimizations

  1. Multi-device Support: WhatsApp allows users to use the same account on multiple devices. This requires careful session management and message synchronization.
  2. Efficient Media Storage: WhatsApp optimizes media storage by deduplicating identical media files shared across different chats (because we all have that one friend who sends the same meme to every group).

Conclusion: Bringing It All Together

So there you have it—a tour through the system design of WhatsApp! We’ve explored high-level architecture (HLD), delved into low-level design (LLD), and even thrown in some humor to make the journey fun. From XMPP protocols to Kafka queues, Cassandra databases, and Signal encryption, WhatsApp is a masterpiece of scalable, real-time messaging that keeps our group chats alive and kicking!

Top comments (1)

Collapse
 
wittedtech-by-harshit profile image
Harshit Singh

Do Share Your Feedbacks.