liveBook · Manning

Command

As explained in Microservices Patterns, Second Edition

A system operation that mutates application data.

A command is a system operation that mutates application data, such as createOrder() or cancelOrder(). A query, by contrast, retrieves data. Commands can be invoked by a user through a UI, another application, an event, or a schedule; the operation itself is technology-neutral. section 3.1.1

A command must preserve correctness when it makes multiple changes or runs concurrently with other operations. Partial failure can leave data inconsistent, concurrent updates can conflict, and business rules such as credit limits must still hold. section 9.1.1

Creating an order

A simplified createOrder(consumerId, productId) command obtains the product price, checks and reduces the consumer’s available credit, and creates an order. These changes must be coordinated so a failure does not leave credit reduced without the intended order. section 9.1.1

When a command is local to one service, an ACID transaction is the straightforward choice. A command spanning services crosses separate database and transaction boundaries. Classic distributed transactions provide ACID guarantees but introduce complexity and runtime coupling, making them a poor fit for microservices. section 9.1 section 9.4

For a command spanning services, a saga coordinates a series of local transactions. Each step runs a service operation that validates and updates its own persistent entities. Already committed changes may require compensating transactions if a later step fails. A saga does not provide ACID isolation across its steps, so the application must address concurrent-execution anomalies itself. section 9.2 section 9.4

Figure 9.2 A saga replaces what would other be a distributed ACID transaction with a sequence of local ACID transactions in each of the participating services.
saga overview

The diagram contrasts a distributed ACID transaction with the local transactions used by a saga. section 9.2

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains invocation, correctness challenges, and transactional implementation, including saga compensation and isolation trade-offs.
Examples
Uses order creation to illustrate product-price lookup, credit reservation, and order insertion.
What you can do after
Evaluate transactional approaches for commands local to one service or spanning multiple services.
How it connects
Connects commands to queries, ACID transactions, sagas, compensating transactions, and isolation.

Also explained in

Compare how 7 books explain command →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.