Contracts & the catalog
Versioned Avro and Protobuf contracts in a shared contracts module and Apicurio registry, and why a discovery catalog is a mesh requirement even though it isn't built yet.
Learning objectives
By the end of this chapter you can:
- Explain why
contractsis the single source of truth for both Avro event schemas and the one Protobuf service definition, and how each gets generated into Java code. - Distinguish a runtime contract (Avro, enforced live against Apicurio) from a discovery contract (Protobuf, compiled ahead of time), and state what each does and doesn’t guarantee.
- Verify an Avro contract is enforced at runtime by reading raw Kafka bytes for the wire-format magic byte and confirming a matching Apicurio artifact.
- Explain why a discovery catalog is a mesh requirement in principle, and what’s missing in this project (OpenMetadata ingestion) to close that gap.
Why this matters
payment-service and shipping-service both react when order-service
publishes order.placed. Neither one can see inside order-service’s
database; all either gets is the event on the wire. If that event’s shape
changes without warning, both consumers break on the next order that goes
through, and nobody finds out until something downstream fails.
That’s the problem a contract solves: a shape both sides agree to ahead of
time, one that won’t shift under them without notice. This chapter covers
the two mechanisms this project uses for that. A shared contracts module
holds every service’s Avro and Protobuf definitions as the single source of
truth. Apicurio Schema Registry enforces the Avro half of that at runtime,
on the wire, not just at compile time. The chapter closes with what isn’t
built yet, a discovery catalog, and why that gap still matters even with no
demo to show for it.
The code lives in contracts. There’s no standalone demo for the registry itself: demo-kafka.sh exercises it while proving the Kafka data plane, and this chapter’s cross-check comes from there.
Figure 4.1 shows the chapter’s scope. Every protocol surface in the mesh carries its own contract:
| Protocol | Contract type |
|---|---|
| REST | OpenAPI document |
| gRPC | .proto file |
| GraphQL | SDL schema |
| Kafka | Avro schema |
Apicurio Registry is the one home for all of them regardless of format, and
OpenMetadata sits downstream of it, turning those contracts plus the running
Postgres schemas and Kafka topics into a lineage graph. That target picture
is wider than what this chapter’s code wires up today. The sections below
separate running enforcement from the conceptual target, starting with the
two contract types contracts owns.
One module, two contract types
contracts is a plain Maven module: framework-agnostic, with no Quarkus
dependency at all, so any JVM service (or build) can depend on it. It holds
two kinds of contract, generated by two different plugins, for two
different protocols:
examples/contracts/
src/main/avro/
order-placed.avsc # datamesh.order.v1.OrderPlaced
payment-captured.avsc # datamesh.payment.v1.PaymentCaptured
shipment-dispatched.avsc # datamesh.shipping.v1.ShipmentDispatched
src/main/proto/
datamesh/inventory/v1/inventory.proto
The Avro schemas are the event contracts. Here’s order-placed.avsc in full:
{
"type": "record",
"name": "OrderPlaced",
"namespace": "datamesh.order.v1",
"doc": "Emitted by order-service after an order is durably persisted.",
"fields": [
{ "name": "event_type", "type": "string" },
{ "name": "order_id", "type": "string" },
{ "name": "customer_id", "type": "string" },
{ "name": "item_sku", "type": "string" },
{ "name": "quantity", "type": "int" },
{ "name": "amount", "type": "string" },
{ "name": "status", "type": "string" },
{ "name": "created_at", "type": "string" }
]
}
name + namespace together give every record a fully qualified identity
(datamesh.order.v1.OrderPlaced) that doubles as its Apicurio artifact
identity and its generated Java package, so all three stay in lockstep by
construction. amount is a string, not Avro’s decimal logical type: the schema’s
doc field calls this “a first-slice simplification” and names the decimal logical type
as the later refinement. Anyone extending this contract should treat amount
as a known shortcut, not a pattern to copy.
The org.apache.avro:avro-maven-plugin’s schema goal, bound to Maven’s
generate-sources phase, turns each .avsc into a generated
SpecificRecord Java class under target/generated-sources/avro. That’s
the concrete OrderPlaced type OrderEventProducer builds with
OrderPlaced.newBuilder() in the previous chapter. Nobody hand-writes the
record: the .avsc is the one source of truth, and the Java class is just a
build artifact of it.
The Protobuf side is a single .proto for the one synchronous cross-service
call in this project:
syntax = "proto3";
package datamesh.inventory.v1;
service InventoryService {
rpc CheckStock(CheckStockRequest) returns (CheckStockResponse);
}
message CheckStockRequest {
string sku = 1;
int32 quantity = 2;
}
message CheckStockResponse {
bool available = 1;
int32 quantity_on_hand = 2;
}
The contracts module does not generate gRPC stubs itself. It packages
inventory.proto as a plain resource so it ships inside the jar.
Each consuming service (order-service
as a client, inventory-service as a server) adds the quarkus-grpc
extension and points quarkus.generate-code.grpc.scan-for-proto at this
module’s Maven coordinates:
quarkus.generate-code.grpc.scan-for-proto=com.patterncatalyst.datamesh:contracts
That property is what makes the InventoryService Mutiny interface and the
CheckStockRequest/CheckStockResponse builders from the previous chapter
appear under each service’s own target/generated-sources/grpc at build
time. Each module generates its own copy fresh from the one proto
contracts owns; nothing gets copy-pasted between services.
Two jobs the registry does: runtime vs. discovery
These two contract types are enforced differently once a service is running.
A runtime contract sits on the hot path. OrderEventProducer’s
emitter.send(event) is the example here: it only works because Apicurio’s
Avro serializer and deserializer agree on the schema, fetched from the
registry at publish and consume time. Get the schema wrong, or leave it
missing, and the event won’t encode or decode at all. That’s exactly why
Apicurio can enforce governance on it: a compatibility rule configured on
the artifact can reject a breaking schema change at publish time, before it
reaches a single consumer.
A discovery contract is different. The .proto here gets compiled
ahead of time into generated stubs; it’s never fetched at request time, so
nothing fails at runtime if the registry doesn’t have a copy of it. It still
deserves a single, versioned home, so a consumer can always find the exact
shape a gRPC service expects, but the coupling is loose and offline instead
of tight and online.
Figure 4.2 shows the distinction as two numbered paths through the same
registry. The runtime path is the four steps this project runs: ①
OrderEventProducer serializes OrderPlaced and registers (or fetches) its
schema against Apicurio, ② the Avro bytes plus a 4-byte schema id land on the
order-placed Kafka topic, ③ OrderPlacedConsumer in notification-service
fetches that same schema by id, and ④ it deserializes the bytes back into an
OrderPlaced record before persisting a Notification. That’s the same
round trip the previous chapter’s OrderEventProducer/OrderPlacedConsumer
pair walks through in code.
The discovery path (⑤–⑧) goes beyond what is wired up. It shows all four
services publishing contracts (OpenAPI, Protobuf, GraphQL SDL, and Avro
again) into the same registry, with OpenMetadata ingesting them alongside
the Postgres schemas and Kafka topics to build a lineage graph. Of those
four, only Protobuf is a checked-in discovery contract in this project
(inventory.proto, packaged into the contracts jar as covered above).
order-service and inventory-service don’t add the
quarkus-smallrye-openapi extension, so there’s no /openapi.json document
generated today for REST to publish anywhere. And while graphql-gateway’s
quarkus-smallrye-graphql extension does derive a GraphQL SDL schema from
GatewayApi’s annotations (what the diagram draws as schema.graphql),
nothing in this repository registers it anywhere or ingests it into a
catalog. Figure 4.2 marks ⑤–⑧ as the target shape, not code that runs
today.
Conflating the two is how teams get surprised in both directions. Treat the
Avro schema as documentation and you’ll miss that a bad change breaks every
consumer immediately. Treat the proto as if it were enforced like the Avro
schema and you’ll overstate what compiling against a checked-in .proto
actually guarantees at runtime.
What’s registered, and how to see it
application.properties in each producing/consuming service configures the
Apicurio Avro serde explicitly (see the previous chapter’s note on why
value.serializer is pinned rather than autodetected), pointing at the
compose stack’s Apicurio instance. demo-kafka.sh is where this
project shows the registry doing work. It places an order (triggering
OrderEventProducer.publish), reads the raw bytes back off the Kafka
topic with kcat, bypassing any Avro deserializer, and asserts the first
byte of the record value is 0x00, the Apicurio/Confluent wire-format magic
byte that precedes a 4-byte schema id and the Avro-encoded payload. It also
asserts the value does not start with 0x7b ({), which would mean the
serializer had silently regressed to plain JSON.
Then it confirms Apicurio’s own artifact count went up after the publish,
and that a matching artifact exists under either the topic name or the
record’s fully qualified name datamesh.order.v1.OrderPlaced. That confirms
from the registry’s side that a schema was registered for this run, not just
that bytes arrived.
Raw-byte wire-format proof plus registry-side confirmation is cheap evidence
that the contract is enforced, not merely declared in a .avsc file and
ignored at runtime.
Why a catalog is a requirement, not an add-on (and why it isn’t built yet)
A registry tells you the shape of one contract at a time. It doesn’t tell you which products exist across the whole mesh, who owns each one, who consumes whom, or where a given piece of data ultimately came from. That’s a catalog’s job. It ingests from the registry, the databases, and the event backbone to assemble a discoverable picture with lineage, the producer-and-consumer graph across domains, so a change’s downstream blast radius is something you can look up instead of reverse-engineering it from code.
Figure 4.3 repeats Figure 4.1 with each contract marked by its job: a filled dot for the one runtime contract (Avro, enforced live against Apicurio) and hollow dots for the three discovery contracts (OpenAPI, Protobuf, GraphQL SDL). That marking is also why the OpenMetadata box sits across a gap: everything to its left is running code or a checked-in artifact in this repository, and everything inside it is not.
This project doesn’t build that catalog yet. There’s no OpenMetadata
deployment, no ingestion job, and no demo-discovery.sh or
demo-om-lineage.sh in this repository today. The sibling Python reference
architecture layers OpenMetadata on top of the same kind of registry this
chapter describes. The OpenMetadata ingestion and lineage work is planned
but not yet built here, so this section stays conceptual and cites no
catalog demo.
It belongs in this chapter because of the argument underneath it: a mesh’s premise is that domains own their data independently and other domains consume it without a central team brokering access. That premise only holds if products are discoverable and their contracts are trustworthy. This project has a registry, so contracts don’t drift and consumers don’t break silently. It doesn’t yet have a catalog, so nobody can find products or see lineage across the whole mesh, and in practice teams fall back to asking around, which reintroduces exactly the bottleneck a mesh exists to remove.
Discovery infrastructure is a precondition for federated governance to be computational: checked at publish time and recorded automatically, rather than enforced through team coordination. This project demonstrates the registry half of that claim in running code; the catalog half is future work.
Build, run, observe
cd demos && ./demo-kafka.sh
The next chapter’s data-planes discussion returns to this demo for the event backbone.
Here, the two assertion steps that concern contracts are the raw first-byte check (0x00, not {)
right after the order is placed, and the Apicurio artifact-count delta and
artifact-id match that follow it. Together they show that this project’s Avro
contracts hold at runtime.
What you learned
contractsis the single source of truth for both contract types in this project: Avro.avscfor events, compiled ahead of time into generatedSpecificRecords, and a.protofor the one gRPC call, scanned out of the module’s jar by each consuming service’s own build.- Runtime contracts (Avro, fetched/enforced live against Apicurio) and discovery contracts (the proto, compiled ahead of time) carry different coupling. Conflating them leads to surprises about what a contract actually guarantees.
- A catalog is a mesh requirement in principle. Without one, discovery degrades back into a central team being asked where things are, but this project hasn’t built one yet.
The next question is how data moves across the mesh on top of them: the synchronous read layer and the asynchronous event backbone.
For deeper coverage
- Data Mesh by Zhamak Dehghani: federated computational governance and discoverability as mesh requirements.
- Designing Data-Intensive Applications, 2nd ed. by Martin Kleppmann & Chris Riccomini: schema evolution and the compatibility guarantees a registry enforces.
- Apicurio Registry documentation: artifact rules, compatibility levels, and the Avro serde this chapter’s wire-format check exercises.
- Protocol Buffers Language Guide (proto3):
the
.protosyntaxinventory.protouses. - Cloud Native Anti-Patterns by Gerald Bachlmayr et al.: the risks of treating a schema as documentation instead of an enforced contract, a trap this chapter calls out directly.