KatInterview

Backend Architecture for a Scalable Group Messaging Platform

System DesignModel answer
HardModel answerSystem Designmessagingidempotencystorage

Last updated

Design the server-side core of an enterprise chat product: APIs, storage, idempotent and ordered message delivery, versioned membership access control, and real-time fan-out at scale.

Scenario

You are building the backend infrastructure for a workplace communication tool (think Slack or Microsoft Teams). The platform needs to handle both one-on-one direct messages and large group conversations (up to 2,000 members). Messages must be delivered reliably, ordered consistently within each conversation, and access must be tightly controlled as membership evolves over time.

Functional Requirements

  • Conversation Types: Support both 1:1 direct messages and group chats with up to 2,000 participants.
  • Duplicate-Free Ingestion: When a client retries a send due to a network timeout or failure, the system must not produce duplicate messages.
  • Monotonic Per-Conversation Ordering: Every participant in a given conversation must observe messages in the same strict, monotonically increasing order.
  • Membership-Aware Visibility: A user who joins a conversation mid-stream should only see messages from their join point onward (unless a full-history policy is specified). A user who is removed must immediately stop receiving new messages in that conversation.
  • Paginated History Retrieval: Clients should be able to page through message history forward or backward from a specific sequence number or timestamp efficiently.

Non-Functional Requirements

  • Traffic: 50 million daily active users. Approximately 500 million messages per day (roughly 6,000 messages/second average, 25,000/second at peak).
  • Latency: P99 push-to-online-client latency under 100 ms. History read queries under 50 ms.
  • Reliability: Once the server acknowledges a message, it must never be lost. Sequence assignment within a conversation must be linearizable.

What to Cover in Your Design

  1. API Contracts: Define the request/response shapes for sending a message, fetching history, and updating conversation membership.
  2. Storage Schema and Partitioning: Describe the data model for conversations, messages, membership, and idempotency tracking. Justify your database choice and partitioning approach.
  3. Sequence Allocation and Deduplication: Explain how each conversation gets gap-free monotonic sequence numbers, how client retry tokens prevent duplicates, and how concurrent senders are handled.
  4. Membership Versioning and Read Authorization: Describe how join/leave/kick events are recorded and how the read path enforces visibility windows without expensive runtime joins.
  5. Write Path and Real-Time Fan-Out: Walk through the flow from message ingestion through persistence to real-time delivery to connected recipients.

Hints

Hint 1

Think about what level of consistency you actually need — is a global total order required, or is per-conversation linearizability sufficient?

Hint 2

Consider modeling membership changes as events in the same ordered log as messages, so there is never ambiguity about whether a message came before or after a join or removal.

Hint 3

Separate the concerns of sequence generation (a per-conversation monotonic counter) from bulk storage scaling (partitioning by conversation key range) so each can scale independently.