KatInterview

Build a Real-Time Direct Messaging Backend for Millions of Users

System DesignModel answer
MediumModel answerSystem Designsystem-designwebsocketmessaging

Last updated

Design a horizontally scalable direct-messaging backend with live delivery, stored history, presence, per-conversation ordering and fault tolerance for 10 million daily users.

Direct Messaging Backend

You are asked to design the backend for a mobile and web messaging app in which two people exchange private text messages. The service must remain responsive as it grows to 10 million daily active users, with messages arriving at the recipient in well under a second.

What users must be able to do

  1. Send a message to another user and have it appear on their screen almost immediately when they are online.
  2. Open a conversation and scroll through earlier messages.
  3. See whether the other person is currently online, or when they were last seen.

Qualities the system must have

  • Availability: the loss of one server must not take the service down or lose messages.
  • Latency: delivery should feel instant to both parties.
  • Ordering: within a conversation between two users, messages must appear in the order they were sent.
  • Scale: the design has to hold up at 10M DAU with a large number of simultaneous open connections.

What to cover in your answer

  • Capacity estimates that justify your choices.
  • API design: the real-time protocol and any request/response endpoints for history, sync and presence.
  • Data model: how users, conversations and messages are stored, and why.
  • Architecture: how clients, load balancers, connection servers, application services and storage fit together.
  • Delivery paths: what happens when the recipient is online, and what happens when they are offline.
  • Trade-offs: relational vs. non-relational storage for messages, and at-least-once vs. exactly-once delivery.

Group chats, media, voice/video and end-to-end encryption can be treated as out of scope, though you may mention how the design would be extended.

Hints

Hint 1

Begin with rough numbers: messages per user per day, average and peak write rate, and the number of simultaneous connections. Ask yourself which of these is actually the hardest part to scale.

Hint 2

Split responsibilities: servers that merely hold long-lived connections should be separate from the services that sequence, store and route messages, and you need a fast lookup that says which connection server a given user is attached to.

Hint 3

Think about what makes retries safe and what gives a single, trustworthy order per conversation. A client-generated message ID and a per-conversation sequence number each solve one of those problems.

Hint 4

For offline users, ask what must already be true before the sender is told the message was accepted, and how a returning client can discover what it missed.