Skip to content

Kafka / streaming · 7 of 9

Step 1 · The pattern

A leader and its followers

Every partition has a leader that takes writes and followers that copy them. If the leader dies, a follower that is caught up takes over.

  • message
  • being copied to follower
  • confirmed

In short

ISR / acks: how it works and how it breaks

Each Kafka partition has one leader broker and some followers that copy its log. The followers that are fully caught up form the in-sync replica set, or ISR. When a producer sends a message it chooses how much confirmation to wait for. With acks=1 the leader replies as soon as it has written the message itself. That is fast, but for a moment the leader holds the only copy. If it fails in that moment, a follower is promoted without the message, and a write the producer was told had succeeded is gone. With acks=all the leader replies only after every in-sync replica has the message, so any broker that can take over already has it. Pair it with min.insync.replicas=2 so that 'all' can never quietly mean 'just the leader', and keep unclean leader election off. The price is a little latency per write.

In Spring Boot

A producer that waits for the in-sync replicas

application.properties
spring.kafka.producer.acks=all
spring.kafka.producer.properties[enable.idempotence]=true

The producer waits until every in-sync replica has the record, and retries cannot write it twice. Both are already the Kafka client's defaults. Setting them explicitly matters because idempotence is switched off silently if another setting conflicts with it, and with it set explicitly the producer fails at start-up instead.

Gotcha: acks=all waits for the replicas that are in sync right now, which can be the leader alone. The floor is min.insync.replicas, and that is set on the topic or the broker, not in Spring: with three replicas, 2 is the usual choice. Idempotence only covers the producer's own retries, not your code sending the same event again.

How this comes up in interviews

"A producer received a success response for a message, yet the message is gone after a broker failure. How can that happen in Kafka?"

Then they ask

  1. You set acks=all and still lost data. Which other setting did you most likely leave at one?
  2. What do you trade away by requiring at least two in-sync replicas before accepting a write?
  3. What is unclean leader election and why would you keep it off?

A strong answer covers

  • Each partition has a leader and followers. Followers that are fully caught up form the in-sync replica set.
  • With acks=1 the leader confirms once it has the message itself, so for a moment it holds the only copy.
  • With acks=all the leader confirms only after every in-sync replica has the message.
  • Pair it with min.insync.replicas=2 so that 'all' can never quietly mean only the leader.
  • The price is a little latency per write, and refused writes when too few replicas are in sync.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last updated: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.