Blog

Nearest Replica Fetch Planning for MSK Consumer Fleets

A Kafka consumer fleet can turn an ordinary Amazon MSK deployment into a network-cost problem. Writes get most of the early sizing attention because producers define ingest rate, replication factor, and broker storage pressure. Then the platform goes live, more teams subscribe to the same topics, and the read side starts moving the same bytes repeatedly across Availability Zone, VPC, or regional boundaries. The surprise is not that consumers cost money. The surprise is that consumer placement can be as important as broker size.

Nearest replica fetch is one of the first controls Kafka teams should evaluate when that pattern appears. In Apache Kafka, consumers normally fetch from the partition leader. KIP-392 introduced a way for consumers to fetch from the closest replica when the cluster and client are configured for rack awareness. In Amazon MSK, AWS recommends setting client.rack to the Availability Zone ID so consumers can use nearest replica reads to reduce network traffic cost and latency.

That sounds like a small configuration topic, but it is really a planning exercise. The setting only helps when replicas, clients, topology, and workload behavior line up. A consumer in use1-az1 needs a useful replica in the same failure domain. A cost model needs to separate producer writes, broker replication, consumer reads, private connectivity, and cross-region replication instead of hiding every byte behind "Kafka traffic."

Nearest replica fetch planning map

Why consumer traffic becomes the expensive part

Kafka's replication model is easy to reason about on the write path. A topic with a replication factor of 3 stores multiple copies of each partition for availability, and production teams understand that durability has a cost. The read path feels different because adding a consumer group does not change the topic configuration. It feels like a lightweight subscription until the same topic is read by fraud detection, search indexing, observability, data lake ingestion, machine learning features, and back-office exports.

Each consumer group reads its own copy of the records. If those consumers run in different Availability Zones from the partition leaders they read from, the read path can create cross-AZ data movement even though the data already has replicas in several zones. This is exactly where nearest replica fetch earns attention: the goal is not to change Kafka semantics, but to route reads toward a replica that is topologically closer to the consumer when Kafka can do so safely.

For MSK buyers searching for traffic costs Amazon MSK, the pricing page is only the starting point. AWS documents service-specific MSK pricing dimensions and points users back to standard AWS data transfer pricing for traffic that moves in and out of clusters. The planning gap is the mapping between an application topology and those meters.

The mechanism: rack-aware follower fetching

Nearest replica fetch depends on two pieces of metadata. Brokers need rack information so Kafka understands where replicas live. Consumers need a matching rack identifier so the broker can select a preferred read replica for fetch requests. With the standard Kafka selector, the relevant broker-side setting is replica.selector.class=org.apache.kafka.common.replica.RackAwareReplicaSelector, and the relevant consumer-side setting is client.rack.

In AWS, the safest unit is usually the Availability Zone ID, not the public AZ name. AZ names such as us-east-1a can map differently across AWS accounts, while AZ IDs are stable identifiers for the physical zone. AWS's MSK client guidance recommends using the AZ ID for client.rack, which makes the consumer's location comparable with the broker and replica placement.

The design is conservative. If a matching replica is available and current enough, consumers can fetch from that replica. If the matching rack is absent or not usable, Kafka falls back to the leader. That fallback matters because nearest replica fetch is a cost and latency optimization, not a license to weaken availability.

Planning itemWhat to verifyWhy it matters
Broker rack metadataBrokers are distributed across the intended Availability Zone IDsThe selector cannot choose a local replica that does not exist
Consumer client.rackEach workload sets the AZ ID where it actually runsWrong metadata can preserve cross-AZ reads or skew broker load
Kafka version and MSK configurationThe cluster supports and enables the selectorFollower fetch is not automatic for every deployment
Replica freshness and fallbackConsumers behave correctly when the local replica is unavailableCost optimization must not become a reliability risk
ObservabilityMetrics and logs can show fetch source, network, lag, and broker loadSavings are hard to prove without before-and-after telemetry

This is why nearest replica fetch should be treated as a fleet rollout, not a one-line client change. The property is simple; the environment around it is not.

Draw the byte paths before tuning clients

The most useful MSK traffic model starts with a diagram, not with a calculator. Pick one representative topic family and draw every place a byte goes after it is produced. That includes producer-to-broker traffic, broker replication, consumer fetches, connector traffic, mirror or replicator traffic, PrivateLink paths, and any cross-region movement. Then annotate each arrow with the workload that creates it and the network boundary it crosses.

