Design a high-throughput message queue

    A distributed message queue / log (Kafka) provides durable, ordered, high-throughput pub/sub with replay. The key concepts are the partitioned append-only log, replication for durability, and consumer offsets that enable replay and parallelism.

    Streaming & Pipelines, 9 components, about 40 min

    1. Scope

    In scope

    • • Durable ordered message storage
    • • High-throughput produce/consume
    • • Consumer groups for parallelism
    • • Replay from any offset

    Out of scope

    • • Exactly-once across systems (mention)
    • • Stream processing engine
    • • Schema registry

    2. Functional requirements

    Must have

    • • Produce messages durably
    • • Consume in order within a partition
    • • Multiple consumer groups read independently
    • • Survive broker failure

    Nice to have

    • • Compaction
    • • Tiered storage
    • • Transactions/exactly-once

    3. Non-functional requirements

    ThroughputMillions of msgs/sec
    DurabilityReplication factor 3
    OrderingPer-partition guaranteed
    RetentionTime or size based; replayable

    4. Back-of-envelope estimation

    Assumptions

    • • 1M msgs/sec, avg 1 KB
    • • Replication factor 3
    • • 7-day retention
    • • Partitions sized for parallelism

    Derived numbers

    Ingest~1 GB/s— 3 GB/s with replication
    7-day storage~1.8 PB— incl. replicas
    PartitionsThousands— For consumer parallelism

    5. API design

    PRODUCEtopic, key, value

    Append to a partition (by key hash)

    CONSUMEtopic, group, offset

    Read sequentially from an offset

    COMMITgroup offset

    Checkpoint progress for replay/resume

    6. High-level design

    What each box in the reference diagram is responsible for:

    Brokers — Store partitions on disk; each partition has a leader broker.
    KRaft / ZooKeeper — Cluster metadata + leader election.
    Producers — Append messages; partition by key.
    Consumer Groups — Members split partitions for parallel consumption.
    Tiered Storage (S3) — Offloads cold segments to cheap storage.

    7. Data model

    Log: partition = ordered [offset → message] on disk— Append-only segments
    __consumer_offsets: group, topic, partition → committed offset

    8. Deep dives

    Partitions = ordering + parallelism

    A topic is split into partitions, each an ordered append-only log on disk. Ordering is guaranteed within a partition (messages with the same key go to the same partition). Parallelism comes from having many partitions: each consumer in a group owns a subset, so throughput scales with partition count. The trade-off: more partitions = more parallelism but more overhead and no global ordering.

    Replication & ISR

    Each partition is replicated to N brokers (1 leader + N-1 followers). Writes go to the leader and replicate to followers; the in-sync replica (ISR) set tracks caught-up followers. min.insync.replicas controls how many must ack before a write is considered committed — balancing durability vs availability. On leader failure, a follower in the ISR is promoted.

    Offsets & replay

    Consumers track their position (offset) per partition, committed to a special topic. Because the log is retained (not deleted on read), a consumer can replay from any offset — reprocess history, recover from a bug, or bootstrap a new consumer group independently.

    Why it's fast

    Sequential disk writes (append-only) are nearly as fast as memory; the OS page cache + zero-copy sendfile move data to consumers without userspace copies. Batching and compression amortize per-message overhead.

    9. Bottlenecks

    • Hot partition (bad key) → choose high-cardinality keys
    • Rebalance storms → static membership, cooperative rebalancing
    • Disk capacity → tiered storage to S3

    10. Trade-offs

    Durability setting

    Choose: Replication factor 3, min.insync.replicas = 2, producers with acks=all.

    Instead of: acks=1: lower latency, but a leader crash loses acknowledged messages.

    Tolerates one broker failure without losing acknowledged data.

    Partition count

    Choose: Enough partitions for target consumer parallelism and throughput (for example 2–3× current consumers), keyed by the entity that needs ordering.

    Instead of: Very many partitions: more parallelism, but more open files, slower leader elections and rebalances.

    Partitions are the unit of both ordering and parallelism; they are hard to change later without breaking key ordering.

    Delivery semantics

    Choose: At-least-once with idempotent consumers; Kafka transactions only for read-process-write pipelines inside Kafka.

    Instead of: Exactly-once everywhere: only holds within Kafka, not for external side effects.

    External systems need idempotent writes anyway.

    11. Failure modes

    Consumer group rebalancing repeatedly

    Processing stalls.

    Mitigation: Cooperative rebalancing, static membership, and processing that finishes within max.poll.interval.

    Hot partition from a skewed key

    One consumer lags while others idle.

    Mitigation: Better key choice or salting hot keys (accepting loss of ordering for them).

    Disk full on brokers

    Producers blocked.

    Mitigation: Size retention to disk with headroom, alert on usage, tiered storage to object storage for old segments.

    12. What interviewers probe

    Answer each one yourself before opening it.

    Why is Kafka fast?Show a strong answer

    Sequential append to a log, the OS page cache, zero-copy sendfile to consumers, and batching with compression.

    What happens when a leader broker dies?Show a strong answer

    The controller elects a new leader from the in-sync replica set; producers with acks=all lose nothing acknowledged.

    How do consumers track progress?Show a strong answer

    Committed offsets per partition in an internal topic; commit after processing for at-least-once.

    How much storage for 1 GB/s with 7-day retention?Show a strong answer

    1 GB/s × 604,800 s ≈ 605 TB per copy, ~1.8 PB with replication factor 3, before compression.

    Sign in to track this guide in your curriculum progress.