Skip to content

Commands and Queries

At the heart of any event-driven system lies a simple idea: Everything a user does is either a command or a query.

  • A command expresses an intention to change something.
  • A query asks for information – without changing anything.

This distinction is the foundation of CQRS.

From Buttons to Behavior

Imagine you're using the online system of a city library. As a registered member, you can:

  • Borrow a book
  • Extend a loan
  • Return a book
  • Pay a late fee

Each of these actions is a command – it tells the system what you're trying to do. Under the hood, this might be represented as:

  • BorrowBook
  • ExtendLoan
  • ReturnBook
  • PayLateFee

Each command is a clear, named intention – specific to the domain.

Queries Work Differently

Now imagine you're browsing your account. You want to:

  • See which books you currently have on loan
  • Check the due dates
  • Find out if a specific book is available

These are queries. They retrieve information but don’t change anything.

In a CQRS-based system, commands and queries are handled separately – often by different parts of the application, each optimized for its own job.

It All Starts Here

This is the starting point for every interaction:

A user sends a command or a query.

And everything that happens in the system from this point on – events, updates, projections – follows from that first input.

What we mean by 'Book'

For simplicity, we're using the term "Book" throughout this walkthrough. In a real-world system, you'd distinguish between works, editions, and copies. Here, we're treating it as a single concept to keep things clear.

Next up: See how a command is processed by the system using a Command Handler, and how that leads to events.