KatInterview

Architect a Real-Time Direct Messaging Backend

System DesignModel answer
MediumModel answerSystem Designsystem-designwebsocketkafka

Last updated

Design a backend for private two-person messaging that stays durable, ordered and deduplicated, with online and offline delivery, presence, and a justified split between Kafka and Redis.

You are asked to design the backend for a messaging product where two people exchange private text messages in real time.

Scope

  • Only direct, person-to-person conversations. Group chats, channels, and threads are out of scope.
  • Text messages only. A user may be signed in on several devices at once.

Before drawing anything, state your assumptions about:

  • user count and message volume,
  • latency targets for online delivery,
  • durability and reliability expectations.

Then cover the following in your design

  1. The main flows for sending a message and for receiving one.
  2. How messages are stored and how history is fetched.
  3. Delivery when the recipient is connected.
  4. Delivery when the recipient is not connected.
  5. How the system decides whether someone is online.
  6. Where client sessions and WebSocket connections are tracked, and how they are managed through crashes and deploys.
  7. Ordering, retries, deduplication, and delivery/read acknowledgements.
  8. Where Kafka fits, where Redis fits, and the trade-offs between the two.
  9. The Kafka internals that matter for this design: partitions, per-partition ordering, offsets, and consumer groups.

Expect to justify your choices and discuss failure cases.

Hints

Hint 1

Pick numbers first (daily users, messages per user, concurrent connections) and derive messages per second, gateway count, and daily storage growth from them.

Hint 2

Treat the database as the source of truth and the message bus as only a notification path. That makes the offline case a catch-up read rather than a separate queue.

Hint 3

Give each conversation its own monotonically increasing sequence number and make every acknowledgement a cursor over that sequence, not a per-message status row.

Hint 4

Ask what is lost if Redis or a gateway dies, and what the client does next. Use client-generated message IDs, idempotent writes, and TTL-based session entries to answer.