Kafka
Leader Replica
Highwater Mark
Replication
Follower Replicas

How Kafka leader replica decides to advance Highwater Mark HW when replicating data to follower replicas

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Apache Kafka is a distributed event streaming platform used for high-throughput messaging. It operates with a cluster of brokers, each responsible for storing streams of records in topics partitioned for scalability. Within each partition, Kafka maintains multiple replicas for fault tolerance, with one replica acting as the leader and others as followers. An essential mechanism in Kafka's replication process is the Highwater Mark (HW), which indicates the offset position of the latest committed message that all in-sync replicas have acknowledged. Let's delve into how a Kafka leader replica decides to advance the HW during the data replication process.

Kafka Replication and the Role of the Highwater Mark

To understand how Kafka decides to advance the HW, it's crucial first to grasp the basic replication model. Each partition has a leader replica responsible for managing read and write requests. Other replicas follow the leader for data consistency and redundancy.

The Importance of Highwater Mark

The HW is vital in Kafka as it signifies the offset up to which all the in-sync replicas (i.e., those that replicate data consistently from the leader) have acknowledged receiving data. Data at or below the HW is considered "committed," hence available for consumers to read. This mechanism ensures strong consistency and reliability in message delivery.

Mechanism for Advancing the Highwater Mark

Leader Election and Initial State

  1. Controller Election: When a new leader is elected, all replicas are reset to a safe state, ensuring consistency across the board.
  2. Initial HW Assignment: The newly elected leader initializes its HW to the highest offset that all in-sync replicas have acknowledged during the election moment.

The Process of Advancing HW

  1. Write Operation on Leader: When a producer sends records to Kafka, they are first written to the leader replica's log sequentially.
  2. Data Propagation to Followers: These records are then asynchronously propagated to the follower replicas.
  3. Acknowledgment from Followers: Followers must persist these records and send acknowledgment back to the leader.

Advancing the HW

The HW advancement occurs under the following conditions:

  • In-Sync Replicas (ISR): The offset up to which all replicas in the ISR acknowledge the receipt of data is used to determine the HW. The leader compares the latest offsets acknowledged by each follower in the ISR.
  • Quorum-based Commit: If a majority or configured minimum number of replicas (as defined in min.insync.replicas) in the ISR have acknowledged a record, the leader advances the HW up to the lowest acknowledged offset among these replicas.

Example Scenario

Suppose a partition is replicated across three brokers with offsets represented as follows:

BrokerOffsets Acked
Broker 1 (Leader)10
Broker 2 (Follower)10
Broker 3 (Follower)9

In this scenario, the HW would be set to 9, as this is the highest offset received by all in-sync replicas. If, for instance, Broker 3 catches up, acknowledging offset 10, the HW will advance to 10.

Challenges and Error Handling

Split-Brain and Out-of-Sync Repairs

  • Implicit Fencing: Kafka employs a leasing mechanism to reject operations from out-of-sync or fenced replicas.
  • Recovery Strategies: Misaligned replicas are reassigned until they can catch up with the ISR, at which point the leader re-evaluates HW advancement.

Configuration and Tuning

Several configurations influence HW behavior:

  • replication.factor: Determines the number of replicas.
  • min.insync.replicas: Controls the number of replicas that need acknowledgment to advance the HW, balancing between availability and durability.
  • unclean.leader.election: Decides if an out-of-sync replica can become the leader, potentially impacting HW consistency.

Conclusion

Advancing the Highwater Mark is a critical operation ensuring data consistency and reliability in Kafka's distributed environment. Through a well-orchestrated process involving leader election, ISR management, and acknowledgment handling, Kafka effectively balances performance with strong guarantees for message delivery. By understanding the intricate details of HW management, developers can better configure Kafka clusters to meet specific application needs. The interplay of high concurrency, fault tolerance, and replication techniques makes Kafka a robust platform for modern data-driven applications.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design