On this page
Kafka troubleshooting
NotLeaderOrFollowerException (NOT_LEADER_OR_FOLLOWER)
org.
The broker that got the request is not the leader of that partition (for a produce) or not one of its replicas (for a fetch that any replica may serve). The client's metadata is out of date, usually because leadership just moved: a reassignment, a preferred leader election, a controlled shutdown or a leader change after a failure. The client refreshes its metadata and retries, so a short burst of these warnings is normal.
Reproduce it in the playground2 steps beforeTwo partitions, two owners. t+1.100s
orders is a KafScale topic (KafScale 2.1) with two partitions, one replica each, every byte in S3. p1's first two records (keys A and D) made broker-2 take orders-0's lease in etcd and broker-3 take orders-1's (the simulator's hash picks them). Both were acked at 790 ms and consumer c1 of g1 read them at 1000 ms. p1 is set to retries=30 and retry.backoff.ms=500: about 15 s of retries with the simulator's fixed backoff.
Open at this step: step 1, Two partitions, two owners1 step beforeOwner of orders-0 crashes: lease orphaned. t+1.500s
broker-2 was killed without warning at 1500 ms, with nothing in flight: both records had been acked. Its lease on orders-0 is orphaned: it stays in etcd until KafScale's lease TTL of 10 s runs out, at 11500 ms, and the lease lane counts it down. Until then no other broker may write orders-0, and in KafScale no other broker may serve its reads either. The metadata still names broker-2, the lease holder, as the partition's leader. broker-3 still holds orders-1.
Open at this step: step 2, Owner of orders-0 crashes: lease orphanedThe moment it happensorders-0 refuses writes and reads. t+3.100s
p1 sent order-3 (orders-0) and order-4 (orders-1) at 2000 ms. order-4 went to broker-3, its owner, was acked at 2690 ms and read by c1 at 3000 ms. order-3 reached a broker that does not hold the lease and was refused with NOT_LEADER_OR_FOLLOWER, a retriable error, at 2050, 2550 and 3050 ms. c1's fetches of orders-0 at 2000 and 3000 ms got the same error: in KafScale a fetch needs the lease, as a produce does.
Open at this step: step 3, orders-0 refuses writes and readsRight afterLease expires, the next request takes over. t+11.600s
From 2000 to 11050 ms orders-0 refused 19 produce attempts and 10 fetches. At 11500 ms, 10 s after the kill, the orphaned lease expired. The first request for orders-0 after that was p1's retry at 11550 ms, before c1's next fetch at 12000 ms. It went to broker-1 (the simulator's hash over the alive brokers), which took the lease and restored orders-0: etcd's end offset is 1 and S3's is 1, so numbering goes on from 1. broker-1 gave order-3 offset 1 and is uploading it.
Open at this step: step 4, Lease expires, the next request takes overWhat it means
Apache Kafka's protocol describes error code 6 in two halves: for requests intended only for the leader, the broker is not the current leader; for requests intended for any replica, the broker is not a replica of the topic partition. The NotLeaderOrFollowerException javadoc adds that it can be a transient exception during leader elections and reassignments. A producer sends each batch to the broker its metadata names as the partition leader. When leadership has moved to another broker, the broker that is no longer leader answers NOT_LEADER_OR_FOLLOWER; a broker that is down answers nothing, and the client sees a connection error instead. The producer logs "Received invalid metadata error in produce request", asks for new metadata and retries, so a short burst of these warnings during a rolling restart is normal and nothing is lost. A partition that has no leader at all is different: its metadata reports LEADER_NOT_AVAILABLE, the producer keeps its batches and keeps asking for metadata instead of sending them, and they fail with a TimeoutException if no leader appears within delivery.timeout.ms. KafScale, a Kafka-protocol broker that keeps topics in S3, uses the error for its own ownership model: one broker owns each partition through an etcd lease, and any other broker that receives a produce or a fetch for that partition answers NOT_LEADER_OR_FOLLOWER (KafScale's code at v2.1.0). When the owner crashes, its lease stays in etcd until the lease TTL (10 seconds by default) expires, so on KafScale writes and reads of its partitions are refused and retried until another broker takes the lease. A graceful KafScale shutdown releases the leases at once. In KafScale a fetch needs the partition lease as a produce does. While a crashed owner's lease lasts, consumers get NOT_LEADER_OR_FOLLOWER and no records too, so reads of its partitions stop until the lease expires. A graceful shutdown still releases the leases first, but a write still in flight then fails its lease-guarded etcd write with NOT_LEADER_OR_FOLLOWER. Scenario 08.2.1 shows the reads stopping after a crash.
Likely causes, most common first
- Leadership moved and the client has not noticed yet. A reassignment changed the replicas, the preferred leader election moved leadership back to the preferred replica, a controlled shutdown moved a broker's leaders away before it stopped, or the controller elected a new leader after a failure. Until its next metadata refresh the client keeps sending to the broker it knew as leader; if that broker is still up and no longer leads the partition, it answers NOT_LEADER_OR_FOLLOWER.
- A restarted broker came back as a follower. A broker that led partitions is restarted and rejoins as a follower while other brokers lead them. A client that still has the old metadata sends to it once it is reachable again and gets NOT_LEADER_OR_FOLLOWER until it refreshes. While the broker was down the client got connection errors, not this error.
- A fetch went to a broker that is not a replica. A consumer that follows a preferred read replica, or uses stale metadata, can send a fetch to a broker that no longer hosts the partition. That broker answers NOT_LEADER_OR_FOLLOWER and the consumer refreshes its metadata.
- KafScale: another broker holds the partition lease. On KafScale (not Apache Kafka) a produce that reaches a broker other than the lease owner is refused with NOT_LEADER_OR_FOLLOWER. After an owner crash the lease is held by the dead broker until its TTL runs out, so every retry is refused for up to 10 seconds by default; the next produce after expiry lets a live broker take the lease.
- KafScale: a fetch or a late write without the lease. On a KafScale cluster, a fetch that reaches a broker other than the lease owner is refused like a produce, with no records, so after an owner crash its consumers stall for up to the 10 second lease TTL. A produce in flight when its broker stops gracefully is refused at its etcd write, because the broker released its leases first; if its segment reached S3 before the next owner loaded the partition, the next owner makes those records readable, and the producer's retry writes them a second time. KafScale's maintainers confirmed this is intended: delivery is at-least-once.
How to fix it
- Check whether leadership was moving at the time: broker restarts, controller logs about leader elections, reassignments in progress, or offline partitions. A short burst of these warnings during a restart needs no fix.
- If one partition keeps failing for longer than an election takes, check whether it has a leader at all: a partition with no leader shows LEADER_NOT_AVAILABLE in its metadata and producer TimeoutExceptions, which point to offline partitions rather than to this error.
- Keep retries at its default and set delivery.timeout.ms long enough to cover a leader election or, on KafScale, the lease TTL of a crashed owner, so the producer outlasts the move instead of failing the send.
- On KafScale, stop brokers gracefully where possible: a graceful shutdown releases the partition leases at once, while a crash leaves them held until the TTL expires.
- On KafScale, expect consumers to pause too after an owner crash, for up to the lease TTL, and expect a duplicate after a graceful stop unless producers are idempotent: KafScale 2.1.0 drops an idempotent producer’s retried copy, 2.0 has no idempotent producer.
What not to do
- Lowering retries or delivery.timeout.ms to fail fast: a leader election or an expiring lease then turns a transient, retried error into failed sends.
Does the client retry it?
Yes, the client retries. NOT_LEADER_OR_FOLLOWER (error code 6) is retriable, and NotLeaderOrFollowerException extends InvalidMetadataException: the producer requests a metadata update and retries the batch after a backoff that starts at retry.backoff.ms and grows exponentially up to retry.backoff.max.ms (1 second by default), or at once when the response already names the new leader (KIP-951). It reaches the application only when retries or delivery.timeout.ms run out first.
Configs involved
retriesHow many times the producer sends a batch again after a transient error (default 2147483647, so in practice delivery.timeout.ms ends the retrying); idempotence needs it above 0.delivery.The upper bound on reporting success or failure after send() returns, covering the wait in a batch, the wait for the acknowledgement and every retry (default 120000, 2 minutes); it should be at least request.timeout.ms plus linger.ms.timeout. ms
Reproduce it in the simulator
- KafScale 2.1 diskless Kafka: a lease owner crashes (KafScale: a lease owner crashes) Open at the moment it happens: KafScale 2.1 diskless Kafka: a lease owner crashes
- AutoMQ diskless Kafka: a broker crash and WAL failover (AutoMQ: a broker crashes and its WAL is replayed) Open at the moment it happens: AutoMQ diskless Kafka: a broker crash and WAL failover
- AutoMQ diskless Kafka: moving a partition, no copying (AutoMQ: moving a partition without copying) Open at the moment it happens: AutoMQ diskless Kafka: moving a partition, no copying
- Redpanda Kafka leader crash: a Raft election, no replay (Redpanda: the leader crashes, an election, nothing to replay) Open at the moment it happens: Redpanda Kafka leader crash: a Raft election, no replay
Sources
- Apache Kafka 4.4.0 source: NotLeaderOrFollowerException.java
- Apache Kafka 4.4.0 source: Errors.java (NOT_LEADER_OR_FOLLOWER, code 6)
- Apache Kafka 4.4.0 source: Sender.java (invalid metadata error, metadata update, retry)
- Apache Kafka 4.4.0 source: RecordAccumulator.java (exponential retry backoff, no backoff when the leader changed)
- KIP-951: Leader discovery optimisations for the client
- Apache Kafka 4.4: producer config retry.backoff.max.ms
- Apache Kafka 4.4: producer config delivery.timeout.ms
- KafScale 2.1.0 source: lease_manager.go (10 s TTL, release on graceful shutdown, expiry after a crash)
- KafScale 2.1.0 source: broker main.go (a broker without the lease answers NOT_LEADER_OR_FOLLOWER)
- KafScale v2.1.0: broker main.go (a fetch to a broker without the lease)
- KafScale v2.1.0: broker main.go (shutdown releases the leases, then drains)
- KafScale PR #327: fence partition logs across lease changes