Kafka producer logs NotLeaderOrFollowerException after a broker change
Your error
org.apache.kafka.common.errors.NotLeaderOrFollowerException: For requests intended only for the leader, this error indicates that the broker is not the current leader. For requests intended for any replica, this error indicates that the broker is not a replica of the topic partition.2 more lines it can showFix it fast
Most common cause, 1 of 3Leadership moved during a broker restart, crash or rebalance
No code change is needed, because the producer asks for fresh metadata and resends.
# server.properties on every broker
controlled.shutdown.enable=true
auto.leader.rebalance.enable=trueShort answer
NOT_LEADER_OR_FOLLOWER (error code 6) means a request reached a broker that no longer leads that partition, usually because leadership moved during a broker restart, crash or rebalance. Kafka marks it retriable: the producer refreshes metadata and resends by itself. Your code only sees it when retries is 0 or a broker stays down. Applies to Apache Kafka 4.4.
Which cause do you have?
Describe the topic and compare Leader, Replicas and Isr per partition. When each partition has a leader and Isr matches Replicas, the cluster is healthy now and the error came from a leadership move (cause 1 or 2). A short Isr list or missing leaders point to cause 3.
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic ordersThen find the line you see in this list and open its fix:
- 1Leadership moved during a broker restart, crash or rebalance
The WARN lines cluster around a broker restart or deploy, stop on their own, and the records still arrive - 2retries=0 hands the transient error to your code
A send callback or future fails with NotLeaderOrFollowerException, and the producer config contains retries=0 - 3A broker is down or behind, so partitions lack a healthy leader
The warnings never stop, and kafka-topics.sh lists partitions under --unavailable-partitions or --under-replicated-partitions
What does the error look like?
org.apache.kafka.common.errors.NotLeaderOrFollowerException: For requests intended only for the leader, this error indicates that the broker is not the current leader. For requests intended for any replica, this error indicates that the broker is not a replica of the topic partition.Got error produce response with correlation id 1234 on topic-partition orders-0, retrying (2147483646 attempts left). Error: NOT_LEADER_OR_FOLLOWERReceived invalid metadata error in produce request on partition orders-0 due to org.apache.kafka.common.errors.NotLeaderOrFollowerException ... Going to request metadata update now
Cause 1 of 3
Leadership moved during a broker restart, crash or rebalance
You seeThe WARN lines cluster around a broker restart or deploy, stop on their own, and the records still arrive
Check
NOT_LEADER_OR_FOLLOWER right after a broker stops or returns means the client still held the old leader in its metadata. Kafka hands a broker's partitions to other replicas when it stops or crashes, and with auto.leader.rebalance.enable=true it later moves leadership back to the preferred replica.
grep -c "Error: NOT_LEADER_OR_FOLLOWER" producer.logFix
No code change is needed, because the producer asks for fresh metadata and resends. For planned restarts, keep controlled shutdown on so the broker migrates its leaders before it stops, which the Kafka docs say cuts the unavailable time to a few milliseconds per partition.
# server.properties on every broker
controlled.shutdown.enable=true
auto.leader.rebalance.enable=trueVerify
The next rolling restart leaves a few WARN lines and no send callback receives an exception.
grep -c "NotLeaderOrFollowerException" app.logConfirmed by kafka.apache.org
Cause 2 of 3
retries=0 hands the transient error to your code
You seeA send callback or future fails with NotLeaderOrFollowerException, and the producer config contains retries=0
Check
Application code only receives NotLeaderOrFollowerException when the producer did not resend. Kafka's producer docs say that retries=0 disables the automatic retry, so transient errors such as a leader change are propagated to the application. The defaults are retries=2147483647 with delivery.timeout.ms=120000.
grep -rnE "retries|delivery.timeout.ms" src/main/resources configFix
Delete the retries override and bound retry time with delivery.timeout.ms, as the Kafka docs advise. Keep acks=all and enable.idempotence=true (both defaults) so the resent batches do not create duplicates.
# producer.properties
acks=all
enable.idempotence=true
delivery.timeout.ms=120000
# no retries= line: the default resends until delivery.timeout.ms runs outVerify
Restarting one broker under load shows WARN lines in the producer log and zero failed sends in the application.
grep -c "Error: NOT_LEADER_OR_FOLLOWER" producer.logConfirmed by kafka.apache.org
Cause 3 of 3
A broker is down or behind, so partitions lack a healthy leader
You seeThe warnings never stop, and kafka-topics.sh lists partitions under --unavailable-partitions or --under-replicated-partitions
Check
An endless run of NOT_LEADER_OR_FOLLOWER points at the cluster rather than the client. kafka-topics.sh can describe only the partitions whose leader is not available, or only the under-replicated ones, which shows the broker that is missing from Isr.
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --unavailable-partitions
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitionsFix
Get the missing broker running again (its server log and disk usually say why it stopped), let it catch up into Isr, then return leadership to the preferred replicas if automatic rebalancing is off.
bin/kafka-leader-election.sh --bootstrap-server localhost:9092 --election-type preferred --all-topic-partitionsVerify
Both describe commands print no partitions, and new producer warnings stop.
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitionsConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Alert on under-replicated partitions so a broker that drops out of Isr is fixed before clients start failing.
- Restart brokers one at a time and wait until --under-replicated-partitions prints nothing before moving to the next one.
- Review producer configs in code review for retries=0; prefer tuning delivery.timeout.ms.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro.