KatInterview

Durable, Ordered Messaging Backend for 1:1 and Group Chat

System DesignModel answer
MediumModel answerSystem Designmessagingdurabilityordering

Last updated

Design a messaging backend where an acknowledged message is never lost, each conversation has a deterministic order, and reconnecting clients catch up without gaps or duplicates.

Problem

You are designing the backend for a workplace messaging product. It supports direct conversations between two people and group channels of up to 1,000 members. Once the sender has been told "your message was accepted", that message must survive crashes of individual servers or in-memory queues. Every recipient must see a conversation's messages in the same deterministic order. Clients that lose connectivity must be able to resume without missing or double-seeing anything.

Functional requirements

  • Direct (1:1) and group conversations, with groups of up to 1,000 participants.
  • Real-time push to connected clients, and catch-up for clients that were offline.
  • At-least-once transport combined with client-side deduplication, so users experience exactly-once delivery.
  • Membership can change over time. A user may read only messages sent while they were a member.

Non-functional requirements

  • Durability: the sender's acknowledgment may only be returned after the message is on durable, replicated storage. Acknowledging data held only in a volatile broker is not acceptable.
  • Ordering: strict, monotonic ordering within each conversation (no global order is needed).
  • Latency: P99 delivery under 100 ms to connected recipients.
  • Availability: gateway or broker failures must not corrupt data or permanently lose messages.

Scale

  • 50 million daily active users, 10 million concurrent streaming connections (WebSocket or gRPC).
  • About 1 billion messages per day: roughly 12k writes/s on average, 50k writes/s at peak.
  • Group size: median 8, P99 200, hard cap 1,000.

What to cover

  1. APIs: sending, real-time streaming, and catch-up sync.
  2. Storage and ordering: how sequence numbers are issued, the schemas, and how data is partitioned.
  3. Message lifecycle: client, gateway, sequencing and persistence, fan-out, push gateways.
  4. Durability trade-offs: memory-only brokers (e.g. Redis Pub/Sub) versus log- or quorum-backed storage (e.g. Kafka, Cassandra/ScyllaDB) before the sender is acknowledged.
  5. Reconnection: the protocol when a client returns carrying its last delivered sequence number after a drop or a server failover.

Hints

Hint 1

Make the durable log or quorum store the source of truth, and treat every in-memory pub/sub layer as a best-effort speed-up that can lose pushes without losing messages.

Hint 2

Give each conversation its own dense, gap-free counter owned by a single writer at a time (a leased, fenced sequencer). Dense numbers let clients detect missing messages locally.

Hint 3

Make sends idempotent with a client-generated message ID, and put membership joins and leaves into the same per-conversation sequence so read-access windows are totally ordered with messages.

Hint 4

Store each message once per conversation and fan out pushes only to online users; offline users reconcile by pulling from their last known sequence number.