That drawing usually exposes three patterns:

  • Leader concentration. Some partitions have leaders in one zone while many consumers run elsewhere. Nearest replica fetch can reduce read-side movement if replicas exist near those consumers.
  • Consumer fan-out. The same byte is read by several independent groups. A small per-GB network charge becomes material when multiplied by group count and retention replay behavior.
  • Topology drift. Client fleets move between EC2, EKS, ECS, Lambda-adjacent workers, or another VPC, while Kafka configuration stays frozen. The original rack assumption stops matching reality.

The discipline is to model reads as first-class traffic. A producer byte may be written once, replicated for durability, and then fetched many times. If the platform team only models ingest, it will optimize the wrong side of the system.

Consumer byte path model

A rollout plan for MSK consumer fleets

Start with discovery. Inventory the consumer groups that account for most read throughput, then map where they run. For Kubernetes workloads, that often means checking node labels and scheduling rules. For EC2 or ECS workloads, it means identifying the instance or task AZ and how that value becomes a Kafka client property. For managed integration services, check whether you can control the underlying consumer configuration before assuming rack awareness is available.

Next, validate the broker side. On MSK Provisioned clusters, custom configurations determine which Kafka broker properties can be set. AWS documents replica.selector.class as an MSK configuration property for enabling closest replica fetch on supported Kafka versions. Treat the current AWS documentation as the source of truth for what is configurable in your chosen deployment model.

Then test with a narrow blast radius. Choose one consumer group with high throughput, stable ownership, and clear telemetry. Set client.rack from the AZ ID, deploy one slice of the fleet, and compare network, broker fetch load, consumer lag, and application latency. The expected result is not "all network cost disappears." The expected result is that eligible fetch traffic shifts toward same-AZ replicas while correctness and latency remain acceptable.

A practical rollout has four gates:

  1. Topology gate. The consumer's rack value is derived from the runtime environment, not copied into a static config file that can drift.
  2. Compatibility gate. Client libraries, Kafka version, and MSK configuration support the intended behavior.
  3. Load gate. Same-AZ follower reads do not overload a subset of brokers or create worse hot spots than leader-only reads.
  4. Recovery gate. Broker maintenance, replica lag, and AZ events still produce acceptable client behavior and failover time.

If any gate fails, pause the rollout. A partial deployment with measured savings is better than a broad rollout that turns a cost project into an incident.

What nearest replica fetch does not solve

Nearest replica fetch optimizes one class of traffic: consumer reads that can be served from a closer replica. It does not eliminate producer traffic, does not remove broker replication, does not make cross-region replication free, and does not reduce the number of consumer groups reading a topic. It also does not replace partition planning. If a small number of partitions carry most of the traffic, the local replicas for those partitions can still become hot.

The bigger limitation is architectural. Traditional Kafka keeps durable log replicas attached to brokers. That model is proven, and MSK makes it much easier to operate than self-managed Kafka. But the cost curve still follows a stateful broker design: retained data, broker capacity, replication placement, consumer fan-out, and network boundaries remain coupled.

That distinction matters in procurement conversations. A team may enable nearest replica fetch and still find that the dominant cost driver is retained data, over-provisioned broker capacity, or multi-AZ data movement across several workloads. At that point, the question changes from "which client property should we set?" to "which Kafka-compatible architecture gives us the right cost curve?"

Where shared-storage Kafka-compatible architecture fits

After the traffic paths are visible, shared-storage Kafka-compatible systems become easier to evaluate. The architectural idea is to keep Kafka APIs and client semantics while moving durable storage away from broker-local disks and into shared object storage. Brokers become closer to stateless compute, storage scales independently, and the system can reason about durability and availability without copying the same log segment through every broker-local path.

AutoMQ fits this category. It is a Kafka-compatible streaming system built around shared object storage, a WAL layer, and stateless brokers. In the context of MSK traffic-cost planning, the relevant point is not a generic product comparison. The relevant point is whether the workload's cost is being driven by broker-local state, cross-AZ reads, retention growth, or capacity held for recovery and peak traffic.

