Part 4 of Learning Kafka
Replication: surviving a dead broker
Module 4 · 5 min read
Replication: surviving a dead broker
TL;DR
- Partitions split data for scale. Replicas copy it to other brokers for safety, with one leader per partition.
- With factor 3,
min.insync.replicas=2andacks=all, you can lose any one broker without losing acknowledged data or stopping writes. - A controller picks leaders (KRaft now, Kafka 4.0 dropped ZooKeeper). The idempotent producer blocks duplicates from retries.
Split for scale, copy for safety
Module 2’s partitions spread the work but add no safety. If the broker holding P1 dies, P1’s notifications die with it. The other partitions hold different data and can’t help. So Kafka also copies every partition to other brokers, and the number of copies is the replication factor.
How I remember it, partitions hold different data, for scale. Replicas hold the same data, for safety. Replicas of one partition always sit on different brokers, since two copies on one machine die together.
One leader per partition
For each partition one replica is the leader and the rest are followers. Producers write to the leader, consumers read from it by default, followers copy from it. What I kept getting wrong is that there’s no “leader broker”. With three brokers, three partitions and factor 3, Kafka spreads the leaders so every broker serves a share of traffic.
| Partition | Leader | Followers |
|---|---|---|
| payments · P0 | Broker 1 | Broker 2, Broker 3 |
| payments · P1 | Broker 2 | Broker 1, Broker 3 |
| payments · P2 | Broker 3 | Broker 1, Broker 2 |
ISR, the replicas you’d trust to take over
Followers copy by pulling, like consumers (“everything after offset 940”), so an overloaded or cut-off follower falls behind. The ISR (in-sync replicas) is the set that’s fully caught up, leader included. A follower behind for replica.lag.time.max.ms (30 s by default) is dropped, and let back in once it catches up.
Only an ISR member can become leader, since anyone else is missing data. The ISR also decides when a write is safe, through two settings.
acks(producer) is when the leader says “got it”.acks=0never waits. Fastest, and you never know if it worked.acks=1waits for the leader. If it dies before a follower copies the write, it’s gone.acks=allwaits for every ISR member.
min.insync.replicas(topic) makesacks=allrefuse writes when the ISR is smaller than this. Otherwise “all of the ISR” could quietly shrink to “just the leader”.
Try to lose a notification the producer was told was safe.
With acks=1 the leader acknowledges before anyone has a copy, so a badly timed crash loses “safe” notifications. With acks=all an acknowledged write is on every in-sync replica, and only those can lead, so it survives. With two brokers down and min.insync.replicas=2, writes are refused. Kafka would rather stop than keep data on one machine.
For anything that matters I use this.
replication.factor = 3 min.insync.replicas = 2 acks = all (the producer default since Kafka 3.0)
Writes need two of three copies, so any one broker can die without losing data or stopping writes. Factor 2 is awkward. With min.insync.replicas=2 any failure blocks writes, with 1 you trust one machine. Four costs more disk for little extra safety.
Consumers only read up to the high watermark, the point every in-sync replica has copied, so the SMS sender never sends something a failover could erase a second later.
If no in-sync replica is left, the partition stays offline until one returns. unclean.leader.election.enable (off by default) lets a stale replica take over instead, back sooner but minus what it missed. Availability against consistency, chosen per topic. For OTPs I’d consider it, a stale OTP topic beats a dead one. For payment events, never.
Who decides who leads?
The controller places replicas, picks leaders, notices dead brokers and runs failover. It also keeps the cluster metadata, really a routing table.
brokers: 1 → 10.0.0.11:9092 2 → 10.0.0.12:9092 3 → 10.0.0.13:9092 payments-0: leader 1, replicas [1, 2, 3], ISR [1, 2, 3] payments-1: leader 2, replicas [2, 3, 1], ISR [2, 3]
This used to live in ZooKeeper, a separate system. Newer Kafka keeps it in a small Raft-replicated log run by controller nodes (KRaft), and Kafka 4.0 dropped ZooKeeper completely. One less system to run and get paged about.
The full write path
- The producer asks any broker for cluster metadata.
- It caches it, refreshing only on an error like “I’m not the leader any more”, or every few minutes.
- It hashes the key and sends straight to that partition’s leader.
- The leader appends and assigns the next offset.
- Followers fetch and copy.
- The leader acknowledges per
acks.
If step 6’s acknowledgement is lost, the producer retries and the event lands twice. Two SMS. The idempotent producer (default since 3.0) gives each producer an ID and per-partition sequence numbers, and the leader drops sequence numbers it already has. Module 3’s notification-ID check, inside Kafka.
Quick check
With replication.factor=3, min.insync.replicas=2 and acks=all, two of the three brokers go down. What happens to new writes?
The ISR has one member, below min.insync.replicas, so the leader refuses with NOT_ENOUGH_REPLICAS. The producer buffers and retries for a while.
Q&A
What happens to clients when a leader dies?
The controller promotes an in-sync replica. Producers and consumers get an error, refresh their view of the cluster and carry on with the new leader. The partition is down briefly, usually seconds.
Can consumers read from followers?
Not by default. Newer Kafka versions let a consumer read from a nearby follower (say, in the same data centre) to save network cost. Writes still go to the leader.