Video in progress

When you use the observer pattern at the system level (event-driven architecture), how do you evolve the event schema without breaking consumers?

An event is a public contract, and you don't fully know who is listening. So you make only backward-compatible changes (a new optional field), the schema registry checks compatibility in CI, and a breaking change ships as a new event type or version.

📖 Answer

With an in-process observer, changing a listener is a single refactoring. At the system level, 7 services listen to the OrderPlaced event, you don't know the teams behind three of them, and they don't deploy on the same day as you. In Kafka, old messages are retained too: a new consumer reads even week-old messages.

Practical rules:

Compatible changes: adding a new optional field with a default value; a consumer ignoring a field it doesn't understand (tolerant reader).

Breaking changes: removing a field, changing its name or type, changing its meaning (amount is now in whole currency units instead of minor units). The last one is the most dangerous, because schema checking doesn't catch it.

Schema registry. With Avro or Protobuf, use a Confluent/Apicurio registry and BACKWARD or FULL compatibility on the topic. The producer's CI checks the schema against the registry, and the build fails on a breaking change. Even with JSON you can do the same with JSON Schema.

When you must break it: a new event type (OrderPlacedV2) or a new topic. For a while the producer emits both, and the old one is deleted once the consumers have moved. You can tell who is still reading the old one from the consumer group lag.

Event content. There are two extremes: a "thin" event (only orderId, the rest you fetch from an API) and a "fat" event (the whole state). A thin one keeps the schema stable but puts synchronous load on the producer. A fat one gives independence, but every field becomes part of the contract. I usually pick the middle: the fields that cover 80% of the consumers' work, and nothing else.

There is an organizational side too: every event needs an owner, documentation (AsyncAPI) and a deprecation policy. Without them the number of events grows and nobody dares delete any.