Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

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 show

Fix it fast

Most common cause, 1 of 3

Leadership moved during a broker restart, crash or rebalance

No code change is needed, because the producer asks for fresh metadata and resends.

Fix
# server.properties on every broker
controlled.shutdown.enable=true
auto.leader.rebalance.enable=true
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to Apache Kafka 4.4.0, Apache Kafka 4.3.2, Kafka Java client 4.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic orders

Then find the line you see in this list and open its fix:

  1. 1Leadership moved during a broker restart, crash or rebalanceThe WARN lines cluster around a broker restart or deploy, stop on their own, and the records still arrive
  2. 2retries=0 hands the transient error to your codeA send callback or future fails with NotLeaderOrFollowerException, and the producer config contains retries=0
  3. 3A broker is down or behind, so partitions lack a healthy leaderThe 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_FOLLOWER
  • Received 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.

Check
grep -c "Error: NOT_LEADER_OR_FOLLOWER" producer.log

Fix

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.

Fix
# server.properties on every broker
controlled.shutdown.enable=true
auto.leader.rebalance.enable=true

Verify

The next rolling restart leaves a few WARN lines and no send callback receives an exception.

Verify
grep -c "NotLeaderOrFollowerException" app.log

Confirmed by kafka.apache.org

Did this fix it?

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.

Check
grep -rnE "retries|delivery.timeout.ms" src/main/resources config

Fix

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.

Fix
# producer.properties
acks=all
enable.idempotence=true
delivery.timeout.ms=120000
# no retries= line: the default resends until delivery.timeout.ms runs out

Verify

Restarting one broker under load shows WARN lines in the producer log and zero failed sends in the application.

Verify
grep -c "Error: NOT_LEADER_OR_FOLLOWER" producer.log

Confirmed by kafka.apache.org

Did this fix it?

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.

Check
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --unavailable-partitions
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitions

Fix

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.

Fix
bin/kafka-leader-election.sh --bootstrap-server localhost:9092 --election-type preferred --all-topic-partitions

Verify

Both describe commands print no partitions, and new producer warnings stop.

Verify
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitions

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.