WhatsApp

Messaging service

Design a mobile messaging service for one-to-one and group chats that delivers every message exactly once to every device, even after days offline, with end-to-end encryption.

Overview

A messaging service moves small, private messages between phones. Unlike a feed, every message has specific recipients, and each one must arrive: not lost, not shown twice, and in the order it was sent. Recipients are often offline, so the server has to hold messages until their devices come back.

Three things make it hard at scale. Every online phone keeps a connection open to the service, so the service holds hundreds of millions of connections at once. Delivery must survive dropped connections, retries and crashes without duplicates. And with end-to-end encryption, the server routes messages it cannot read, which moves work such as encrypting for each device and each group member onto the phones.

Questions to ask first

  • How many users and messages? — 500 million daily active users; 40 messages sent per user a day
  • Groups? How big? — Yes, up to about 1,000 members
  • Several devices per account? — Yes: one phone plus a few linked devices
  • Does the server keep chat history? — No: it holds a message only until every device has it
  • How long can a device be offline? — Days or weeks; undelivered messages expire after 30 days
  • Encryption? — End to end; the server sees who talks to whom, not what they say

What you will be able to do

  • Size and route hundreds of millions of persistent connections.
  • Explain store-and-forward delivery, acknowledgments and receipts.
  • Get exactly-once display from at-least-once delivery with message IDs.
  • Compare client fan-out and server fan-out for devices and groups under end-to-end encryption.
  • Say what the server stores, for how long, and what it can and cannot see.

Key concepts

  • Persistent connections
  • Session registry
  • Store and forward
  • Delivery receipts
  • At-least-once plus dedup
  • Client fan-out
  • Sender Keys
  • Encrypted attachments
  • Presence

Functional requirements

  • Send a text message to a person or a group
  • Deliver it at once to online devices, and later to offline ones
  • Show sent, delivered and read status to the sender
  • Deliver every message to all of a user's linked devices
  • Send photos and videos as attachments
  • Show whether a contact is online and typing

Non-functional requirements

  • Messages to online devices arrive in under 500 ms at p99 within a region (a target we chose)
  • No message is lost once the server has acknowledged it
  • Each message is shown once, in the order sent within a chat
  • Chat servers can fail or be redeployed without losing messages
  • The server cannot read message content

Interview questions

  • How does a message reach a user who is offline?
  • How do you make sure a message is shown exactly once?
  • How do you scale to hundreds of millions of open connections?
  • How do group messages work with end-to-end encryption?
  • How do sent, delivered and read receipts work?
  • How do you guarantee ordering within a chat?
  • Where do photos and videos go?
  • What happens when a chat server crashes?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio