Home / Data Platform / Streaming & CDC

Data PlatformHow-toAdvanced

Kafka follower fetching with client.rack - cut cross-AZ consumer traffic without breaking latency or failover

Configure KIP-392 rack-aware follower fetching so consumers read from a replica in their own zone. Covers broker and client settings, MSK zone IDs, Kubernetes, the latency trade-off and how to verify it works.

Deepak Bhardwaj6 min read

Applies toApache Kafka 2.4 and later brokers (KRaft or ZooKeeper), Java clients 2.4+, librdkafka-based clients 1.5+; Amazon MSK provisioned clusters

The goal

By default every consumer fetches from the partition leader. With three zones and leaders spread evenly, about two thirds of consumer bytes cross a zone boundary. On clouds that bill inter-zone traffic, fan-out consumers (several consumer groups reading the same topic) can cost more than the brokers.

KIP-392 (Kafka 2.4) lets a consumer fetch from an in-sync follower in its own rack. Producers still write to the leader, and replication still crosses zones. Only the consumer read path changes.

Note

Follower fetching is not the same as rack-aware replica placement (broker.rack used when creating topics) or rack-aware partition assignment (KIP-881). You need broker.rack for all three. This post covers the read path. Placement is covered in the partition reassignment and HA posts.

How it works

  1. Each broker has broker.rack set to its zone.
  2. The leader runs a replica.selector.class. The built-in RackAwareReplicaSelector picks an in-sync replica whose rack matches the consumer's client.rack.
  3. The leader returns that replica as the preferred read replica in the fetch response. The consumer then fetches from it until something changes: an error, the replica leaving the ISR, or a metadata refresh.
  4. Followers only serve records up to the high watermark, so read_committed and acks semantics stay the same.

Everything depends on exact string matches. us-east-1a and use1-az1 are different racks.

Step 1: check what the brokers report

# Rack per broker
kafka-broker-api-versions.sh --bootstrap-server $BOOTSTRAP | grep -o 'id: [0-9]* rack: [^)]*'

# Selector and rack on one broker (shows defaults with --all)
kafka-configs.sh --bootstrap-server $BOOTSTRAP --entity-type brokers --entity-name 1 \
  --describe --all | grep -E 'broker.rack|replica.selector.class'

If replica.selector.class is empty, the leader never points consumers anywhere else, whatever the client sends.

Step 2: configure the brokers

# server.properties on each broker
broker.rack=eu-west-1a
replica.selector.class=org.apache.kafka.common.replica.RackAwareReplicaSelector

Treat replica.selector.class as a static setting and apply it with a rolling restart, one broker at a time, waiting for under-replicated partitions to return to zero between brokers.

kafka-topics.sh --bootstrap-server $BOOTSTRAP --describe --under-replicated-partitions

Amazon MSK: MSK sets broker.rack for you to the Availability Zone ID (for example use1-az1), not the zone name. Add replica.selector.class to a custom cluster configuration and apply it. MSK performs the rolling update:

aws kafka create-configuration --name follower-fetch \
  --server-properties fileb://server.properties   # contains replica.selector.class=...
aws kafka update-cluster-configuration --cluster-arn $ARN \
  --configuration-info Arn=$CONFIG_ARN,Revision=1 --current-version $CURRENT_VERSION

Step 3: set client.rack on consumers

Java consumer:

client.rack=eu-west-1a

Python with confluent-kafka (librdkafka):

from confluent_kafka import Consumer

c = Consumer({
    "bootstrap.servers": BOOTSTRAP,
    "group.id": "billing-aggregator",
    "client.rack": zone,          # must equal broker.rack exactly
    "enable.auto.commit": False,
})

The value must come from where the consumer is running, not from a static config file. Look it up at startup.

EC2, using IMDSv2 to get the zone ID (use this for MSK):

TOKEN=$(curl -s -X PUT http://169.254.169.254/latest/api/token -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
  http://169.254.169.254/latest/meta-data/placement/availability-zone-id

Azure VM:

curl -s -H Metadata:true \
  "http://169.254.169.254/metadata/instance/compute/zone?api-version=2021-02-01&format=text"

On Azure the instance metadata returns the zone number (1, 2, 3). Use the same form in broker.rack, and remember that zone numbers are mapped per subscription.

Kubernetes: pods don't see their node's topology.kubernetes.io/zone label directly. Common options:

  • An init container that reads the node object through the API (with RBAC to get nodes) and writes the zone to a shared file the app reads.
  • Strimzi: the rack property on KafkaConnect and KafkaMirrorMaker2 resources sets client.rack from a node label for you.
kubectl get nodes -L topology.kubernetes.io/zone

Step 4: verify it is working

Before and after the change, compare per-broker outbound bytes against what you expect. With follower fetching, BytesOutPerSec moves from being concentrated on leaders to spread across all replicas in proportion to the consumers in each zone.

On Java clients, the consumer fetch manager exposes a per-partition preferred-read-replica metric. A value of -1 means the client is reading from the leader. Check it with JMX or your metrics exporter:

kafka.consumer:type=consumer-fetch-manager-metrics,client-id=*,topic=*,partition=*
  preferred-read-replica

Check cross-zone traffic in the cloud bill or flow logs. On AWS, filter Cost Explorer on usage type DataTransfer-Regional-Bytes. Leave a few days before you compare.

The trade-offs

Effect Why What to do
End-to-end latency goes up slightly A follower only learns the new high watermark on its next fetch from the leader, so records become readable on the follower a little later Keep latency-critical consumers (sub-second SLAs) on the leader by leaving client.rack unset
Uneven broker load All consumers in one large zone hit the replicas in that zone Balance consumers across zones, and size brokers for consumer egress, not just ingress
Slow failback after a replica recovers The consumer keeps its current choice until an error or metadata refresh metadata.max.age.ms (default 5 minutes) controls how often the client re-learns the preferred replica
No benefit when a zone holds no replica of a partition The selector falls back to the leader Check rack-aware placement: every partition should have a replica in each zone where it is consumed
A follower drops out of the ISR The selector only picks in-sync replicas The consumer gets an error and returns to the leader automatically. Fix the slow follower

Warning

Rolling out replica.selector.class restarts every broker. Clusters running with min.insync.replicas equal to the replication factor will reject acks=all writes while each broker is down. Check those settings before the rolling restart.

Combine it with rack-aware assignment

From Kafka 3.5 (KIP-881), the built-in range and cooperative-sticky assignors can also use client.rack to give each consumer the partitions that have a replica in its zone. Follower fetching already sends reads to a local replica, and rack-aware assignment helps when replication factor is lower than the number of zones. Check the assignor behaviour for your client version. The new consumer group protocol (KIP-848) moves assignment to the broker.

Prevent it next time

  • Put client.rack discovery into your shared consumer library or base image, so new services get it without having to remember.
  • Alert when the share of fetches with preferred-read-replica = -1 grows, for example after a client upgrade or a new deployment platform that doesn't set the zone.
  • Add a check to your topic-creation pipeline that every partition has a replica in each consuming zone.
  • Re-check broker.rack values after broker replacements. A blank or mistyped rack silently sends all consumers back to the leader.