Cleanly Separating Commands, Events and Queries
Command, event and query are three kinds of messages with clear semantics: a request that can be rejected, a fact that has happened and a query without side effects. If you name and separate them cleanly, you gain clarity in the model – without a big CQRS setup.
updateOrder(...) is not a domain message. The name doesn’t say whether someone wants something or whether something has already happened – it only describes that fields change. Method names like this are the reason models get blurry: request, fact and question travel under the same name through the same classes, and nobody can tell from the signature which of them is currently on its way.
Command, event and query clean this up. The three differ in three points that you can check on every single message: in their tense, in whether they may be rejected and in the number of recipients. The following sections go through these points – first conceptually, then in TypeScript, then along the mistakes that regularly happen along the way.
The form of a message follows its meaning. JSON objects that look the same can still carry completely different contracts.
Telling messages apart by what they state
- Meaning · request to attempt a state change
- Time reference · aimed at an intended future
- Outcome · can be rejected or fail
- Meaning · a fact that has already occurred
- Time reference · immutable past
- Outcome · is not rejected after the fact
- Meaning · request for existing information
- Time reference · view of the current or a specified state
- Outcome · does not change the domain state
This distinction makes names, error paths and responsibilities more precise before any framework comes into play.
Three messages, three meanings#
Let’s start with the core, because this is where everything is decided. A command, an event and a query are fundamentally different things, even if they look superficially similar in code – in the end, each of them is an object with a few fields. The difference is not in the structure but in the meaning.
A command is an imperative wish. “Do X.” CreateOrder, ShipProduct, CancelReservation. A command expresses an intention that has not happened yet. And this is crucial: a command is a request that can be rejected. It goes through validation and business rules, and if something isn’t right, it fails. Exactly one handler is responsible; a command is directed – it has an intended recipient.
An event is the exact opposite on the timeline. It is a fact that has already occurred. OrderCreated, ProductShipped, ItemOrdered – named in the past tense. An event is immutable, because you can no longer change the past. And an event cannot be rejected: you cannot say “no” to something that has happened. It is the result of a successfully processed command. While a command has exactly one handler, an event can have zero, one or many consumers – it is a notification to potentially many.
Finally, a query retrieves data without changing anything. GetOrderSummary. It returns a result and leaves the observable state of the system untouched – it is free of side effects. A query reads, ideally from a view optimized for reading, and can be repeated harmlessly as often as you like.
This semantic separation is the whole point. Bertrand Meyer formulated it decades ago as Command-Query Separation: every method is either a command that changes state but returns nothing, or a query that returns something but doesn’t change state – never both. The event joins as a third kind when we want not only to order state changes but also to report on them.
The name carries the semantics#
When I come across blurry models in reviews, it is almost always down to naming. The name of a message is not decoration – it is the semantics made visible. A command in the imperative, an event in the past tense. This discipline sounds trivial, but it is the cheapest and most effective lever I know.
Let’s compare the three kinds directly:
- A command is in the imperative and present tense (
CreateOrder): an intention, directed at exactly one handler, that can be rejected and can fail. - An event is in the past tense (
OrderCreated): a fact, immutable and not rejectable, directed at zero to many consumers. - A query usually starts with
GetorFind(GetOrderSummary): a question, free of side effects; it returns data and changes nothing.
Once a team has internalized this naming convention, the code almost reads itself. You see CreateOrder and know: someone wants something here that can go wrong. You see OrderCreated and know: this has already happened, I can count on it. The name replaces half the documentation.
The flow: from wish to fact to answer#
So how do the three fit together? The typical flow is surprisingly straightforward. A command arrives and is checked by its handler. The handler validates and applies business rules. If everything checks out, it emits one or more events – the facts that follow from the successful command. These events then update the read model, the view for reading. And finally, a query reads from exactly this read model without touching the write path at all.
Martin Fowler describes this as two separate paths: the changed state is routed through the command model, and the display comes from the query model. Writing goes via command to event, reading goes straight to the read model.
flowchart LR C[Command: CreateOrder] --> H[Command Handler] H -->|validate / business rules| E[Event: OrderCreated] E --> RM[(Read Model)] Q[Query: GetOrderSummary] --> RM
In this picture, you can see the asymmetry that defines the whole approach. The write path is a process with a check: command, validation, and only then the fact. The read path is a direct query without a detour. Both share the read model, but they approach it from opposite sides – one writing via events, the other reading via queries.
Cast in code#
Enough theory – let’s look at what this looks like in TypeScript. The following code is illustrative, not a runnable repository; it is meant to show the semantics, not to replace a framework. Let’s start with the message types themselves, modeled as discriminated unions:
type LineItem = { sku: string; quantity: number };
type Command =
| { type: 'CreateOrder'; orderId: string; customerId: string; items: LineItem[] };
type DomainEvent =
| { type: 'OrderCreated'; orderId: string; customerId: string; occurredAt: Date };
type Query =
| { type: 'GetOrderSummary'; orderId: string };
You can already read the semantics from the types. The command is in the imperative. The event is in the past tense and carries an occurredAt – the point in time at which the fact occurred, because an event lives from its place in time. The query starts with Get and visibly carries nothing but a question.
Now the command handler. It is the heart of the write path: it checks, decides and emits. What matters is that a command can be rejected – and that the return value is a list of events, because a command may well entail several facts:
class RejectedCommand extends Error {}
function handleCreateOrder(cmd: Extract<Command, { type: 'CreateOrder' }>): DomainEvent[] {
if (cmd.items.length === 0) {
throw new RejectedCommand('order needs at least one item');
}
return [
{
type: 'OrderCreated',
orderId: cmd.orderId,
customerId: cmd.customerId,
occurredAt: new Date(),
},
];
}
This is where the semantics become concrete. The handler validates, and if the request cannot be fulfilled, it rejects it – the command fails before any fact comes into being. Only when the check passes is the event born. No event without passed validation; that is the order we want.
On the other side is the query handler. It does exactly one thing: read. No mutation, no emitted event, no side effect:
interface OrderReadModel {
findSummary(orderId: string): OrderSummary;
}
type OrderSummary = { orderId: string; itemCount: number; status: string };
function handleGetOrderSummary(
q: Extract<Query, { type: 'GetOrderSummary' }>,
readModel: OrderReadModel,
): OrderSummary {
return readModel.findSummary(q.orderId); // read only, no mutation, no events
}
And to close the circle: how does the read model get its data in the first place? Through a projection that reacts to events. The projection takes an OrderCreated and updates the read view – this is how the fact flows from the write path into the view that queries later read:
function applyOrderCreated(
event: Extract<DomainEvent, { type: 'OrderCreated' }>,
store: Map<string, OrderSummary>,
): void {
store.set(event.orderId, {
orderId: event.orderId,
itemCount: 0,
status: 'created',
});
}
If you lay these four building blocks side by side – command type, command handler, projection, query handler – you see the entire flow at a glance. The command is the wish, the handler checks it, the event is the fact, the projection carries it into the read model, and the query reads from there. Every message has its place and its meaning.
The pitfalls I keep seeing#
As clear as the theory is, the mistakes in practice are just as stubborn. I keep running into four of them.
The most common one is inverted naming. An OrderCreated that is actually a command, or a CreateOrder that flies around as an event. As soon as that happens, the team loses the distinction between “intention that can fail” and “fact that has already occurred”. The name lies, and the model with it. When I find an event in the imperative in a review, that is my first red flag.
The second pitfall is the query with a side effect. A supposed query that writes on the side, increments a counter or fires an event. That breaks the freedom from side effects that Meyer demands for queries. A query has to be harmlessly repeatable – if I call it twice, nothing in the system may change. As soon as a query mutates state, it is no longer a query but a command in disguise.
The third mistake is conceptual: confusing CQRS with Event Sourcing. Separating command, event and query doesn’t need Event Sourcing. Nor does it necessarily need two databases or an asynchronous message bus. You can cleanly separate the three kinds of messages in a perfectly ordinary service with a single database. Greg Young sums up the relationship in one rule of thumb: you can use CQRS without Event Sourcing; if you use Event Sourcing, you practically need CQRS. Not the other way around.
The fourth pitfall is the anemic command as a setter. An UpdateOrder(status = 'shipped') reveals nothing about the domain intention. Why is the status changing? Because the product was shipped. Then the command is called ShipProduct, not UpdateOrder. A command should express a domain intention, not a generic data manipulation. The difference between UpdateOrder and ShipProduct is the difference between a form and a story.
A quick look beyond: CQS, CQRS and the costs#
So that the terms don’t get mixed up, a clean distinction pays off. CQS – Command-Query Separation according to Meyer – is a principle at the method level: every method is either a command or a query. CQRS – coined by Greg Young and popularized by Fowler – lifts the same principle to the object or service level: a separate model for writing and a separate model for reading. Fowler puts it this way: you can use a different model to update information than the model you use to read information.
And that comes with a sentence that often gets lost: CQRS is not a default. Fowler explicitly warns that CQRS adds risky complexity and is only justified when there is a clear benefit. Separate models, possibly separate stores, asynchronous consistency – you don’t want that everywhere. If you introduce CQRS everywhere by reflex, you take on more problems than you solve. I have described in more detail elsewhere why CQRS is a domain boundary, not a framework – the decision for separate models belongs at the domain consistency boundary, not in every service.
The good news: the purely semantic separation of command, event and query costs almost nothing and almost always pays off. It is not CQRS. It is the conceptual hygiene that CQRS can later build on, if you need it at all. You can name commands in the imperative, events in the past tense, keep queries free of side effects – and stay in a completely conventional service while doing so. This clarity in the small is the real gain.
The common thread#
The question from the beginning – does someone want something, or has something happened? – can be answered for every message as soon as it carries an honest name. A command is the wish that gets checked and can fail. An event is the fact that follows from it, immutable and not negotiable. A query is the question that reads without touching. Three messages, three meanings, three names – and the name carries the meaning outward.
This separation is not a big architecture project. It starts with discipline in naming and with refusing to let a query secretly write. You don’t have to introduce Event Sourcing or set up two databases to reap the benefits. If you consistently distinguish between CreateOrder, OrderCreated and GetOrderSummary in your next model, you have already done most of the work. Everything else – CQRS, projections, separate stores – is a question you ask later, and only when there is a real need. The semantic separation itself costs nothing but attention, and that attention pays off in every model.
Further reading#
- Slide deck on this topic: https://slides.mikebild.dev/introduction-cqrs-event-sourcing.en.html
- Martin Fowler, CQRS – the compact definition, including the warning about complexity.
- Martin Fowler, CommandQuerySeparation – Meyer’s original principle at the method level.
- Martin Fowler, What do you mean by “Event-Driven”? – on classifying events as messages and facts.
- Greg Young, CQRS – the origin of the object/service level and the relationship to Event Sourcing.
- Vaughn Vernon, Implementing Domain-Driven Design (2013), chapter on domain events – on naming events and their meaning in the domain.
- My own talk material “CQRS + Event Sourcing” (SlideShare) – the slides in which I go through this separation in more detail.
Revised and translated from German with the help of AI.