KatInterview

Build a Scalable Real-Time Messaging Backend for Group Conversations

System DesignModel answer
HardModel answerSystem Designsystem-designchatwebsocket

Last updated

Design a messaging backend for 1:1 and group conversations with near-real-time delivery, exact unread counts, durable history, offline sync, and clear ordering guarantees at large scale.

Task

Design the backend of a messaging product used by a large marketplace to connect two parties, and also to host multi-person group threads. A user can start a conversation with one or more people, post text messages, and see others' messages show up almost immediately. Every participant must see a correct unread count per conversation and be able to scroll back through the entire history.

Functional scope

  • Direct (two-person) and group conversations
  • Sending and receiving text; small payloads, with attachments passed by reference
  • Creating groups and adding or removing members
  • Unread count per conversation, plus paginated history
  • The same history available on all of a user's devices
  • Delivery to people who are offline at send time

Non-functional goals

  • Highly available, low-latency send and receive; p95 delivery to connected recipients under one second
  • Durable: once the server confirms a message, it must not be lost
  • An explicitly stated ordering and consistency model, with justification

Assumptions (adjust and state your own)

  • About 50M monthly active users and 5M simultaneous connections at peak
  • About 1B messages per day: roughly 12K writes/s on average, about 60K/s at a 5x peak
  • Typical conversations hold up to ~100 members. Explain what changes when one reaches thousands or tens of thousands
  • Reads dominate: opening a conversation loads the latest ~30 messages, and older history is paged

What to cover

  1. Clarifying questions and the guarantees you commit to (ordering, delivery semantics, consistency)
  2. API surface: conversations, membership, send, history, delivery and read acknowledgements
  3. Entities, partition and clustering keys, and the storage engine for the message log versus metadata
  4. The send path: validation, sequence assignment, durability boundary, when the sender is acknowledged
  5. How a message gets its position, and why timestamps alone fail
  6. The connection layer, locating a recipient's live socket, and scaling to millions of sockets
  7. Fan-out on write versus on read, and where the switch happens as groups grow
  8. Offline delivery, retries, push wake-ups, reconnect and a second device catching up
  9. Exact per-user unread counts and efficient deep pagination
  10. Membership changes: what new members see, whether removed members get in-flight messages
  11. Read receipts and typing indicators, and their cost in large groups
  12. Sharding, hot conversations, failure modes, metrics and alerts

Hints

Hint 1

Model each conversation as an append-only log with a per-conversation sequence number, then derive unread counts, sync cursors and membership visibility from sequence numbers rather than from timestamps.

Hint 2

Decide where the durability boundary is (replicated log or quorum write) and acknowledge the sender only after it. Make everything downstream (inbox updates, socket delivery, push) an asynchronous consumer of that log so it can be replayed.

Hint 3

Choose fan-out per conversation size: write-time fan-out for small groups, a shared per-conversation topic plus pull-on-open for very large ones. Treat push notifications as wake-up hints and sync by cursor as the real delivery path.