KatInterview

Building a Durable Messaging Backend: Endpoints, Schema, Sync

System DesignModel answer
MediumModel answerSystem Designsystem-designapi-designdatabase-schema

Last updated

Design the endpoints, data model, and core flows of a basic messaging service with durable sends, safe retries, ordered history, live push, and catch-up after a dropped connection.

You are asked to design the backend core of a simple messaging product. Focus on three things: the public API, the database model, and the main flows. Do not spend time on global scale or invented traffic numbers.

Scope and assumptions

  • Users are authenticated. Messages are durable text stored inside conversations.
  • State clearly whether you support one-to-one conversations, group conversations, or both, and design accordingly.
  • A send counts as successful once the message is durably stored. Treat delivered to a device and read by a user as separate states, not part of "sent".
  • Media attachments, end-to-end encryption, search, and very large scale are optional extensions that would need extra requirements.
  • No latency SLA or traffic target is given. Aim for a coherent baseline.

Questions worth raising with the interviewer

  • Can membership change over time, and should a newly added member see older messages?
  • Must messages be ordered within a conversation? What should a retry do after a send times out?
  • Are delivered and read receipts required, or only durable sends plus history?
  • Should live events reach every device a user is signed in on?

Part 1: API contract

Define endpoints to create (or join) a conversation, send a message, and fetch history. Cover authentication, membership checks, pagination, and retry behavior.

Your answer should include:

  • Identifiers for conversations and messages, plus who is allowed to do what.
  • A client-generated idempotency key so a retried send does not duplicate.
  • Stable pagination for history, and a clear line between "stored" and "pushed live".

Part 2: Database model

Define tables or collections, primary keys, useful indexes, and transaction boundaries.

Your answer should include:

  • Users, conversations, memberships, and messages, related so that access checks are simple.
  • A per-conversation ordering mechanism and an index that serves history queries.
  • Uniqueness enforcement for retried sends, and a consistent message plus outbox write if fanout is asynchronous.

Part 3: Flows and failure handling

Walk through:

  1. A successful send to an online recipient.
  2. A send to an offline recipient.
  3. A reconnect after a live connection dropped.

Your answer should include:

  • The order of authorization, durable commit, acknowledgement, and notification.
  • How missed or duplicated live events are reconciled against stored history.
  • How a timeout that happens after the commit is resolved without creating a second message.

Hints

Hint 1

Pick one conversation model (for example, groups, with a 1:1 chat as a group of two) and make the schema, authorization, and fanout all follow from it.

Hint 2

Give every message a per-conversation sequence number assigned inside the commit transaction, and add a unique constraint on (sender, client-generated message id) so retries collapse to a single row.

Hint 3

Treat the live channel as a best-effort hint. The database is the source of truth, so a client that reconnects asks for everything after the last sequence number it saw, and de-duplicates by message id.

Hint 4

Write the message row and an outbox row in one transaction, and let a separate worker publish from the outbox. This avoids the case where a message is stored but never announced.