# 模式注册表

模式注册表作为一个单一的中央服务运行，负责跟踪、管理和检查用于序列化与反序列化流经 [Apache Kafka](https://www.edgepedia.org/zh/apache-kafka) 的数据的模式，从而使事件数据的结构来自一个共享的真相源，而不是由每个生产者和消费者各自定义。<sup>[1](https://www.ibm.com/think/topics/schema-registry)</sup> 生产者按照已注册的模式序列化消息，并为每条消息标注一个模式标识符；消费者则取回该模式来解码有效载荷。随着模式随时间演变，注册表还强制执行兼容性规则，这一过程称为模式演化。<sup>[1](https://www.ibm.com/think/topics/schema-registry)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心功能 | 用于序列化和反序列化的模式中央存储库，在接受新版本之前进行兼容性检查<sup>[1](https://www.ibm.com/think/topics/schema-registry)</sup> |
| 模式格式 | Apache Avro、JSON Schema 和 Protocol Buffers（Protobuf）是常见格式<sup>[1](https://www.ibm.com/think/topics/schema-registry)</sup><sup> • </sup><sup>[2](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)</sup> |
| 传输机制 | 生产者在每条消息中嵌入模式 ID 而非完整模式；消费者按 ID 获取模式以进行反序列化<sup>[1](https://www.ibm.com/think/topics/schema-registry)</sup> |
| 兼容性模式 | 向后、向前和完全，每种都有一个传递变体，会针对每一个先前版本进行检查，而不仅仅是最新版本<sup>[3](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)</sup> |
| 默认模式 | Confluent Schema Registry 中为 BACKWARD，非传递<sup>[3](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)</sup> |
| 参考实现 | Confluent Schema Registry，其他产品仿效其 REST API 和传输格式；它将模式元数据存储于 Kafka 本身<sup>[4](https://www.compilenrun.com/docs/middleware/kafka/kafka-schemas/kafka-schema-registry/)</sup><sup> • </sup><sup>[2](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)</sup> |
| 已知盲区 | 兼容性检查是结构性的；在类型不变的情况下更改单位或含义，可以通过所有模式<sup>[5](https://www.thedataengineering.com/mechanisms/schema-evolution/how-it-works/compatibility)</sup> |

## 为什么 Kafka 需要它

Kafka 本身对其承载数据的结构不施加任何约束，将这一角色留给了模式注册表。<sup>[6](https://www.confluent.co.uk/blog/multiple-event-types-in-the-same-kafka-topic/)</sup> 没有中央注册表，应用程序各自独立管理消息模式，这增加了不兼容变更的风险，导致消费者失败或误解数据。<sup>[1](https://www.ibm.com/think/topics/schema-registry)</sup> 在缺乏注册表的情况下，团队经常依赖不成文的约定——如口头谅解、共同维护但未由程序强制执行的文档，或 wiki 页面——来规定消息的格式；注册表为消息的编码和解码带来了机器验证的一致性。<sup>[7](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/schema-registry/schema-registry-overview)</sup>

**带宽与解耦。** 将模式保持在有效载荷之外，消除了每条消息的开销：事件代理只处理字节，而生产者和消费者针对相同的模式定义进行验证。<sup>[8](https://learn.microsoft.com/en-us/azure/event-hubs/schema-registry-overview)</sup> 在每个有效载荷中传递模式 ID 而非完整的模式定义，提高了网络吞吐效率。<sup>[9](https://learn.microsoft.com/en-us/azure/event-hubs/schema-registry-concepts)</sup> 注册表还服务于治理目的：模式位于中央存储库中，组织可以在那里记录、审查、版本化和审计事件数据结构，并且它们可以支撑生产者和消费者之间的数据契约，定义数据应如何演变的期望。<sup>[1](https://www.ibm.com/think/topics/schema-registry)</sup>

## 核心概念

基于 [Confluent](https://www.edgepedia.org/zh/confluent) 模型构建的注册表用一小套对象词汇来组织其数据：<sup>[2](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)</sup>

- **模式（Schema）**：结构定义本身，以 Avro、Protobuf 或 JSON 表达。每个注册的模式获得一个全局唯一的模式 ID，生产者将该 ID 嵌入每条消息，消费者用它来获取模式。<sup>[2](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)</sup>
- **主体（Subject）**：模式演化的命名作用域。在每个主体内，模式版本按定义的顺序保存，兼容性针对每个主体单独配置；除非选择其他策略，主体以主题命名并带有 `-key` 或 `-value` 后缀（TopicNameStrategy），而 RecordNameStrategy 和 TopicRecordNameStrategy 则从记录中获取主体名称。<sup>[2](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)</sup><sup> • </sup><sup>[6](https://www.confluent.co.uk/blog/multiple-event-types-in-the-same-kafka-topic/)</sup>
- **版本（Version）**：主体序列中的位置。在某个主体下注册相同的模式不会创建新版本；注册兼容但不同的模式会递增版本号。<sup>[2](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)</sup>
- **上下文（Context）**：主体的高层分组或命名空间，允许不同团队或应用程序使用相同的主体名称而不冲突；标识为 `.` 的默认上下文始终存在。<sup>[7](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/schema-registry/schema-registry-overview)</sup>

同一个模式 ID 可以作为一个主体的版本 1 和另一个主体的版本 3 出现，这就是为什么注重精确性的文档会区分模式 ID 和主体版本。<sup>[2](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)</sup>

## 兼容性模式与部署顺序

兼容性是一对模式的属性：写入者模式 W（数据在其下产生）和读取者模式 R（消费代码所期望的）。由于注册表无法知道已部署读取者的完整集合，它将新模式与先前注册的模式进行对比，作为整个读取者群体的代理。<sup>[5](https://www.thedataengineering.com/mechanisms/schema-evolution/how-it-works/compatibility)</sup> 这些模式直接映射到部署顺序：<sup>[5](https://www.thedataengineering.com/mechanisms/schema-evolution/how-it-works/compatibility)</sup><sup> • </sup><sup>[3](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)</sup>

- **向后（Backward）**：新的读取者可以读取旧模式写入的数据，因此读取者可以先部署。用 Confluent 的术语来说，这允许添加可选字段和删除字段。<sup>[3](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)</sup>
- **向前（Forward）**：旧的读取者可以读取新模式写入的数据，因此写入者可以先部署。这允许添加字段和删除可选字段。<sup>[3](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)</sup> 向前兼容性在很大程度上是读取者解析器的属性，而非模式的属性；如果消费者拒绝未知字段，该模式就只是装饰。<sup>[5](https://www.thedataengineering.com/mechanisms/schema-evolution/how-it-works/compatibility)</sup>
- **完全（Full）**：双向兼容，在部署顺序不受控制时需要；在 Confluent 的定义中，它将变更限制为仅添加或删除可选字段。<sup>[3](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)</sup>

**传递性检查。** 普通的兼容性检查仅将新模式与最新版本进行比较。传递模式将其与每一个先前版本进行比较，因为读取者可能落后不止一个版本。众所周知的陷阱出现在完全兼容但非传递的情况下：将 A 升级到 B 可行，B 升级到 C 可行，但 A 到 C 失败，而 A 正是自三月以来未部署任何东西的团队仍在运行的版本。<sup>[5](https://www.thedataengineering.com/mechanisms/schema-evolution/how-it-works/compatibility)</sup> Confluent 的文档给出了相应的例子：在一个主体中有三个模式按 X-2、X-1 和 X 的顺序变更，FULL 兼容性保证使用新模式 X 的消费者可以处理使用 X 或 X-1 的生产者写入的数据，但不一定保证 X-2，而 FULL_TRANSITIVE 将这一保证双向扩展到 X-2。<sup>[3](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)</sup>

配置的兼容性类型还决定了客户端应用程序应升级的顺序：在 BACKWARD 或 BACKWARD_TRANSITIVE 下，无法保证使用旧模式的消费者能读取用新模式产生的数据，因此消费者应先升级。<sup>[3](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)</sup>

## 结构性局限

兼容性检查比较的是形状。从以整数表示的美分变为以整数表示的美元，而字段仍为 `int64`，可以通过所有模式；每个消费者都能干净地解析数据，而每个下游数字都错了 100 倍。同样，新的枚举值可能悄无声息地落入仪表板的 `ELSE` 分支，被计为“其他”。语义变更需要不同种类的工具，例如数据契约，而即便是这些工具也只能部分奏效。<sup>[5](https://www.thedataengineering.com/mechanisms/schema-evolution/how-it-works/compatibility)</sup> 研究工作已提出为注册表扩展广义模式演化功能，利用 LLM 解释模式变更的语义，例如字段重命名或类型变更，而当前注册表对此处理不佳，通常只能通过可能中断服务的重大手动干预来解决。<sup>[10](https://arxiv.org/abs/2406.11227)</sup>

## 实现

Confluent Schema Registry 是最初的也是事实上的标准；其他产品仿效的是它的 REST API 和传输格式。<sup>[4](https://www.compilenrun.com/docs/middleware/kafka/kafka-schemas/kafka-schema-registry/)</sup> 它独立于 Kafka 代理运行，使用 Kafka 作为其持久存储后端和预写变更日志，并分配在租户-上下文组合内唯一的单调递增模式 ID。<sup>[2](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)</sup> 其服务器采用 Confluent Community License 许可，该许可为源码可用，而非 Apache 2.0。<sup>[11](https://www.axonops.com/blog/comparing-kafka-schema-registries/)</sup>

云提供商提供自己的注册表。AWS Glue Schema Registry 是一个无服务器注册表，与 [Amazon MSK](https://www.edgepedia.org/zh/amazon-msk)、Kinesis Data Streams、Managed Service for Apache Flink 和 [AWS Lambda](https://www.edgepedia.org/zh/aws-lambda) 集成，支持 AVRO v1.11.4、[JSON Schema](https://www.edgepedia.org/zh/json-schema)（Draft-04、Draft-06 和 Draft-07），以及 Protobuf proto2 和 proto3，但不支持 `extensions` 或 `groups`。<sup>[12](https://docs.aws.amazon.com/glue/latest/dg/schema-registry.html)</sup> 尽管 Azure Event Hubs Schema Registry 属于 Event Hubs 命名空间，它也可以服务于其他消息或事件代理；它将模式组织为独立管理的模式组，每个组有自己的兼容性模式，并且仅针对 Avro 格式处理模式演化。<sup>[9](https://learn.microsoft.com/en-us/azure/event-hubs/schema-registry-concepts)</sup> Google 的 Managed Service for Apache Kafka 提供具有主体、版本和上下文的模式注册表，目前处于预览阶段。<sup>[7](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/schema-registry/schema-registry-overview)</sup>

**开源替代方案。** 来自 Aiven 的 Karapace 是一个 Apache 2.0 许可的注册表和 REST 代理，实现了 Confluent Schema Registry API 并将模式存储在 Kafka 中。<sup>[11](https://www.axonops.com/blog/comparing-kafka-schema-registries/)</sup> Apicurio Registry 同样采用 Apache 2.0 许可，将模式与 OpenAPI 和 [AsyncAPI](https://www.edgepedia.org/zh/asyncapi) 等 API 工件一起存储，提供原生 API 和 Confluent 兼容 API，并推荐使用 [PostgreSQL](https://www.edgepedia.org/zh/postgresql) 作为生产存储。<sup>[11](https://www.axonops.com/blog/comparing-kafka-schema-registries/)</sup> 在一项比较排名中，Apicurio Registry（82/100）和 Confluent Schema Registry（81/100）得分几乎持平，Karapace 在可移植性方面最强，为 92/100。<sup>[13](https://faun.dev/toolbox/compare-schema-registries/)</sup>

## References

1. [What is a Schema Registry? IBM](https://www.ibm.com/think/topics/schema-registry)
2. [Schema Registry Concepts for Confluent Cloud, Confluent Documentation](https://docs.confluent.io/cloud/current/sr/fundamentals/index.html)
3. [Schema Evolution and Compatibility for Schema Registry on Confluent Platform, Confluent Documentation](https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html)
4. [Schema Registry: Why and How, Compile N Run](https://www.compilenrun.com/docs/middleware/kafka/kafka-schemas/kafka-schema-registry/)
5. [Compatibility modes from deployment order, The Data Engineering](https://www.thedataengineering.com/mechanisms/schema-evolution/how-it-works/compatibility)
6. [Multiple Event Types in the Same Kafka Topic, Revisited, Confluent Blog](https://www.confluent.co.uk/blog/multiple-event-types-in-the-same-kafka-topic/)
7. [Schema registry overview, Google Cloud Managed Service for Apache Kafka](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/schema-registry/schema-registry-overview)
8. [Azure Schema Registry in Event Hubs, Microsoft Learn](https://learn.microsoft.com/en-us/azure/event-hubs/schema-registry-overview)
9. [Schema Registry in Azure Event Hubs, Microsoft Learn](https://learn.microsoft.com/en-us/azure/event-hubs/schema-registry-concepts)
10. [Compound Schema Registry (Silvery D. Fu and Xuewei Chen, arXiv:2406.11227)](https://arxiv.org/abs/2406.11227)
11. [Open Source Schema Registry Comparison: Which One to Use? AxonOps Blog](https://www.axonops.com/blog/comparing-kafka-schema-registries/)
12. [AWS Glue Schema Registry, AWS Glue documentation](https://docs.aws.amazon.com/glue/latest/dg/schema-registry.html)
13. [Best Schema Registries: ranked and compared, FAUN.dev](https://faun.dev/toolbox/compare-schema-registries/)

---
*Initially written Oct 26, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Data mining, warehousing, and big data › Big data platforms and frameworks

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/schema-registry

English original: https://www.edgepedia.org/schema-registry
