Designing a Distributed Message Queue (Kafka)

Difficulty: Advanced Topics: Partitioned Log, Consumer Groups, Exactly-Once Delivery, Replication, Ordering Asked at: Google, Amazon, Microsoft, LinkedIn, Uber, Confluent · Prerequisites: Scalability, Database Replication, and Leader Election


1. Understanding the Problem

A distributed message queue decouples producers from consumers — services publish events without knowing who consumes them, and consumers process at their own pace without blocking producers. The hard engineering problems: guaranteeing no message loss even when brokers crash, maintaining strict ordering within a partition while allowing parallel consumption, and achieving exactly-once semantics in a distributed system where networks are unreliable.
💡 Exactly-once semantics = every message gets processed one time, never zero and never twice. It is the hardest guarantee to give because a lost network reply makes “did my write land?” genuinely unanswerable. Learn more →

Real examples: Apache Kafka, Amazon Kinesis, Google Pub/Sub, RabbitMQ, Apache Pulsar, Redpanda.


2. Naive First Cut

flowchart LR
    PROD["Producer"]
    QUEUE["Single Server<br/>in-memory queue"]
    CONS["Consumer"]

    PROD -->|"push message"| QUEUE
    QUEUE -->|"pop message"| CONS

In-memory FIFO queue on a single server. Producer pushes, consumer pops.

Why this breaks:

  • Server crash = all messages lost (in-memory, no persistence)
  • Single server limits throughput to one machine’s I/O (~100K msg/sec)
  • One consumer means no parallel processing — throughput bottlenecked by consumer speed
  • No replay — once a message is consumed, it’s gone (can’t reprocess after a bug fix)
  • No ordering guarantees when you add multiple consumers
  • No backpressure — if consumers are slow, memory fills up and the server runs out of memory (OOM) and dies
    💡 Backpressure = letting a slow consumer push back so producers slow down, instead of the buffer quietly growing until the machine dies. Without it the only limit on the queue is the size of RAM.

The rest of the doc evolves this into a replicated, partitioned, persistent log with consumer groups.


3. Prior Art We’re Drawing From

  • LinkedIn Kafka - Invented the partitioned commit log model. Messages are appended to an immutable, ordered log. Consumers track their own position (offset) and can replay from any point. This single abstraction replaced dozens of point-to-point queues at LinkedIn. (LinkedIn Engineering)
  • Amazon Kinesis Shards - Similar to Kafka partitions but with a different scaling model: throughput is provisioned per shard (1MB/sec write, 2MB/sec read). Auto-scaling splits hot shards when write capacity is exceeded.
  • Google Pub/Sub - Separates the concept of “topic” from “subscription.” Multiple subscriptions to the same topic each get independent message delivery — no consumer group coordination needed. Messages are stored until acknowledged (pull-based with ACK deadlines).
  • Redpanda (Raft-based replication) - Replaces Kafka’s ZooKeeper-based controller with embedded Raft consensus per partition. Simplifies operations and provides stronger consistency guarantees during leader elections. (Redpanda Blog)
    💡 Raft = a protocol a group of machines uses to agree on one ordered list of decisions and on who is currently in charge. It is how a cluster answers “who owns partition 3 right now?” without two machines both believing it is them. Learn more →

4. Functional Requirements

Core (Top 3)

  1. Publish messages to a topic - producers write messages to named topics with guaranteed durability (acknowledged only after replication)
  2. Consume messages with ordering - consumers in a group process messages in partition order, exactly once, with the ability to replay from any offset
  3. Scale throughput horizontally - adding partitions and brokers increases capacity linearly without downtime

Below the Line

  • Message filtering (server-side tag-based routing)
  • Dead-letter queues for poison messages
    💡 A dead-letter queue (DLQ) is a side queue for messages that keep failing. Without one, a single unprocessable “poison” message blocks its partition forever because the consumer retries it and never gets past it.
  • Message time-to-live (TTL) and compaction (retain latest per key)
  • Transactions across multiple topics
  • Multi-region replication

5. Non-Functional Requirements

Core

  • Throughput: 1M+ messages/sec per cluster (with appropriate partitions)
  • Latency: Produce-to-consume P99 (the delay that 99% of messages beat) < 10ms (within a single datacenter)
    💡 Averages hide the bad cases, so capacity targets are written against a high percentile. A 2ms average with a 400ms P99 means one message in a hundred is unusably slow. Learn more →
  • Durability: Zero message loss — acknowledged writes survive broker crashes
  • Ordering: Strict ordering within a partition (total order)