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
Start by agreeing on the scope. Here is each question to ask, with the answer this design assumes.
- How many users and messages? 500 million daily active users, each sending 40 messages a day.
- Are there groups, and how big? Yes, with up to about 1,000 members.
- Can an account have several devices? 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.
- What about encryption? It is end to end. The server sees who talks to whom, not what they say.
The design at a glance
Every online phone holds one connection, through a load balancer, to a chat server that serves about a million phones. When a message arrives, that server reads the group and device lists if it needs them (with each device's keys), stores the encrypted message in the mailbox of every recipient device, and asks a session registry which chat server holds each recipient. It forwards the message there, and that server pushes it down the recipient's connection. A device that is offline is woken by a push notification, and the message waits in its mailbox until it connects. The mailbox store holds pending messages only: a row is deleted once its device acknowledges it.
Photos and videos never take this path. Phones upload them to a media blob store that holds only ciphertext, and the recipient downloads them from there. The sections below take these parts one at a time, starting with the connections.
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