---
title: "Kafka KIP-392: consumers fetch from the closest replica"
description: "With replica.selector.class and client.rack a Kafka consumer reads from the follower in its own zone, a fetch round behind the leader, else from the leader."
url: https://monedula.dev/kafka-simulator/scenarios/fetch-from-closest-replica/
---

# Kafka KIP-392: consumers fetch from the closest replica

Scenario 02.1.5 · Fetch from the closest replica. Consumer groups · Advanced · about 3 min · Apache Kafka 4.4.

KIP-392 (Kafka 2.4+): with replica.selector.class set to the rack-aware selector, a consumer with client.rack reads from the in-sync follower in its own zone, sees new records one follower fetch later than a leader reader, and goes back to the leader when that follower is gone.

Run it: https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5

## What you start with

By default every consumer reads from the partition leader, so a consumer in another availability zone pays for cross-zone traffic on every fetch. KIP-392 (Kafka 2.4) lets it read from a follower instead. Here every broker runs replica.selector.class=RackAwareReplicaSelector. The leader of orders-0, broker-1, is in az-a; its followers are broker-2 in az-b and broker-3 in az-c. The groups g-near and, later, g-new run in az-b (client.rack=az-b); g-leader sets no client.rack, so it always reads from the leader. Watch which broker each read comes from, the high watermark each follower has learned, and what happens when broker-2 goes down.

## Step by step

1. **Three 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](https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5&t=0)
2. **The 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](https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5&t=1)
3. **Read 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](https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5&t=2)
4. **OFFSET_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](https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5&t=3)
5. **The 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](https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5&t=4)
6. **Back to the leader** (t+3.600s): broker-2 went down at 3000 ms and left the ISR. With no in-sync replica in az-b, the leader names itself as the preferred read replica, so g-new and g-near read O3 from broker-1 in az-a: the read crosses zones again, but the consumers keep going. [Open at this step](https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5&t=5)
7. **broker-2 is back in sync** (t+4.900s): broker-2 restarted at 4500 ms. Its catch-up fetch (4700 ms) brought it O3 and high watermark 3, and it rejoined the ISR at 4800 ms, so it is the preferred read replica for az-b again. [Open at this step](https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5&t=6)
8. **Follower reads again** (t+5.600s): O4 committed at 5205 ms. g-leader read it from the leader at 5300 ms; g-new and g-near read it from broker-2 at 5410 and 5500 ms, after broker-2 learned high watermark 4 at 5405 ms. [Open at this step](https://monedula.dev/kafka-simulator/playground/?scenario=02.1.5&t=7)

## The takeaway

The consumer sends client.rack with each fetch and the leader names a preferred read replica: an in-sync replica in that rack, else itself. Reading from a follower in your own zone removes the cross-zone consumer hop, at a small cost in freshness: a follower learns the high watermark only from its next fetch, so a record becomes readable there one fetch round after it is readable on the leader. A consumer whose offset is ahead of what the follower knows gets OFFSET_NOT_AVAILABLE and retries. When the follower dies or leaves the ISR, the consumer reads from the leader again, and returns to the follower once it is back in sync. Replication itself still crosses zones.

## Configs in this scenario

- [`replica.selector.class`](https://kafka.apache.org/44/configuration/broker-configs/#brokerconfigs_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`](https://kafka.apache.org/44/configuration/consumer-configs/#consumerconfigs_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.
- [`auto.offset.reset`](https://kafka.apache.org/44/configuration/consumer-configs/#consumerconfigs_auto.offset.reset): Where a consumer starts when its group has no committed offset or the offset no longer exists: latest (the default), earliest, by_duration (a time back from now, such as by_duration:PT1H), or none to throw an exception instead. [How auto.offset.reset works](https://monedula.dev/kafka/configs/auto-offset-reset/)

## Further reading

- [KIP-392 on the Apache Kafka wiki](https://cwiki.apache.org/confluence/display/KAFKA/KIP-392%3A+Allow+consumers+to+fetch+from+closest+replica)

## Kafka proposals behind this scenario

- [KIP-392 explained: fetch from the closest replica](https://monedula.dev/kafka/kips/kip-392/)

## Troubleshooting

- [OFFSET_NOT_AVAILABLE in Kafka: follower fetch and elections](https://monedula.dev/kafka/troubleshooting/offset-not-available/)
