liveBook · Manning

Database transaction

As explained in Microservices Patterns, Second Edition

A unit of work whose operations execute under defined consistency and completion semantics.

A database transaction groups operations into a unit of work with defined consistency and completion semantics. It helps system commands address partial failures, concurrent updates, and business invariants such as a customer’s credit limit. Different transaction models provide different guarantees; the widely used ACID model is atomic, consistent, isolated, and durable. section 9.1.1

In a monolith with a single database, a system operation can often run as one transaction. The operation has a consistent view of data, and the database isolates it from concurrent operations. This usually makes the operation easier to implement, understand, and troubleshoot. section 6.1.1

Creating an order

A createOrder operation reads a product’s price, checks and reduces the consumer’s available credit, and creates an order. These changes must preserve the credit limit and avoid leaving a partially updated state if the operation fails. section 9.1.1

BEGIN TRANSACTION

SELECT * FROM PRODUCTS WHERE PRODUCT_ID = ?

UPDATE CONSUMER
  SET AVAILABLE_CREDIT = AVAILABLE_CREDIT - ?
  WHERE CONSUMER_ID = ?
    AND AVAILABLE_CREDIT >= ?

INSERT INTO ORDERS (...) VALUES (...)
COMMIT TRANSACTION

The SQL listing groups the product lookup, conditional credit update, and order insertion between transaction boundaries. section 9.1.1

A system query can also use a transaction for a consistent view. In a monolith, findOrder can read the necessary data from one database and use a centralized transaction manager to isolate the entire query. A join across domain tables is possible, though it trades away domain module boundaries. section 12.1.4

Choose transaction boundaries with the operation’s architecture and workload in mind:

  • Keep an operation in one transaction when practical. A transaction can span modules in a monolith, avoiding coordination among separate transactions. section 6.1.1 section 6.4.2
  • Split bulk work into multiple transactions when this improves error handling, performance, or scalability. An operation can also be split into a synchronous part and later event handlers. section 6.1.1
  • Expect redesign when moving an operation across services. Transactions are local to each service, so multiple transactions are not automatically isolated from one another. Coordination such as the Saga pattern adds consistency, concurrency, error-handling, and rollback concerns. section 6.1.1 section 6.4.2

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains correctness challenges and transaction boundaries, then contrasts single transactions with service-local transactions and their coordination costs.
Examples
Illustrates order creation with a product lookup, conditional credit update, and order insertion, plus an order-details query.
What you can do after
Evaluate transaction boundaries when choosing between a single database operation and coordinated transactions across services.
How it connects
Connects transactions to ACID, concurrency, business invariants, monolithic architecture, microservices, and the Saga pattern.

Also explained in

Compare how 21 books explain database transaction →

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.