System DesignSep 23, 2026⏱️ 3 min read
Design a Distributed Message Queue: Commit Logs, Replication, ISR & Delivery Semantics

Design a Distributed Message Queue: Commit Logs, Replication, ISR & Delivery Semantics

Designing Kafka From First Principles

Beyond using a message queue, senior engineers must understand how brokers store terabytes of streaming events at millions of messages/sec without falling over under disk I/O or distributed consensus overhead.
Distributed Message Queue Internals

Distributed Message Queue Internals


1. Storage Engine: Append-Only Partitioned Commit Logs

Relational databases struggle at stream ingestion because random B-Tree updates thrash disk heads. A distributed message queue uses sequential append-only log files:
  • Topics & Partitions: Each topic splits into $P$ partitions distributed across brokers. Every message inside a partition gets a monotonically increasing offset.
  • Segment Files: Instead of one giant 10 TB file, each partition splits into rolling Segment Files (e.g., 1 GB chunks) accompanied by .index and .timeindex sparse lookup files so offset -> byte_position finds any event in $O(\log N)$!
  • Sequential Disk + OS Page Cache: Sequential disk writes match RAM speeds on modern kernels, and Linux sendfile() zero-copy transfers bytes straight from OS page cache to the network NIC without copying into JVM/user space.

2. Consumer Groups & Rebalancing

Support both Point-to-Point queuing and Pub/Sub broadcasting cleanly with one abstraction:
  • Multiple Consumer Groups reading the same topic act as independent Pub/Sub subscribers (Billing + Analytics both read orders independently).
  • Instances within ONE Consumer Group act as competing workers: each partition is assigned to at most one consumer instance inside that group, guaranteeing strict message ordering per partition!
  • Offset Checkpointing: Store committed offsets cleanly in an internal compacted topic (__consumer_offsets) rather than hitting ZooKeeper on every ack.

3. High Availability: Leader-Follower & In-Sync Replicas (ISR)

Each partition elects one Leader Broker (handles all reads/writes) and $N-1$ Follower Replicas.
  • ISR (In-Sync Replicas): The dynamic subset of followers actively caught up within replica.lag.time.max.ms.
  • Producer Durability Settings (acks):
acks SettingBehaviorTrade-Off
acks=0Fire-and-forgetLowest latency, loses messages on crash
acks=1Leader writes locally & acksFast; loses recent writes if leader dies before replication
acks=all (-1)Leader waits for all min.insync.replicas to persistZero data loss for critical financial/order streams

4. Delivery Semantics & Batching Optimizations

  • At-Most-Once: Commit offset before processing (risks loss, zero duplicates).
  • At-Least-Once: Commit offset after processing (requires idempotent consumers to ignore duplicates).
  • Exactly-Once: Combine idempotent producer sequence numbers + transactional coordinator across read-process-write atomic offsets.
  • Throughput Boosters: Batch multiple small events client-side (linger.ms + batch.size) compressed with LZ4/Snappy before shipping over TCP!

Share this article

Comments