Sub topic 1
What an Event Is, and How It Differs from a Command
Prerequisites
This chapter assumes you can read short Python and JSON snippets. It also assumes you know how HTTP request/response APIs work, and you have a basic idea of what a database transaction is. It helps to know that a distributed system is several programs cooperating over a network.
Events, commands and queries
Three kinds of messages show up in most systems. They look similar on the wire, but they mean different things.
- A command asks something to do work. Example:
PlaceOrder. It is named in the imperative, it has one intended receiver, and the receiver may refuse it. - A query asks something to report data. Example:
GetOrder. It changes nothing and expects an answer. - An event states that something already happened. Example:
OrderPlaced. It is named in the past tense, it cannot be refused, and the sender does not know who will use it.
The key difference is the direction of knowledge. A command sender knows who it wants to act. An event sender only reports a fact and lets others decide what to do.
| Message | Tense | Receivers | Can be rejected | Expects reply |
|---|---|---|---|---|
| Command | Imperative (`ReserveStock`) | One, known | Yes | Usually a result |
| Query | Question (`GetStock`) | One, known | Rarely | Yes, data |
| Event | Past (`StockReserved`) | Zero or more, unknown | No | No |
What an event looks like
An event is an immutable record of a fact. Because it is already true, you cannot change it. If the world changes, you publish a new event. A useful event carries an envelope with a unique ID, a type, a version, a timestamp and a payload.
{
"id": "6f1c2d9e-3b7a-4c55-9d0e-1a2b3c4d5e6f",
"type": "order.placed",
"version": 1,
"occurredAt": "2026-11-26T11:00:00Z",
"data": {
"orderId": "o-1",
"customerId": "c-9",
"totalCents": 4599
}
}The id lets consumers detect duplicates. The type and version let the schema change over time. Later chapters cover both uses in detail.
Seeing the difference in code
The sketch below keeps a simple in-process event bus. The publisher announces a fact. It does not know which handlers exist, and one event reaches two independent handlers.
from collections import defaultdict
class EventBus:
def __init__(self):
self._handlers = defaultdict(list)
def subscribe(self, event_type, handler):
self._handlers[event_type].append(handler)
def publish(self, event_type, payload):
for handler in self._handlers[event_type]:
handler(payload)
bus = EventBus()
bus.subscribe("order_placed", lambda e: print("reserve stock for", e["order_id"]))
bus.subscribe("order_placed", lambda e: print("send receipt for", e["order_id"]))
bus.publish("order_placed", {"order_id": "o-1"})Adding a third handler, such as one for analytics, needs no change to the publisher. With commands, the order service would have to call each service by name.
Common errors
- Disguising a command as an event. A name like
SendEmailRequestedis a command in event clothing if the publisher expects exactly one service to act. That is fine sometimes, but it keeps the coupling you were trying to remove. - Using the present or imperative tense.
UpdateInventoryis a command.InventoryAdjustedis an event. - Assuming a reply. If the publisher waits for a consumer's result, the design is request/response with extra steps.
- Changing past events. Facts are immutable. Publish a correcting event instead, such as
OrderCancelled.
Exercise
Classify each as command, query or event: (a) GetCustomerProfile, (b) PaymentCaptured, (c) CancelSubscription. Explain which one the sender cannot expect to be refused.
Show answer
(a) is a query, (b) is an event, (c) is a command. The event PaymentCaptured cannot be refused, because it reports something that already happened. The command CancelSubscription can be rejected, for example if the subscription is already cancelled.
Exercise
A service publishes CreateInvoice to a topic and waits for a reply before continuing. Why is this not really event-driven, and how would you rename and restructure it?
Show answer
It is a command with a single expected receiver and an expected reply, so the publisher is coupled to the invoicing service. Instead, publish a past-tense fact such as OrderPlaced and let the invoicing service decide to create an invoice. The publisher then continues without waiting.