模式演进与兼容模式
学习 Confluent Schema Registry 如何在模式变更时强制执行兼容性,以及如何安全地选择向后、向前或完全兼容。
模式演进与兼容模式 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Apache Kafka & Stream Processing Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Schemas Evolve
Data contracts change over time — you add fields, deprecate others, rename things. Schema evolution is the discipline of changing schemas without breaking existing producers and consumers.
The Compatibility Problem
Producers and consumers often deploy at different times. A new producer might emit a v2 schema while old consumers still expect v1.
Without rules, that mismatch causes deserialization failures in production.
Backward Compatibility
Backward: new schema can read data written with the old schema.
- Safe changes: delete a field, add a field with a default.
- Upgrade consumers first.
Forward Compatibility
Forward: old schema can read data written with the new schema.
- Safe changes: add a field, delete a field that had a default.
- Upgrade producers first.
Full Compatibility
Full requires both backward and forward compatibility at once.
- Only changes safe in both directions are allowed (add/remove fields with defaults).
- Producers and consumers can be upgraded in any order.
Transitive Variants
Each mode has a transitive version (BACKWARD_TRANSITIVE, etc.).
Non-transitive checks only against the latest schema; transitive checks against all prior versions — stronger guarantees, more constraints.
Setting Compatibility
Compatibility is configured per subject (or globally) via the Schema Registry REST API.
curl -X PUT \
http://localhost:8081/config/orders-value \
-H 'Content-Type: application/vnd.schemaregistry.v1+json' \
-d '{"compatibility": "BACKWARD"}'A Backward-Safe Avro Change
Adding a field with a default keeps old data readable under BACKWARD mode.
{
"type": "record",
"name": "Order",
"fields": [
{"name": "id", "type": "string"},
{"name": "amount", "type": "double"},
{"name": "currency", "type": "string", "default": "USD"}
]
}Testing Before You Register
The registry can check a candidate schema against the current one before you commit it.
curl -X POST \
http://localhost:8081/compatibility/subjects/orders-value/versions/latest \
-H 'Content-Type: application/vnd.schemaregistry.v1+json' \
-d '{"schema": "...escaped avro..."}'Breaking Changes
Changes that almost always break compatibility:
- Renaming a field (treated as delete + add).
- Changing a field's type incompatibly.
- Adding a required field with no default.
For these, create a new subject/topic version instead.
Choosing a Mode
Rules of thumb:
- BACKWARD (default) when you upgrade consumers first.
- FORWARD when producers lead.
- FULL for independently deployed teams.
- Use transitive variants for long-lived, replayable topics.
Quick Check
Test your understanding of compatibility modes.
Recap
You learned schema evolution and compatibility modes.
- BACKWARD: new reads old, upgrade consumers first.
- FORWARD: old reads new, upgrade producers first.
- FULL: both, any order.
- Transitive variants check all prior versions; renames and required no-default fields break compatibility.
用 AI 导师学习 Apache Kafka & Stream Processing Fundamentals — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「模式演进与兼容模式」课时是免费的吗?
是的 — 「模式演进与兼容模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
「模式演进与兼容模式」这节课中我会学到什么?
学习 Confluent Schema Registry 如何在模式变更时强制执行兼容性,以及如何安全地选择向后、向前或完全兼容。 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「模式演进与兼容模式」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?
能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。