On this page
Kafka troubleshooting
OffsetNotAvailableException (OFFSET_NOT_AVAILABLE)
org.
A broker answered with OFFSET_NOT_AVAILABLE (error code 78) because it does not yet know a high watermark that covers the offset asked for. With follower fetching (KIP-392) that is a follower a fetch round behind the leader; right after a leader election it is a new leader whose high watermark has not caught up. Both are short, and the client retries.
Reproduce it in the playground3 steps beforeThree zones, one replica each. t+0.000s
orders-0 is led by broker-1 in az-a; broker-2 (az-b) and broker-3 (az-c) are its followers. Every broker runs replica.selector.class=RackAwareReplicaSelector. g-near runs in az-b (client.rack=az-b); g-leader has no client.rack.
Open at this step: step 1, Three zones, one replica each2 steps beforeThe leader reader goes first. t+0.850s
O1 reached both followers at 705 ms, which moved the leader's high watermark to 1, and g-leader read it from broker-1 at 800 ms. broker-2 holds O1 too, but it still knows high watermark 0: a follower learns the high watermark only from its next fetch. So g-near, which reads from broker-2, has nothing yet.
Open at this step: step 2, The leader reader goes first1 step beforeRead from the follower in az-b. t+1.050s
At 905 ms the followers' next fetch carried high watermark 1, and at 1000 ms g-near read O1 from broker-2, its preferred read replica: the in-sync replica in az-b. No consumer byte crossed a zone, at the cost of one fetch round (200 ms here) of freshness.
Open at this step: step 3, Read from the follower in az-bThe moment it happensOFFSET_NOT_AVAILABLE. t+1.850s
O2 committed at 1705 ms. At 1710 ms g-new joined in az-b with auto.offset.reset=latest, so it starts at the leader's high watermark, offset 2. Its fetch went to broker-2, which still knew high watermark 1: offset 2 is past what the follower can serve, so broker-2 answered OFFSET_NOT_AVAILABLE. The consumer retries; nothing is lost.
Open at this step: step 4, OFFSET_NOT_AVAILABLERight afterThe follower catches up. t+2.050s
broker-2 learned high watermark 2 at 1905 ms. g-near read O2 from it at 2000 ms, and g-new's fetches now succeed (there is nothing new for it yet).
Open at this step: step 5, The follower catches upWhat it means
Consumers only read records below the high watermark, the offset every in-sync replica has. The leader moves the high watermark as followers fetch, and a follower learns it only from the leader's answer to its next fetch, so a follower's high watermark trails the leader's. KIP-392 (Kafka 2.4) lets a consumer with client.rack read from an in-sync follower in its own rack when the brokers set replica.selector.class, for example to org.apache.kafka.common.replica.RackAwareReplicaSelector. The consumer's position may come from the leader: auto.offset.reset=latest looks up the leader's end offset, and a consumer may read from the leader before it is pointed at the follower. If that position is above the high watermark the follower knows but within its log, KIP-392 has the follower answer OFFSET_NOT_AVAILABLE rather than serve records it cannot vouch for. The second case is a leader election: until the new leader's high watermark reaches the start offset of its leader epoch, a request for the latest offset (ListOffsets) fails with OFFSET_NOT_AVAILABLE, the case the error's own description names: "The leader high watermark has not caught up from a recent leader election so the offsets cannot be guaranteed to be monotonically increasing." The consumer handles a fetch error like this one by logging "Error in fetch for partition orders-0: org.apache.kafka.common.errors.OffsetNotAvailableException" at DEBUG, requesting a metadata update and clearing its preferred read replica, so its next fetch goes to the leader, which names the follower again. No record is skipped or lost.
Likely causes, most common first
- A follower reader is ahead of what its follower knows. With replica.selector.class and client.rack set, the consumer fetches from a follower. A new consumer starting at latest, or one that read from the leader while its follower was out of the ISR, can hold a position the follower has not learned is committed yet. The gap is about one follower fetch round, so the error appears once or twice and then stops.
- The follower is slow to fetch. A follower that fetches less often, or waits longer at the leader (replica.fetch.wait.max.ms, default 500 ms), learns each new high watermark later. If it stays in the ISR, it stays the preferred read replica, and a consumer that keeps catching up with the leader keeps meeting the gap.
- A leader election just happened. A newly elected leader starts its epoch at its log end offset, and its high watermark can sit below that until the followers fetch from it. A ListOffsets request for the latest offset in that window, from a consumer resetting its position, kafka-get-offsets.sh or an admin client, gets OFFSET_NOT_AVAILABLE until the high watermark catches up.
How to fix it
- Usually nothing: the client retries, and the error stops once the follower or the new leader learns the high watermark. It only needs a look when it repeats for minutes on the same partition.
- Check that the follower serving the rack is healthy and in sync: kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitions lists partitions whose ISR is short, and the follower's fetch metrics show whether it keeps up.
- Check the rack names: a consumer's client.rack must match a broker.rack exactly, or the leader names itself as the preferred read replica and follower fetching never happens.
- For an admin script that lists offsets right after a failover, retry the request with a short backoff instead of failing the job.
What not to do
- Turning follower fetching off because of DEBUG-level OFFSET_NOT_AVAILABLE entries: they are part of how it works, and leader reads cost cross-zone traffic again.
- Treating it like OFFSET_OUT_OF_RANGE: the offset is valid and the data is there, it is just not confirmed committed on that broker yet.
Does the client retry it?
Yes, and the client does it for you. OffsetNotAvailableException is a RetriableException. A consumer that gets it on a fetch logs it at DEBUG, drops its preferred read replica, refreshes metadata and fetches again, first from the leader; an application normally never sees it.
Configs involved
replica.Since Kafka 2.4 (KIP-392), the class a leader uses to pick the replica a consumer reads from (default null, always the leader); org.apache.kafka.common.replica.RackAwareReplicaSelector picks an in-sync replica in the consumer's client.rack, which keeps reads inside one zone.selector. class client.The rack (in the cloud, the availability zone) the client runs in (default empty); with a broker-side replica.selector.class such as RackAwareReplicaSelector a consumer fetches from a follower in its own rack instead of the leader, and since Kafka 4.4 a producer with partitioner.rack.aware=true sends keyless records to partitions whose leader is in its rack.rack replica.How long a follower fetch request waits at the leader for new records (default 500 ms); keep it below replica.lag.time.max.ms so replicas of quiet partitions do not drop out of the ISR.fetch. wait. max. ms
Reproduce it in the simulator
- Kafka KIP-392: consumers fetch from the closest replica (Fetch from the closest replica) Open at the moment it happens: Kafka KIP-392: consumers fetch from the closest replica
Sources
- KIP-392: Allow consumers to fetch from closest replica
- Apache Kafka 4.4.0 source: Errors.java (OFFSET_NOT_AVAILABLE, code 78)
- Apache Kafka 4.4.0 source: OffsetNotAvailableException.java
- Apache Kafka 4.4.0 source: FetchCollector.java (fetch error handling)
- Apache Kafka 4.4.0 source: FetchUtils.java (requestMetadataUpdate clears the preferred read replica)
- Apache Kafka 4.4.0 source: Partition.scala (ListOffsets while the high watermark lags the epoch start)
- Apache Kafka 4.4.0 source: RackAwareReplicaSelector.java
- Apache Kafka 4.4: broker config replica.selector.class
- Apache Kafka 4.4: consumer config client.rack