AutoMQ's documentation describes a zero cross-AZ traffic design for clients in multi-AZ deployments, where producers and consumers can stay local to their Availability Zone while shared storage provides durable data access. Nearest replica fetch improves read routing among broker replicas. A shared-storage design changes the storage and traffic assumptions underneath the broker layer.

This does not make MSK the wrong choice. Many teams should use MSK when they want a managed AWS Kafka service, have predictable traffic, and value deep AWS service integration. The point is to compare architectures against the byte-path model rather than against a single unit price. If nearest replica fetch removes the pain, keep the solution simple. If it only trims the edge of a larger cost curve, include a Kafka-compatible shared-storage option in the evaluation.

Architecture evaluation scorecard

Production scorecard for the final decision

The final decision should be written as an engineering memo, not a pricing screenshot. The memo should name the workloads tested, the network boundaries crossed, the client settings used, and the operational risks accepted. It should also explain which costs were reduced by nearest replica fetch and which costs remained unchanged.

Use the same scorecard across MSK tuning and alternative architecture evaluation:

DimensionMSK nearest replica fetch questionArchitecture alternative question
Kafka compatibilityDo existing clients support client.rack and follower fetch behavior?Are client APIs, transactions, ACLs, and ecosystem tools compatible?
Network costWhich consumer reads move from cross-AZ to same-AZ?Which data paths disappear or become local by design?
Broker loadDo follower reads rebalance pressure or add hot spots?Can compute scale independently from retained data?
RecoveryWhat happens during broker maintenance, lag, and AZ events?How does the system recover data and service after compute loss?
OperationsCan teams maintain correct rack metadata across all runtimes?Does the platform reduce or shift operational work?
EvidenceWhich metrics prove the before-and-after result?Which benchmark or proof of concept validates the business case?

The strongest outcome is often incremental. Enable nearest replica fetch for the fleets where it clearly applies. Fix consumer placement and metadata drift. Use the resulting telemetry to decide whether the remaining cost is still large enough to warrant an architecture change.

Closing the loop

Searches for MSK traffic cost usually begin with a bill, but the durable answer is a topology map. Nearest replica fetch gives Kafka teams a practical lever when consumers are reading across zones unnecessarily. It works best when broker placement, replica selection, client metadata, and observability are handled as one plan.

If that plan shows the read path is the main issue, MSK rack-aware consumers may be enough. If the map shows a deeper coupling between broker-local storage, retained data, read fan-out, and multi-AZ traffic, evaluate a Kafka-compatible shared-storage architecture alongside MSK. AutoMQ's architecture and zero cross-AZ traffic documentation are a useful next step for that comparison: Explore AutoMQ for Kafka-compatible traffic-cost planning.

References

FAQ

What is nearest replica fetch in Kafka?

Nearest replica fetch lets a consumer read from a replica that is topologically closer to the client instead of always fetching from the partition leader. It depends on rack-aware broker metadata, a replica selector, and the consumer client.rack setting.

Does nearest replica fetch remove all MSK data transfer costs?

No. It can reduce eligible consumer read traffic that would otherwise cross Availability Zone boundaries, but it does not remove producer traffic, broker replication, private connectivity charges, cross-region movement, or reads by additional consumer groups.

Should MSK consumers use AZ names or AZ IDs for client.rack?

For AWS deployments, use the Availability Zone ID when possible. AZ names can map differently across accounts, while AZ IDs provide a stable physical-zone identifier for matching clients to broker racks.

Is nearest replica fetch safe during broker failures?

It is designed as an optimization with fallback behavior, but safety still depends on normal Kafka production practices: healthy replicas, appropriate replication factor, in-sync replica policy, client failover handling, and testing during maintenance or failure events.

When should AutoMQ be evaluated in this planning process?

Evaluate AutoMQ after mapping the traffic paths. It is most relevant when the remaining cost comes from broker-local state, retained data growth, cross-AZ traffic, or capacity over-provisioning, and when Kafka protocol compatibility remains a hard requirement.

Newsletter

Subscribe for the latest on cloud-native streaming data infrastructure, product launches, technical insights, and efficiency optimizations from the AutoMQ team.

Join developers worldwide who leverage AutoMQ's Apache 2.0 licensed platform to simplify streaming data infra. No spam, just actionable content.

I'm not a robot
reCAPTCHA

Never submit confidential or sensitive data (API keys, passwords, credit card numbers, or personal identification information) through this form.