KatInterview

Build a Chat Platform with Channels, DMs, and Presence

System DesignModel answer
MediumModel answerSystem Designwebsocketsfan-outpub-sub

Last updated

Design a real-time workspace chat backend with channels, DMs, paginated history, unread tracking, and presence, supporting 5M concurrent connections and channels of up to 50,000+ members.

Problem

You are asked to design the backend for a workplace chat product. People belong to a workspace and talk in public channels, private channels, and one-to-one direct messages. Delivery must feel instant, and nothing that has been confirmed to a sender may ever disappear.

What the product must do

  • Send and receive: Users exchange text in DMs and in channels, with live delivery to anyone who is online.
  • Browse history: Users scroll backward through older messages in a conversation, loaded in pages.
  • Presence: Users can tell whether other workspace members are online or offline.
  • Unread state: The system tracks how far each user has read in each channel so it can show unread counts.

Quality targets

  • Latency: Under 100 ms end-to-end delivery for users in the same region.
  • Ordering: Every participant sees the messages of a channel in the same strict order.
  • Durability: After the server confirms a message, it must not be lost, even through failures.
  • Scale range: The same design must serve tiny group chats and company-wide announcement channels without degrading.

Load assumptions

  • 20 million daily active users
  • 5 million simultaneous persistent connections at peak
  • 500 million messages per day, about 6,000/s on average and 25,000/s at peak
  • Channel sizes from 2 up to 50,000+ members
  • About 500 bytes per message including metadata

What your answer should cover

  1. APIs: The main client-facing operations: sending, fetching history, joining or managing channels, updating read position.
  2. Real-time connection layer: Protocol choice (persistent sockets vs. polling), how traffic is routed, and how the system knows which server holds which user's connection.
  3. Fan-out: How a single message reaches every online member, and how your approach differs between small and very large channels.
  4. Storage and schema: Which databases you would choose and how you would model users, channels, messages, and read positions.
  5. Presence: How online/offline status is collected and distributed efficiently at this scale.

Include back-of-the-envelope capacity numbers and call out the trade-offs you are making.

Hints

Hint 1

Convert the numbers into load before designing. Writes are only about 25K/s at peak, but each message is delivered to many online recipients, so delivery volume (hundreds of thousands per second) is the real driver.

Hint 2

For ordering, avoid distributed locks: route all messages of a channel through a single partition or single writer, and have that writer assign a dense per-channel sequence number. Use the same number for pagination and for gap detection on clients.

Hint 3

Treat a 2-person DM and a 50,000-member channel differently: per-recipient routing for small channels, but for huge ones deliver once per gateway server that has at least one member connected and let that server fan out locally.

Hint 4

For presence and read state, avoid per-event broadcast and per-message storage: use heartbeats with expiring keys, only publish on state changes, and compute unread counts as the difference between a channel's latest sequence and the user's read cursor.