Monedula

Markdown

Part VI · Reference

Kafka troubleshooting

OffsetNotAvailableException (OFFSET_NOT_AVAILABLE)

Kafka error · OFFSET_NOT_AVAILABLE · retriable

org.apache.kafka.common.errors.OffsetNotAvailableException

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 playground

The 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_AVAILABLE

What 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

  1. 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.
  2. 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.
  3. 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.selector.class 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.
  • client.rack 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.
  • replica.fetch.wait.max.ms 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.

Reproduce it in the simulator

Sources

All troubleshooting pages