This post is part of Working with events, a series about messages in distributed systems.
The previous post was about the story itself: what an event is for, how long it lasts and what it should carry. This post is about everything around the story that turns it into a contract.
Turning a story into a contract takes three things:
- The envelope, which carries what every event needs no matter what it tells, and which the series has been leaning on all along: detecting duplicates needs an id, keeping order needs a key, and tracing needs a correlation id.
- The name, which looks cosmetic until it hurts.
- Evolution, because every contract changes sooner or later, and the consumers that depend on it should not find out the hard way.
One thread runs through all three: the version. It can be a field on the envelope or a segment in the name, and, ideally, it is something you hardly ever need to change.
The envelope: what every message carries
Think of a letter. The envelope says who sends it, who it is for and when it was posted, and the postman only ever reads the envelope. The letter inside is the story, and only the person who opens it cares about it.
A message works the same way. The payload is the story: the cart, the customer, the total. The envelope, or metadata, is everything that the infrastructure and the consumers need in order to handle the message without understanding the story.
What goes on the envelope
There is no single list, but these fields come up again and again:
- Id. A unique identifier for this message, and the same one for every copy of it. It is what a consumer uses to detect duplicates, as we will see in the next post, about delivery guarantees.
- Type. What happened, with a name like the ones in the second part of this post.
- Source. Who is telling the story: the service, the context, sometimes the instance.
- Time. When it happened. Not when it was published, and not when it was received: those can be minutes or days apart, and replaying old events makes the difference impossible to ignore.
- Subject or key. The entity the story is about, such as the order or the customer. Brokers use it to keep the events of the same entity together and in order, as we saw with partitioning by key in the post about queues and streams.
- Version or schema id. Which shape of the event this is, so that a consumer, or a schema registry, knows how to read it.
- Correlation and causation ids. Which process this message belongs to, and which message caused it. They get a section of their own in the post about running events in production.
Some systems add a tenant, a content type or a trace context. A good rule of thumb: if every consumer would need it to handle the message, whatever the story, it belongs on the envelope.
A standard for the envelope
You do not have to invent the names. CloudEvents is a specification for exactly this envelope: id, source, type, time, subject, dataschema, and a data field with the story. Anything else goes into extension attributes. The standard does not define the correlation and causation ids in this example, but it allows them: an extension only needs a lowercase alphanumeric name, so the event is still a valid CloudEvent. One extension is worth knowing: partitionkey. It holds the key a broker partitions by, and the Kafka binding turns it into the message key. subject may look like the natural candidate, but it only describes the entity; nothing partitions by it unless you say so.
{
"specversion": "1.0",
"id": "4f1c2b7e",
"source": "/sales/carts",
"type": "Acme.Sales.Event.Carts.CartCheckedOut",
"time": "2026-11-12T10:00:00Z",
"subject": "cart/ct-123",
"dataschema": "https://schemas.acme.example/sales/cart-checked-out/1",
"correlationid": "c7d1e9a0",
"causationid": "a1b2c3d4",
"partitionkey": "ct-123",
"datacontenttype": "application/json",
"data": { "cartId": "ct-123", "customerId": "c-981", "total": 49.9 }
}
Headers or body
The envelope can travel in two places. It can go in the message headers, the way Kafka headers or AMQP properties work, so that brokers and tools can route and filter without opening the payload. Or everything can travel together in the body, which is easier to store, copy and inspect. CloudEvents calls these binary mode and structured mode, and supports both.
Whichever you choose, choose it once, for everybody. A consumer that has to guess where the id is this time is a consumer that will get it wrong.
Schemas and serialization
The payload needs a shape, and the shape needs a format. The three you will meet most often are:
- JSON, readable by anyone and flexible, with no schema unless you add one, usually with JSON Schema.
- Avro, compact and always read with a schema. It is common with Kafka and a schema registry, which stores the schemas so that each message only carries a reference to its own.
- Protobuf, compact too, with numbered fields that make many changes safe, and unknown fields that old readers simply skip.
The choice affects size, speed and tooling, but above all how events can evolve, because each format has its own rules about which changes are compatible. That is the subject of the last part of this post.
The name: how a message is identified and found
The type on the envelope is a name, and names deserve more care than they usually get. There are many valid ways to name messages, and the slide I use for this part carries a disclaimer in red: your messages, your rules. At a previous company we had a standard and everybody followed it, and that is fine too. But whatever convention you choose, a namespaced format usually helps.
Names identify
A namespace first of all helps you identify things, because sooner or later you discover that two things with the same name mean different things. The ubiquitous language can be treacherous: each bounded context has its own, as the post about who owns the story explained, and the same word can mean something else on the other side of a boundary.
Internet providers are my favorite example. Upload and download bandwidth mean different things depending on whether you are talking to the people who run the servers or to the people in marketing: what is download for me is upload for whoever is on the other side. And, as Eduardo Ferro added, what the provider has contracted with its own upstream provider is exactly the opposite again.
It would not be the first time that two names end the same way, say OrderPlaced, and then two companies merge, or two business areas start publishing into the same place, and it turns out that “order” means something different to each of them. The namespace keeps them apart.
Names filter
The second reason is filtering. As a system grows, the consumers of a topic tend to be of different kinds, and it is common to want to consume only part of it. In a topic where Sales publishes its events, maybe I only care about carts. With namespaced names, I can subscribe to exactly that:
Acme.Sales.Event.Carts.#
In RabbitMQ, for example, * matches exactly one segment and # matches any number of them: Acme.Sales.Event.Carts.* would match CartCheckedOut, but not CartCheckedOut.v2. Where you put the version also decides which filters work.
Kafka does not filter on the broker, as the post about queues and streams explained: every consumer reads the whole partition and discards what it does not need. The name is still what it filters on.
Including the message type, event or command, in the name is a choice. I do not usually make it, but it is a valid one: it lets a consumer that only handles events filter out everything else. In a system like Kafka it tends to be unnecessary for a different reason: you will want commands in another topic anyway, or in a queue that only their consumer reads, as in the post about who owns the story, because you will not want to keep them for long. Nobody needs the full history of every command they received last year.
Where does the version go?
Then comes versioning, and here the options branch out a lot. You can include the version in the name or leave it out.
If you leave it out, the version goes into the envelope or into the schema. That is fine, but you lose the ability to filter by name.
If you put it in the name, where you put it also has consequences, because these two names mean different things:
Acme.Sales.Event.Carts.CartCheckedOut.v2
Acme.Sales.v2.Event.Carts.CartCheckedOut
The first one says that CartCheckedOut has two versions. The second one says that there are two versions of the whole package of events published by Acme Sales.
Maybe you lived through the move to version 2 of the Twitter API. Years ago they realized that, surprise, the API had been badly designed, and instead of versioning individual endpoints they released a new version of the whole API. That style is common in big products with long delivery cycles, the ones that sell version 2 and then version 3, one every year or two. For events, versioning each event separately usually gives you more granularity, and it makes double publish possible, which we will get to in the next part.
You may also decide not to put the version in the name at all, because you manage versions through a schema registry and do not want to pollute the names. That is a valid choice. There is no single answer.
My default recommendation is not to complicate things. If you do not even know whether you will have versions, do not design for them yet. In fact, if you can avoid having versions, avoid it, and that is what the next part is about.
There is one cheap exception worth considering, and it is the version field from the envelope: carry a version number or a schema id in the metadata from the first day, even if it is always 1. Events live long and get replayed, and one day you may need to know which shape an old event has.
Evolution: change the contract without breaking it
The rule I start from comes from Greg Young, who has worked on this more than almost anyone; he even built a database for events. In Versioning in an Event Sourced System, he says, roughly, that a new version of an event must be convertible from the old version. If it is not, it is not a new version of the event, “but rather a new event”.
I keep coming back to narrative for this. If you cannot easily go from the old event to the new one, maybe the story you are telling is completely different. Maybe it is another event, and you are telling me another story. That is not evolution: the story has branched.
Versioning events is versioning APIs
If you look closely, versioning events is very much like versioning APIs, and the same patterns apply. The first one I always recommend is the open/closed principle, applied to versioning.
If the only thing I did to an event was add a new field, no consumer of the old version cares, as long as consumers ignore the fields they do not know. There is a new field that they did not use before and will not use now, until they want to. That sounds obvious, but it is not the default everywhere: Jackson, for example, fails on unknown properties unless you tell it not to, and so does a JSON Schema with additionalProperties: false. Martin Fowler calls a consumer that reads only the fields it needs and ignores everything else a tolerant reader. So adding a field is always preferable to, for example, changing the type of an existing one.
That is why I prefer to talk about evolving events rather than versioning them.
Consume by data, not by version number
From the consumer’s point of view, you do not necessarily have to depend on a version number. As a consumer, I may know that this event used to carry one piece of data and, from a certain moment, started carrying another. My consumption strategy can be based on the data the event brings, not on its version number.
It may look like extra complexity. But think about the pain of bumping a version every single time you add a field: that ends up being hell. You can still dispatch by version number, by event name or even by type, if your language is strongly typed. Two patterns help here: a data mapping pattern, which translates whatever arrives into the model you work with, and a strategy pattern, which picks a different handler for each shape.
on CustomerRegistered(event):
email = event.contactEmail ?? event.email
Avoid breaking changes
What matters most is avoiding breaking changes to the contracts, because they lead to version explosion. Schema registries, which came up with Avro in the first part, are a natural step in this evolution: they hold the schema of every event, available to everyone. If versions start to explode in there, you are in trouble.
Two words help here. A change is backward compatible when consumers that use the new schema can still read old events, which is what you need to replay the history. It is forward compatible when consumers that still use the old schema can read new events, which is what you need to deploy the publisher before its consumers. A schema registry can enforce either rule, or both, every time someone registers a new version. How easy that is depends on the format: JSON Schema, Avro and Protobuf each have their own rules for what counts as compatible.
A strategy for each kind of change
Each kind of change has its own strategy:
| Change | Strategy |
|---|---|
| Adding a field | Just add it: open/closed, with tolerant readers |
| Renaming a field or changing its type | Expand and contract |
| Removing a field | Tolerant reader, plus a deprecation notice |
| Deep structural changes | Upcasting |
| Changing the meaning | A new event |
Expand and contract is another name for parallel change, applied to the fields of an event. I add the new properties, announce that the old ones will be deprecated, give consumers time to stop using them, and then remove them.
Double publish applies the same idea to the whole event, and Diego Álvarez put it into words before I could: if you want a new version, you send both versions until everybody stops consuming the old one. The publisher emits the event twice, once per version, and each consumer consumes the one it knows. At some point nobody reads the old version any more, and the publisher stops emitting it. There are other mechanisms, but they are more aggressive.
Two details make it safe. Both versions should carry the same event id on the envelope: a consumer that switches from one to the other can start reading the new version a little before the switch and skip the ids it has already processed, without losing or repeating anything. And publishing twice is two writes: if you do not want one version to go out without the other, write both to the outbox in the same transaction. The outbox comes in the next post.
Tolerant reader, which we saw with new fields, also covers removals: a consumer that never read a field does not notice when it goes, and a deprecation notice warns the ones that did. Together, they let fields leave quietly.
Upcasting happens in the consumer. Inside my event consumer I know the whole history of schemas the event has had, and I know how to go from version 1 to version 2, to 3, to 4. So I process each event, identify its version and transform it into the version I work with. It is a lot of work, but it depends on how often the event changes. If you have a business process with five years of history, and the event changed once a year, what works best? Probably keeping those transitions as a strategy in your code, rather than any other kind of magic.
A new event is the answer when the meaning changes. That is Greg Young’s rule again: if it is not convertible, it is another story.
Other things to consider
A few more questions are worth asking before the first version is ever published.
- Do you need semantic versioning? For events, rarely. What you usually need to know is whether a change is compatible, or whether it is a new event.
- Code-based or broker-based strategies? You can deal with versions in the consumer’s code, with upcasters and strategies, or in the infrastructure, with a schema registry’s compatibility rules or one topic per version. The more you push into the broker, the closer you get to the smart pipes we wanted to avoid, like the enterprise service bus in the post about queues and streams.
- Are you versioning the data or the behaviour? A change in the shape of the data can evolve. A change in what the event means cannot: that is a new event.
Besides contract changes and version explosion, there are two more common issues: the command-and-control consumer, which tries to dictate how the publisher’s events must look (the consumer that the post about who owns the story warned about), and broker abuse, which tries to solve with routing rules in the broker what should be solved in the contract.
Questions to ask
- Does every message carry an id that stays the same for every copy of it?
- Do you record when things happened, and not only when they were published?
- Which key keeps the events of the same entity together?
- Is the envelope the same for every team, or does each one invent its own?
- Which format do your events use, and which changes does it let you make safely?
- Could two parts of the business use the same name for different things?
- Which filters will your consumers need, and can your names support them?
- Do commands and events live in the same topic, and should they?
- Do you actually need versions yet? If you do, will you version events one by one or the whole package?
- Where does a consumer find the version of the event it is reading: in the name, on the envelope or in a schema registry?
- Is this change an evolution of the story, or a different story?
- Can every current consumer safely ignore the change?
- Do consumers depend on version numbers, or on the data they need?
- How many versions of this event will be alive at the same time, and for how long?
- Who needs to be told before a field disappears, and how?
- How much history will your consumers have to understand?
Next in the series: delivery guarantees, from duplicates and idempotency to the outbox and the inbox.