In Kafka, a partition can have multiple copies stored across different brokers. These copies are called replicas, and they help Kafka remain available when a broker fails.
However, not all replicas have the same responsibility.
For each partition, Kafka designates one replica as the leader, while the remaining replicas act as followers.
The leader handles incoming writes and determines the order of records. Followers replicate the leader's data so that another broker can take over if the leader becomes unavailable.
Let's understand how these roles work and how Kafka keeps partition replicas synchronized.
Suppose we have a Kafka topic called orders with one partition and a replication factor of three.
Kafka stores three copies of that partition across three brokers.
All three brokers maintain a copy of the partition's log, but only Broker 1 is currently responsible for accepting writes.
When a producer sends a record to this partition, it sends the request to Broker 1.
Broker 1 appends the record to its local log.
Brokers 2 and 3 then fetch the record from Broker 1 and append it to their own logs.
This ensures that the same records are stored across multiple brokers.
Kafka assigns leadership per partition, not per topic or broker. A broker can be the leader of one partition while acting as a follower for another.
For example:
By distributing leadership across brokers, Kafka can spread read and write traffic throughout the cluster.
The partition leader is responsible for managing the authoritative log for its partition.
Its main responsibilities include accepting producer writes, assigning offsets to records, serving fetch requests, and tracking follower replication progress.
Let's follow a simple example.
Suppose a producer sends three order events:
The leader appends them to the partition log in the order they are accepted:
Each record receives a unique offset within that partition.
The leader establishes the ordering of records, and followers reproduce that same ordering.
This is how Kafka preserves message order within a partition, even when that partition has multiple replicas.
The leader also tracks how far the followers have replicated the log. This information helps Kafka determine which records are committed and which replicas are eligible to take over if the leader fails.
Kafka uses leader-follower replication, where followers copy records from the leader.
An important detail is that replication is pull-based.
The leader does not continuously push records to every follower. Instead, each follower sends fetch requests to the leader, asking for records it has not yet replicated.
Suppose the leader's log contains:
But a follower has only replicated records through offset 101.
The follower sends a fetch request starting at offset 102.
The leader responds with the missing records, and the follower appends them to its local log.
The sequence below shows this pull-based exchange.
After replication:
Both replicas now contain the same records in the same order.
Followers repeat this process continuously as new records arrive.
Kafka uses batching and efficient fetch requests to make this process scalable.
Because followers pull data independently, one slow follower does not necessarily prevent other followers from continuing to replicate.
However, a slow follower can affect durability and availability depending on whether it remains part of the in-sync replica set.
Not every follower is necessarily caught up with the leader.
A follower might fall behind because of network delays, heavy disk activity, or a temporary problem with its broker.
Kafka tracks a set of replicas known as the In-Sync Replicas (ISR).
The ISR includes the leader and followers that are sufficiently caught up and actively replicating data.
For example:
Initially, all three replicas are in sync.
Now suppose Broker 3 experiences a network problem and stops making sufficient replication progress.
Kafka may remove it from the ISR:
Broker 3 is still an assigned replica, but it is no longer considered in sync.
Kafka uses settings such as replica.lag.time.max.ms to determine when a follower has fallen too far behind.
If Broker 3 recovers and catches up with the leader, it can rejoin the ISR.
The ISR is important because it determines when records can be committed and which replicas can normally become leaders.
To understand replication, we need to distinguish between records that have been appended and records that have been committed.
Kafka tracks two important positions in a partition log.
The Log End Offset represents the offset where the next record would be appended.
For example, if a replica contains records through offset 104, its LEO is 105.
Different replicas can temporarily have different log end offsets because followers may still be catching up.
The high watermark represents the boundary up to which records have been committed and are available to ordinary consumers.
It advances when records have been replicated to all in-sync replicas and Kafka's minimum ISR requirement is satisfied.
Consider this example:
Assume all three replicas are in the ISR and the minimum ISR requirement is satisfied.
The leader has records through offset 104, but Follower 2 has only replicated through offset 102.
Therefore, the high watermark cannot advance beyond offset 103, the position immediately after the last committed record.
Records at offsets 100 through 102 are committed.
Records at offsets 103 and 104 have not yet been committed.
By default, Kafka consumers can only read committed records up to the high watermark.
This prevents consumers from seeing records that might disappear if an uncommitted write is lost during a leader failure.
Kafka producers can control when a write is considered successful using the acks configuration.
There are three common settings.
acks=0: The producer does not wait for an acknowledgment from the broker. This offers low overhead but provides the weakest delivery assurance.
acks=1: The leader acknowledges the request after appending the records locally. Followers may not have replicated them yet.
acks=all: The leader waits until the records satisfy Kafka's replication commitment requirements before acknowledging the write.
For example, suppose a topic has a replication factor of three, with all three replicas in the ISR.
With acks=all, the leader waits for the records to be replicated to all three in-sync replicas.
The min.insync.replicas setting also matters.
If we configure:
Kafka requires at least two in-sync replicas for successful acks=all writes.
If the ISR falls below two replicas, those writes fail rather than accepting records without the required redundancy.
This combination is widely used in production:
Producer configuration:
These settings provide stronger durability guarantees while allowing the system to tolerate certain broker failures.
However, min.insync.replicas=2 does not mean Kafka always waits for exactly two replicas. It sets the minimum ISR size. With acks=all, Kafka waits for the required replication across the current ISR.
By default, Kafka consumers fetch records from the partition leader.
However, Kafka also supports fetching from follower replicas when the cluster and clients are configured appropriately.
This feature can be useful when brokers are distributed across different racks or availability zones.
For example, suppose a consumer runs in Zone B, but the partition leader is located in Zone A.
If an eligible follower exists in Zone B, the consumer may be directed to read from that follower.
This can reduce cross-zone network traffic and improve data locality.
Kafka still ensures that consumers do not read beyond the committed portion of the partition log.
Follower fetching is an optimization rather than a change to Kafka's replication model.
The leader remains responsible for coordinating writes and replication.
Suppose Broker 1 is the leader of orders-0, while Brokers 2 and 3 are followers.
Suddenly, Broker 1 crashes.
The partition can no longer accept writes through its previous leader.
Kafka's KRaft controller detects the broker failure and coordinates the election of a replacement leader.
Under normal conditions, the controller selects an eligible replica from the ISR.
For example:
The cluster metadata is updated to reflect the leadership change.
Producers and consumers refresh their metadata and redirect requests to Broker 2.
There may be a short interruption while Kafka detects the failure and completes the leadership transition.
With healthy replication, this process generally allows the partition to recover without losing committed records.
Modern Kafka also supports Eligible Leader Replicas (ELR), which can allow certain replicas outside the current ISR to become leaders when Kafka can establish that doing so is safe. ELR is enabled by default on new clusters starting with Kafka 4.1.
If no safe leader is available, the partition may remain unavailable until an appropriate replica recovers.
Allowing an out-of-sync replica to become leader through an unclean election can restore availability. The new leader may be missing committed records, and those records are then lost.
Suppose Broker 1 restarts after Broker 2 has become the new leader.
Broker 1 does not automatically reclaim leadership.
Instead, it rejoins the cluster and follows the current leader for that partition.
It may need to synchronize its log with Broker 2.
For example, Broker 1 might contain records that were appended before the failure but never committed.
Meanwhile, Broker 2 might have accepted new records after becoming leader.
Kafka uses leader epochs and log recovery mechanisms to identify the valid log history.
If Broker 1 contains records that do not belong to the new leader's authoritative history, those records may need to be truncated.
Broker 1 then fetches the missing records from Broker 2.
Once it catches up sufficiently, it can rejoin the ISR.
The flow below summarizes how Broker 1 returns as a follower.
Kafka may later transfer leadership back to Broker 1 through preferred leader election, but this is a separate operation.
This process ensures that replicas converge on a consistent partition log after failures.
You can inspect partition leadership using Kafka's command-line tools.
For example:
You might see output similar to:
Here:
Leader: 1 means Broker 1 is the current leader.Replicas: 1,2,3 identifies all assigned replicas.Isr: 1,2,3 identifies the replicas currently in sync.If Broker 3 falls behind, the ISR might change to:
This is one of the simplest ways to understand replication health.
Monitor under-replicated partitions, offline partitions, and ISR changes. Frequent ISR changes can indicate network instability, overloaded brokers, or storage performance problems.
Kafka uses a leader-follower replication model for each partition.
The leader accepts writes, assigns offsets, and coordinates replication. Followers continuously fetch records from the leader to maintain copies of the partition log.
The ISR tracks replicas that are sufficiently caught up, while the high watermark determines which records are committed and visible to consumers.
If a leader fails, Kafka can elect another eligible replica, allowing the partition to continue operating.
The most important distinction is that the leader controls the order of writes, while followers provide redundancy and the ability to recover from failures.
10 quizzes