机架感知与多 AZ 部署
学习 Kafka 的机架感知如何将副本分散到不同机架和可用区,从而承受数据中心级别的故障。
机架感知与多 AZ 部署 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Apache Kafka & Stream Processing Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is Rack Awareness?
Rack awareness tells Kafka about the physical (or cloud) topology of your brokers.
Without it, all replicas of a partition could land on brokers sharing the same rack or availability zone. If that zone fails, the partition becomes unavailable.
- Each broker advertises a
broker.rackvalue. - Kafka then places replicas on different racks when possible.
Configuring broker.rack
Set broker.rack in each broker's server.properties to identify its rack or zone.
# server.properties on a broker in us-east-1a
broker.id=1
broker.rack=us-east-1a
# Another broker in a different zone
broker.id=2
broker.rack=us-east-1bHow Replicas Get Spread
When a topic is created, Kafka's replica assignment algorithm alternates across racks.
For a partition with replication factor 3 and 3 racks, each replica lands on a distinct rack — guaranteeing the partition survives the loss of any single rack.
Cloud Availability Zones
In AWS, GCP, or Azure, map broker.rack to the availability zone (AZ).
- AZs are physically isolated within a region.
- Spreading replicas across AZs protects against a full AZ outage.
- Beware cross-AZ network costs for replication traffic.
Creating a Rack-Aware Topic
No special flag is needed — if brokers have broker.rack set, topic creation is automatically rack-aware.
kafka-topics.sh --create \
--bootstrap-server localhost:9092 \
--topic orders \
--partitions 6 \
--replication-factor 3Verifying Replica Placement
Use --describe to inspect which brokers hold each replica, then cross-check against broker racks.
kafka-topics.sh --describe \
--bootstrap-server localhost:9092 \
--topic ordersThe min.insync.replicas Tie-In
Rack awareness protects placement, but durability also depends on min.insync.replicas.
With replicas spread across 3 AZs and min.insync.replicas=2, a producer using acks=all can still write even if one entire AZ is down.
Follower Fetching from Closest Replica
Kafka 2.4+ supports fetch from follower. Consumers can read from a replica in their own rack, cutting cross-AZ traffic and latency.
# broker config
replica.selector.class=org.apache.kafka.common.replica.RackAwareReplicaSelector
# consumer config
client.rack=us-east-1aLimitations to Know
Rack awareness is best-effort:
- If racks < replication factor, some replicas share a rack.
- Reassignments triggered manually do not always respect racks unless you generate a rack-aware plan.
- It does not rebalance existing topics when you add
broker.racklater.
Generating a Rack-Aware Reassignment
To fix existing topics after enabling rack awareness, generate a reassignment plan with the reassign-partitions tool.
kafka-reassign-partitions.sh \
--bootstrap-server localhost:9092 \
--topics-to-move-json-file topics.json \
--broker-list 1,2,3 \
--generateDesign Checklist
Before going to production:
- Replication factor >= number of racks you want to tolerate losing + 1.
- Set
min.insync.replicasto at least 2. - Use
acks=allon critical producers. - Enable follower fetching to control cross-AZ costs.
Quick Check
Test your understanding of rack awareness.
Recap
You learned how rack awareness spreads replicas across racks and availability zones.
- Set
broker.rackper broker. - Topic creation becomes automatically rack-aware.
- Combine with
min.insync.replicasandacks=allfor AZ-failure resilience. - Use follower fetching to reduce cross-AZ cost.
常见问题解答
「机架感知与多 AZ 部署」课时是免费的吗?
是的 — 「机架感知与多 AZ 部署」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
「机架感知与多 AZ 部署」这节课中我会学到什么?
学习 Kafka 的机架感知如何将副本分散到不同机架和可用区,从而承受数据中心级别的故障。 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「机架感知与多 AZ 部署」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?
能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 副本与容错
- 控制器与 ZooKeeper/Kraft 的角色
- 设计 Kafka 集群
- 机架感知与多 AZ 部署