System Design advanced LIVE DEMO
πŸ“¨

Distributed Message Queue (Kafka Deep-Dive)

Explore Kafka as a distributed commit log through a 14-step visualization covering producer batching, partition leaders, ISR, high watermark, consumer offsets, failover, recovery

System Designkafkadistributed-systemsreplicationisrhigh-availabilitymessage-queue
Interactive Demo

Continue with the guided workspace

Use the paired workspace to connect this guide's mental model with live component state, visible signals, and the outcome each step or scenario is meant to prove.

A distributed log-based message queue (in the style of Apache Kafka) stores messages as an append-only commit log partitioned across many brokers. Producers write to the end of a partition, consumers read sequentially by offset, and durability comes from replicating each partition to an In-Sync Replica set, with reads capped at the High Watermark so consumers never see uncommitted writes.

Mental Model

Kafka operates as an append-only distributed commit log rather than a traditional transient message queue. Topics are split into independent ordered partitions distributed across brokers. Producers append messages to the end of these logs, and consumers read sequentially using numeric offsets. Durability is managed by replicating partitions to multiple brokers using an In-Sync Replica (ISR) set, and consumers are protected from uncommitted writes by reading only up to the High Watermark.

Components and Why They Exist

Producer App

This component exists to generate data payloads and send message streams to the brokers. It aggregates messages, runs partitioning hash functions, and schedules batch uploads to the partition leaders. In the demo, observe batch accumulation and message packets traveling to specific broker leaders.

Topic Partitions

This component exists to spread topic storage across servers, facilitating horizontal scalability and parallel consumption. It hosts sequential commit logs where incoming messages are assigned monotonically increasing offsets. In the demo, watch partition segments growing as message offsets increment sequentially.

Broker Leaders (Broker A, B, C)

This component exists to act as single coordination points for client writes and enforce commit boundaries. It appends writes to local disks, streams logs to replicas, manages the local ISR set, and tracks the High Watermark. In the demo, watch partition leaders manage Log End Offsets (LEO) and update status badges like Leader or ISR.

Follower Replicas

This component exists to hold copies of partition logs, ensuring durability and high availability in case broker leaders fail. They request log updates from leader partitions asynchronously and flush logs locally to stay caught up within the ISR window. In the demo, watch follower replica nodes fetching log entries from partition leaders.

KRaft Controller

This component exists to coordinate cluster configuration, monitor broker heartbeats, and elect new leaders quickly. It registers broker updates, handles leader elections on node crashes, and updates routing metadata directories. In the demo, watch the controller node trigger failover events and promote new partition leaders.

Consumer Group

This component exists to balance partition workloads across multiple parallel consumer instances dynamically. It coordinates group membership, assigns partitions to members, polls messages up to the High Watermark, and logs offsets. In the demo, watch partition rebalance pauses and consumer offset markers advancing.

Edge Cases and Misconceptions

  • Message loss with acks=1: If a producer writes to the leader broker with acks=1, it gets a success response as soon as the leader appends the log. If the leader crashes before followers replicate that message, the message is permanently lost. Set acks=all (with min.insync.replicas > 1) to guarantee durability.
  • The rebalance storm problem: If a consumer member misses heartbeats (e.g., due to long garbage collection pauses), the coordinator assumes it is dead and triggers a rebalance. During rebalance, all partition consumption pauses, which can cascade into consumer lag buildup and system-wide delays.
  • Compaction is not deletion: Log compaction retains only the latest record for each key. It runs asynchronously in the background, meaning stale keys may temporarily coexist in segments until clean sweeps complete.

Frequently Asked Questions

When is a Kafka record actually safe to read?

A record becomes safe for ordinary consumers when the partition leader advances its High Watermark past that offset. With acks=all, the leader waits for the required in-sync replicas to append the record before it reports success and can advance committed progress. Consumers configured with isolation.level=read_committed add transaction visibility rules on top of that replication boundary.

Why can an acknowledged acks=all write still be unavailable during an outage?

Acknowledgment proves the write met the configured ISR threshold at that moment; it does not guarantee that every partition remains available after a broker loss. If the remaining ISR is smaller than min.insync.replicas, Kafka rejects further durable writes rather than weakening the guarantee. Capacity and availability can fall while already committed records remain safe.

How do partitions affect ordering and scaling?

Kafka orders records within a partition, not across a whole topic. A stable message key sends related records to the same partition and preserves their order, while more partitions let producers and consumer-group members work in parallel. Increasing partition count later improves parallelism but does not create a single global ordering and may change key-to-partition placement.

What should operators watch during replica recovery?

Watch replica lag, ISR membership, under-replicated partitions, and controller events together. A restarted broker can serve only after it has fetched the missing log; it should not rejoin ISR merely because its process is healthy. Keeping min.insync.replicas, replication factor, and alert thresholds aligned prevents recovery pressure from being mistaken for restored durability.

Common Questions

What is the purpose of the In-Sync Replicas (ISR) list?

The ISR list contains all partition replica brokers that are actively heartbeating and fully caught up with the leader broker (within the lag bounds). When acks=all is set, a message is only committed once it is acknowledged by all brokers in the ISR, guaranteeing durability.

How does the leader broker failover process work in Kafka?

If the leader broker crashes, the cluster controller detects the failure and elects a new leader from the In-Sync Replicas (ISR) list. The client producers and consumers then fetch updated metadata and reconnect to the newly elected leader automatically.

What is the difference between LEO and High Watermark (HW)?

The Log End Offset (LEO) is the offset of the next message to be written to the local log of a broker. The High Watermark (HW) is the maximum offset replicated by all ISR members. Consumers can only read messages up to the High Watermark.

Why does consumer group rebalancing pause consumption?

During a rebalance, the consumer coordinator reassigns partitions among active consumer group members. To avoid duplicate consumption and partition conflicts, the group must temporarily pause polling until assignments are finalized.

What is log compaction, and how does it differ from deletion policies?

Log compaction ensures that the queue retains at least the last known value for each message key within a single partition log. Unlike size- or time-based deletion policies that discard entire old segment files, compaction sweeps through inactive segments, keeping only the latest offset for each key to reclaim storage space.

What happens when the ISR list size drops below `min.insync.replicas`?

If min.insync.replicas is set to 2, and 2 out of 3 brokers in the ISR list crash, producers attempting writes with acks=all will receive a NotEnoughReplicasException, blocking new messages until at least one replica catches up and rejoins the ISR.

Why do consumers only read up to the High Watermark (HW)?

If consumers could read up to the Log End Offset (LEO), they might fetch messages that have been written to the leader but not yet replicated by followers. If the leader then crashes, those unreplicated messages are truncated and lost, resulting in the consumer having read 'ghost' data (dirty reads).

How does Kafka locate a requested offset in segment files?

Kafka first finds the segment whose offset range contains the request. It binary searches that segment's sparse offset index for the nearest indexed offset at or below the target, jumps to the recorded byte position in the `.log` file, then scans sequentially until it reaches the requested record. This keeps the index small while bounding the scan.