Design a chat backend

System design Senior Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Order the steps

Order a message send.

  • Offline recipients get a push and sync after their last sequence
  • Chat service assigns the next conversation sequence and stores it
  • Sender gets an ack with the sequence
  • Message is published to recipients' gateways
  • Client sends message with its own id over the WebSocket

Choose an answer, then open Show answer below to check it.

Boxes for client, WebSocket gateway, chat service, message store, pub/sub bus and push service; send a message to an offline user.
Text version

Boxes on the board, in order:

  1. Client
  2. WS gateway
  3. Chat service
  4. Message store
  5. Pub/sub bus
  6. Push service
Show answer

Answer: E B C D A

  1. Client sends message with its own id over the WebSocket The id makes retries safe.
  2. Chat service assigns the next conversation sequence and stores it Durable before ack.
  3. Sender gets an ack with the sequence Client marks it sent.
  4. Message is published to recipients' gateways Online users receive it.
  5. Offline recipients get a push and sync after their last sequence Catch-up path.

30-second answer

Clients keep a WebSocket to a gateway tier; a presence or session store maps user to gateway. A send goes to the chat service, which assigns a per-conversation sequence number, stores the message durably, acks the sender, and routes it to recipients' gateways via a pub/sub bus. Offline users get a push notification and fetch missed messages by 'give me everything after sequence N'. Client-generated message ids make retries idempotent.

Practise saying it · 40 s. No recording, just a timer

Design chat.

Say your answer out loud, then check which of these points you covered:

  • WebSocket gateways
  • Per-conversation sequence
  • Durable store before ack
  • Pub/sub routing
  • Offline sync by sequence

Deep dive

Partition message storage by conversation id; order by sequence within it.

Delivered and read receipts are small messages through the same path.

Large groups: fan out by reference, not copying the message per member.

End-to-end encryption moves content handling to clients.

Common mistakes

  • Ordering by server timestamps from several machines.
  • No idempotency, so retries duplicate messages.
  • Treating push notifications as the delivery channel.

Senior take & follow-ups

Connections, write path with sequence and durable store, routing, offline sync, idempotency.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you order messages?

    Show hint

    A per-conversation sequence assigned at write.

  2. How do offline clients catch up?

    Show hint

    Fetch after their last seen sequence.

Suggest a fix if something here is wrong or out of date.