KatInterview

Architecting a Real-Time Messaging System at Scale

System DesignModel answer
MediumModel answerSystem Designmessagingkafkawebsockets

Last updated

Design a messaging backend that sustains 1M concurrent connections and 50K messages per second with sub-100ms end-to-end latency.

Context

Your team is building a communication platform — think real-time chat combined with a system notification layer. Product has signed off on the following requirements and you need to walk through the architecture in a technical discussion.

Functional Requirements

  • Users can send and receive messages in real time.
  • Users can retrieve their message history for any conversation.
  • Critical system notifications (e.g., payment confirmations, security alerts) must be delivered reliably.

Non-Functional Requirements

  • Scale: Support 1 million concurrent connections and sustain 50,000 messages per second (MPS).
  • Latency: End-to-end message delivery in under 100 ms.
  • Availability: The system must remain operational during partial failures and rolling deployments.
  • Durability: No messages lost, even if a downstream consumer temporarily falls behind.

What You Are Asked to Do

Describe a high-level architecture that satisfies the requirements above. Your answer should cover the following four areas:

  1. Message Broker: What technology handles buffering and fan-out between producers and consumers? Justify your choice and explain how you would partition the data.

  2. Storage Layer: How and where do you persist messages? Compare a relational approach to a NoSQL approach for this specific workload and explain which you would choose and why.

  3. Connection Management: How do you maintain and route 1 million persistent connections across a fleet of gateway servers? What does load balancing look like, and where do you store connection state?

  4. Backpressure: What happens when a consumer cannot process messages as fast as they arrive? Describe the mechanisms at each layer of the system that prevent cascading failure.

Be concrete: name technologies, estimate numbers, and call out trade-offs. A strong answer will also identify where the most common failure modes live.

Hints

Hint 1

Start with back-of-the-envelope math before touching architecture. 50K MPS × 1 KB per message gives you ~50 MB/s of ingest, but the real bottleneck is fan-out: if each message has 5 recipients on average, that's 250K deliveries per second. Let that number drive your design choices.

Hint 2

A single server can hold roughly 50–100K WebSocket connections when properly tuned. That immediately tells you how many gateway nodes you need for 1M connections — and reminds you that gateways should be stateless so you can scale them independently from your business logic.

Hint 3

Separate the real-time delivery path from the history/persistence path. They have different consistency requirements, different read/write patterns, and different failure modes. Trying to serve both from one data store is where many designs go wrong.