Schema Registry and Avro Contract Evolution
Enforce event contracts with a schema registry and evolve payloads using compatibility rules.
Why Event Contracts Need a Registry
In an event-driven FastAPI backend, your service publishes events to Kafka or Pulsar and many independent consumers read them. The event payload is a contract: producers and consumers must agree on field names, types, and structure.
- If the producer renames
user_idtouserId, every consumer breaks silently. - Plain JSON has no enforced shape, so a typo ships straight to production.
A Schema Registry stores versioned schemas centrally and rejects messages that violate the agreed contract, decoupling teams while keeping data safe.
Avro: A Compact, Schema-First Format
Apache Avro is the most common serialization format used with schema registries. Each record is described by a JSON schema, and the binary payload itself carries no field names, only values, making it compact.
- The schema defines
name,type,fields, and optionaldefaultvalues. - Readers need the schema to decode the bytes, which is exactly why the registry exists.
Below is a minimal Avro schema for an OrderCreated event.
order_created_schema = {
"type": "record",
"name": "OrderCreated",
"namespace": "com.shop.events",
"fields": [
{"name": "order_id", "type": "string"},
{"name": "user_id", "type": "string"},
{"name": "amount_cents", "type": "long"},
{"name": "currency", "type": "string"},
],
}
print(order_created_schema["name"], "has", len(order_created_schema["fields"]), "fields")All lessons in this course
- Producing and Consuming Kafka Events Asynchronously
- Schema Registry and Avro Contract Evolution
- The Transactional Outbox Pattern
- Idempotent Consumers and Exactly-Once Semantics