KatInterview

Build a Persistent Real-Time Messaging Service with Reliable Sync

System DesignModel answer
MediumModel answerSystem Designsystem-designchatwebsockets

Last updated

Design a text messaging service where conversations are stored durably, messages arrive live, and clients recover cleanly from disconnects, retries, and duplicate events.

Design a messaging service in which users exchange text messages in real time and can later reopen any conversation to see its full history.

Walk through each of the following:

  • Client state: what the app keeps locally, and how it shows a message before the server has confirmed it.
  • APIs: the requests and events needed to send messages, load history, and sync.
  • Delivery path: how a message travels from sender to recipients, and what "sent" and "delivered" mean in your design.
  • Reconnect behavior: how a client that was offline or briefly disconnected catches up without losing or duplicating messages.

The requirements leave several things open: conversation size, encryption, attachments, and delivery guarantees. Begin by stating a bounded initial scope (for example, text only, small conversations), then explain how the design would grow beyond it.

Questions you should settle or call out as assumptions:

  • What ordering guarantee is needed inside one conversation?
  • Are edits, deletes, and read receipts in scope?
  • How do offline clients catch up?
  • What exactly distinguishes sent from delivered?

Hints

Hint 1

Give every conversation its own monotonically increasing sequence number assigned by the server. It can serve as the ordering key, the pagination cursor, and the reconnect checkpoint.

Hint 2

Let the client generate a unique ID for each outgoing message, so a retried send can be recognized and de-duplicated on the server.

Hint 3

Treat the persistent store as the source of truth and the real-time channel as a hint. After any reconnect, the client asks for everything after its last known sequence number.

Hint 4

Define a clear state progression for each message (pending, sent, delivered, read) and decide which component is responsible for each transition.