Scenario 02.1.5 · Fetch from the closest replica
Kafka KIP-392: consumers fetch from the closest replica
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 this scenario in the playgroundThree 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 eachThe 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 firstRead 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-bOFFSET_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_AVAILABLEThe 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 upBack 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: step 6, Back to the leaderbroker-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: step 7, broker-2 is back in syncFollower 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: step 8, Follower reads againWhat 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
- 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: step 1, Three zones, one replica each
- 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: step 2, The leader reader goes first
- 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: step 3, Read from the follower in az-b
- 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: step 4, OFFSET_NOT_AVAILABLE
- 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: step 5, The follower catches up
- 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: step 6, Back to the leader
- 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: step 7, broker-2 is back in sync
- 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: step 8, Follower reads again
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.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 -
auto.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 worksoffset. reset
Embed this scenario
Paste this into a blog post, a course page or your team's docs. The scenario plays inside your page, and the link under it points readers to this walkthrough.
<iframe src="https://monedula.dev/kafka-simulator/embed/?scenario=02.1.5" title="Kafka Simulator: Fetch from the closest replica" width="100%" height="520" style="border:1px solid #d0d0d0;border-radius:8px" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
<p><a href="https://monedula.dev/kafka-simulator/scenarios/fetch-from-closest-replica/">Kafka KIP-392: consumers fetch from the closest replica</a></p> Further reading
KIP-392 on the Apache Kafka wiki
Kafka proposals behind this scenario
Troubleshooting
More in Consumer groups
- Kafka consumer poll loop: subscribe, poll, fetch
- Watch Kafka auto.offset.reset in a live simulation
- Kafka committed offsets: enable.auto.commit and restart
- Replay a Kafka topic: consumed records stay in the log
- Kafka consumer group: how partitions split across members
- Kafka consumer group rebalance when a consumer joins
- Kafka partition assignors: range vs round-robin vs sticky
- Kafka eager vs cooperative rebalancing (cooperative-sticky)
- Kafka consumer crash and session.timeout.ms
- Watch Kafka max.poll.interval.ms in a live simulation
- Kafka static membership: group.instance.id avoids rebalances
- Kafka consumer lag: why it grows and how it drains
- Evicted Kafka consumer: how a survivor takes its partition