KatInterview

Build a Multi-Device Chat Backend for Two-Party Conversations

System DesignModel answer
MediumModel answerSystem Designsystem-designmessagingwebsockets

Last updated

Design a low-latency chat service for two-party conversations where each user has several devices, covering delivery state, push fallback, retry-safe sends and stable history paging.

Problem

You are asked to design the backend for a direct-messaging feature between exactly two parties, for example a customer and a support agent, or two members of a marketplace. Each person may be signed in on several devices at once (phone, tablet, browser). A message sent from one device must show up on every other device of the sender, and on every device of the recipient. Devices that are offline or backgrounded should be reached through mobile push (APNs/FCM).

Functional requirements

  • Send text messages between two users with low latency.
  • Multi-device sync: all devices of both participants converge on the same conversation view.
  • Message states: sent, delivered and read, visible consistently across devices.
  • Push fallback for devices with no live connection.
  • History: clients can load older messages without gaps or duplicates, even while new messages keep arriving.
  • Retry safety: flaky networks and client resends must never produce duplicate messages.

Non-functional requirements

  • Online-to-online delivery under 150 ms.
  • 99.99% availability of the messaging path.
  • Strict ordering inside a single conversation; transport is at-least-once, so clients must process messages idempotently.

Scale

  • 50 million daily active users, about 2 devices each.
  • 500 million messages per day (about 6,000/s average, 25,000/s peak).
  • Up to 10 million concurrent persistent connections.

What to cover

  1. APIs, data model, partitioning and the ordering mechanism.
  2. High-level architecture: connection gateways, session registry, storage, notification pipeline.
  3. How a message reaches every active session, and how read state propagates.
  4. A pagination scheme that stays correct under concurrent inserts.
  5. Failure handling: reconnects, deduplication, out-of-order arrival.

Back your design with rough capacity numbers and call out the trade-offs you are making.

Hints

Hint 1

Convert the scale into numbers first: peak sends multiplied by roughly four target devices (two for each participant) gives your fan-out rate, and 10M sockets tells you how many gateway nodes you need.

Hint 2

Order messages with a per-conversation sequence number assigned by a single owner for that conversation, and use a client-generated message ID so a retry can be recognised and answered with the same sequence number.

Hint 3

Track delivered and read as per-user, per-conversation watermarks (highest sequence number) rather than per-message flags, and give each device a sync cursor over a per-user event log so reconnecting is a simple replay.

Hint 4

For paging, avoid offsets: use a cursor on the sequence number (before for older pages, after for filling gaps), which stays stable while new rows are appended.