This post is part of Working with events, a series about messages in distributed systems.
If I had to keep a single idea from this whole series, it would be this one: an event is a story, and the story belongs to whoever tells it.
To get there, we first need to separate the two kinds of messages that travel through the same pipes. That is the first part of this post, and it ends where the difference matters most: who owns each contract. The second part zooms out from a single message to the whole business, with two tools from Domain-Driven Design that help you find the stories, and their storytellers, before you design a single event.
One thread runs through both parts: ownership. Whether you look at one message or at a wall full of sticky notes, the question is the same. Whose story is this?
Commands and events: who owns each message
Two kinds of messages
A broker does not care what you send through it. A queue is a delivery mechanism, in the same way that HTTP is. That is why so many conversations about “events” are really about messages, and why it is worth being precise about which kind you are sending.
| Command | Event | |
|---|---|---|
| Intent | Invokes behavior: do this | Informs: this happened |
| Consumers | One, maybe running as several instances | Many, or none at all |
| Senders | Many | Usually one |
| Contract owner | The consumer | The publisher |
Intent. A command is an order. An event informs, and it always talks about the past.
Consumers. A command usually has a single consumer. Several instances of it may be running, but conceptually there is one recipient. An event can have many consumers, or none at all, and the publisher should not care which.
Senders. Many different senders can issue the same command to the same consumer. An event usually has a single publisher: the part of the system where the thing happened.
Ownership. This is the one that matters most. The contract of a command belongs to the consumer, the one who knows how to carry out the order. The contract of an event belongs to the publisher, the one who tells the story.
If you think about it, a typical API behaves like a command. It does not matter whether it is a GET or a POST: you ask, the API decides what it accepts, and the API owns the contract.
A story told in messages
Back to the shop from the previous post. This is the same flow, message by message, with who sends each one and who owns its contract:
| Message | Kind | Sent by | Contract owned by |
|---|---|---|---|
CheckOutCart |
Command | The shopper, through the website | Cart |
CartCheckedOut |
Event | Cart | Cart |
PlaceOrder |
Command | Orders itself, reacting to CartCheckedOut |
Orders |
OrderPlaced |
Event | Orders | Orders |
ChargeCard |
Command | Billing | Payment provider |
OrderPaid |
Event | Billing | Billing |
SendEmail |
Command | Notifications | Mail service |
PlaceOrder deserves a second look. Nobody sends it to Orders in this flow: Orders has a rule of its own, whenever a cart is checked out, place the order, and carries out the command itself. It is the same command the back office and the partners send in the diagram above, so its contract still belongs to Orders.
In this flow, commands and events alternate, although in general a command can produce several events, and an event can trigger several commands or none. Commands appear where someone asks a specific piece to do something, and especially at the edges, where we talk to systems we do not control. Events appear where a service tells whoever is interested that something happened in its domain. The names follow: commands are imperative (CheckOutCart), events are in the past tense (CartCheckedOut). If this chain looks familiar, it is the grammar of Event Storming, a workshop format for discovering a domain with sticky notes, orange for events and blue for commands. It gets a part of its own below.
Read the last column of the table from top to bottom: the owner changes at almost every step, because commands belong to whoever receives them and events to whoever tells them. At the edges, that is not even up for discussion. Nobody gets to tell the payment provider what its API should look like; Billing adapts to it.
Beyond fire and forget
The most common pattern is the one-way notification: the publisher tells the story and moves on. But decoupled does not mean you can never get an answer.
Request/reply exists in distributed systems too. You send a message with the request, a command or a query, including an identifier and the address where you expect the answer. The responder sends its reply to that address, with the same identifier, while the requester listens in the background for the reply that matches. The reply is neither a command nor an event: it is addressed to one specific requester, which is waiting for it. Enterprise Integration Patterns calls these pieces a return address and a correlation identifier, an idea that comes back near the end of the series, in the post about events in production. Libraries usually hide all this, and some protocols support it directly: AMQP has reply_to and correlation_id, and NATS has request/reply built in. They are not as rare as they seem.
Single request, multiple responses goes one step further: one request produces several answers. A long-running job reports its progress. Or several services answer the same request, for example different providers returning quotes, and the requester gathers whatever arrives.
Only on the back end?
Not at all. Messages and events are everywhere in front-end code.
The example I always use is GTA V. As the story goes, the single-player game was built around messages sent to an in-memory queue. When the online mode arrived, those messages were allowed to travel between players. That made it very hackable, because anyone who could craft those messages could send them directly.
It is a good reminder of what commands really are. When commands travel as messages, whoever can write to the channel can give orders to your system. The channel becomes part of your security boundary.
Commands in disguise
We should also be honest about something. Sometimes, just to be able to say they use events, people publish an event that is really a command in disguise.
It is usually easy to spot. The name describes what someone else should do rather than what happened: SendWelcomeEmail dressed up as WelcomeEmailRequested. It was designed for exactly one consumer. And the publisher would consider it a failure if nobody acted on it. An event that is not allowed to be ignored is an order.
The disguise is not harmless. The publisher is now coupled to the behavior of one specific consumer while pretending not to be, and the contract ends up shaped around that consumer’s needs. Which leads to the most common mistake of all.
Don’t let consumers design your events
When we talk about events, we are always talking about telling a story. Which story do I tell? The story of my domain. Whose domain is it? Mine. An event is a primitive for telling that story, and that puts us squarely in the territory of the ubiquitous language. And whose ubiquitous language is it? The owner’s.
That is why one of the worst practices I know is letting consumers design our events. It feels collaborative and pragmatic: a consumer needs something, so we add it. But each of those additions bends the story towards what someone else wants to hear, until the event no longer describes what happened in my domain.
I am insistent about this because ownership conditions the design of the events themselves: their names, their content, how they evolve and who has the right to change them. It will come back when we talk about fat events, boundaries and versioning.
In practice, ownership is often contested. A powerful consumer, a shared library of “common” events, a platform team that wants a standard: the pressure is real. My position does not change. The publisher owns the story. Consumers can ask; the owner decides.
That does not leave consumers defenceless. A consumer can translate the events it receives into its own model, what Domain-Driven Design calls an anti-corruption layer, so that the publisher’s language does not leak into its code.
Finding the stories: Event Storming and bounded contexts
The first part looked at ownership one message at a time. That raises two practical questions before you design a single event: which stories does your business tell, and who tells each one?
Two ideas from Domain-Driven Design help here. Event Storming helps you find the stories. Bounded contexts help you decide who owns them.
A wall, sticky notes and the past tense
Event Storming is a workshop format created by Alberto Brandolini. You put the people who know the business and the people who build the software in front of a very long wall, give everybody orange sticky notes, and ask them to write down things that happen in the business, in the past tense: cart checked out, order placed, order paid, order shipped.
It is chaotic on purpose. At first everyone writes whatever they know, and the wall fills up with duplicates, gaps and contradictions. Then the group sorts the notes into a single timeline, and the real conversation starts: this one happens before that one, these two are the same thing with different names, nobody knows what happens here.
One of the best ways to find the gaps is to tell the story backwards. Pick the event that means everything went well, the one that closes the happy path, such as order shipped, and walk back from it, asking each time what had to happen before. Told forwards, a story skips the steps everyone takes for granted. Told backwards, every event has to be explained by the one before it, and the missing ones stand out: there is no order shipped without an order paid, and no order paid without someone or something charging the card. Brandolini calls this the reverse narrative and uses it to check a timeline once it is on the wall, but it works just as well as a starting point when the room does not know where to begin.
Other colours join the orange ones as the story takes shape:
- Blue for the commands that cause the events, with a small yellow note for the person who issues each one.
- Lilac for policies: whenever this happens, do that. Whenever an order is placed, charge the card.
- Pink for external systems, like the payment provider.
- Green for the information someone needs in order to decide, the read models.
- And hotspots, usually bright magenta, for the questions nobody can answer yet.
If that grammar sounds familiar, it is the one from the first part: commands where someone asks for something, events where something happened.
What the wall tells you
The wall is not documentation; it is a conversation that leaves a trace. But it gives you three things that are hard to get any other way.
First, candidate events, named by the people who live the process, in their own language. Not all of them will become messages, and some will never leave a service, but they are the stories your business already tells.
Second, the questions that matter. Hotspots mark the places where the business itself is not sure how things work, and those tend to be exactly the places where an event design would go wrong.
Third, the boundaries. At some points along the timeline the language changes. The people who talk about carts and discounts stop talking, and the ones who talk about invoices and refunds take over. Those points, often marked by a key event such as order placed, are where one storyteller ends and the next one begins.
Bounded contexts: where a word means one thing
A bounded context is the boundary inside which a model, and the language that goes with it, is consistent. Inside it, “order” means one precise thing. Outside it, the same word can mean something else. For Sales, an order is what the customer accepted, with its prices and discounts. For Shipping, it is a set of parcels with an address and a delivery window.
We will meet the same problem again in the post about the event contract, when we name events and two parts of the business use the same word for different things. A bounded context is the answer at the level of the model.
For events, bounded contexts give you the storyteller. An event belongs to the context where it happened, and that context owns its name, its content and the way it evolves. The events that stay inside a context are its own business. The events that cross a boundary are the ones other teams will hear, and they deserve the most care.
How contexts listen to each other
Domain-Driven Design has a vocabulary for how contexts relate to each other, the context map, and a few of its terms are worth knowing when you work with events:
- A published language is a shared, documented way of telling your stories to others. Integration events are a published language.
- A conformist consumer simply adopts the model of whoever publishes. Sometimes that is fine, and it is always cheaper.
- An anti-corruption layer translates someone else’s model into your own, so that it does not leak into your code. It already appeared in the first part, on the side of the consumer.
None of this requires a big design up front. It is a way to name what is already there, so that you can decide on purpose.
Start small
You do not need two days and a rented room. An hour with the right people, a long enough wall or a virtual whiteboard, and one process you care about are enough to start. Three things make it work:
- The people who know the business have to be in the room. Without them, you only get the developers’ version of the story.
- Past tense, always. “Send email” is a command; “email sent” is an event. That difference is the whole point of the exercise.
- Chaos first, order later. If you start by arguing about names, you never finish the timeline.
Alberto Brandolini’s book, Introducing EventStorming, covers the format in depth.
Questions to ask
- Is this message asking someone to do something, or telling what happened?
- Who owns this contract, and who is allowed to change it?
- Would the publisher consider it a failure if nobody consumed the message? Then it is probably a command.
- Is the name in the publisher’s language or in the consumer’s?
- If this message crosses a trust boundary, what happens when someone else sends it?
- Who in your business knows how this process really works, and were they in the room?
- Which events did everybody agree on, and which ones had two names?
- Where does the language change along the timeline?
- Which context owns each event, and does it stay inside that context or cross a boundary?
- Where are the hotspots, and who is going to answer them?
Next in the series: queues, streams and the consumers that read them.