Kafka Exception NotLeaderOrFollowerException
Broker returns this error if a request could not be processed because the broker is not the leader or follower for a topic partition. This could be a transient exception during leader elections and reassignments. For `Produce` and other requests which are intended only for the leader, this exception indicates that the broker is not the current leader. For consumer `Fetch` requests which may be satisfied by a leader or follower, this exception indicates that the broker is not a replica of the topic partition.
Common Causes
- Metadata is stale on the client — it cached the old leader and is sending requests to a broker that is no longer the leader after a failover
- Partition reassignment moved the partition to different brokers but the client has not refreshed its metadata cache yet
- Consumer fetching with 'replica.selector.class' (rack-aware fetch) sending to a follower that lost its assignment
Solutions
- Lower 'metadata.max.age.ms' (e.g. 60000ms) on producers/consumers so metadata refreshes more frequently after leader changes
- After a broker rolling restart or partition reassignment, force client reconnection or wait for the default metadata refresh cycle to complete
- Check that all brokers in the cluster are healthy and reachable; a broker stuck in recovery can cause repeated leader-not-available / not-leader cycles
Example Stack Trace
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.Diagnostic Commands
# Check for under-replicated partitions
kafka-topics.sh --describe --bootstrap-server localhost:9092 --under-replicated-partitions
# Verify broker connectivity and API versions
kafka-broker-api-versions.sh --bootstrap-server <broker>:9092 2>&1 | head -5Related
Protocol error: NOT_LEADER_OR_FOLLOWER (code 6)
Related Broker exceptions: CorruptRecordException · FencedLeaderEpochException · LeaderNotAvailableException · NetworkException · NotEnoughReplicasAfterAppendException · NotEnoughReplicasException · TimeoutException · UnknownLeaderEpochException
NotLeaderOrFollowerException in production? Conduktor Console gives you real-time visibility into clients, consumer groups, and broker health. Browse every Kafka exception or protocol error code.