KatInterview

Build a Reliable Private Messaging Backend for Two-Person Chats

System DesignModel answer
MediumModel answerSystem Designsystem-designmessagingwebsocket

Last updated

Design a backend for private two-person chats that stores messages durably, keeps them in order, tolerates retries, replays to offline devices, and restricts access to participants.

Private Two-Person Messaging Service

You are asked to design the backend for a messaging product in which exactly two users talk to each other privately. Walk through how messages are sent, stored, fetched, ordered, and delivered to people who are online or offline. Also describe how read state works, how retries are handled, and how the system recovers from failures.

Be explicit about what the service promises when a client reconnects after a drop or sends the same request twice.

Assumptions

  • Every conversation has exactly two participants.
  • Text content is stored and retained. Large attachments are handled elsewhere and are not part of the core path.
  • A user can be signed in on several devices, and any device may be offline for a long time.
  • Ordering across different conversations is not needed.
  • Anyone who is not a participant must never be able to read a conversation.

Questions worth raising with the interviewer

  • Is total order needed inside a conversation, or only per sender?
  • Are delivery and read receipts in scope?
  • What are the retention and deletion rules?
  • What are the expected peak send rate and reconnect surge?

Part 1: API and data model

Define the entities (conversation, message, participant, cursor, receipt) and the APIs for sending and fetching history.

Points to address: client-generated deduplication keys, per-conversation sequence numbers, authorization on every read and write, and cursor-based pagination.

Part 2: Delivery and consistency

Describe the durable write path, the real-time push path, recovery for offline devices, synchronization across a user's devices, and how duplicates are handled.

Points to address: store before acknowledging, at-least-once delivery with client-side deduplication, ordering and resume cursors, and keeping receipts separate from message durability.

Part 3: Scale and failure handling

Cover partitioning, very active conversations, WebSocket gateway design, backpressure, regional outages, and monitoring.

Points to address: a stable partition key, bounded fan-out, stateless gateways with resumable sessions, protection against lag and slow clients, and reconciliation after partial failures.

Hints

Hint 1

Make the send call safe to repeat: the client generates a unique key per message, and the server returns the already-stored message if it sees that key again.

Hint 2

Give each conversation its own monotonically increasing sequence number assigned at write time. Clients resume from 'last sequence I have seen' instead of relying on timestamps.

Hint 3

Treat the database as the source of truth and the live push as an optimization. If a push is lost, the client's next catch-up fetch from its cursor must repair it.

Hint 4

Partition by conversation ID so one conversation's writes stay ordered on one shard, and think about what happens when a single conversation is unusually busy or a client cannot keep up.