liveBook · Manning

Invariant

As explained in Microservices Patterns, Second Edition

An invariant is a condition that must remain true for application data or system state to be valid.

An invariant is a condition that must remain true for application data or system state to be valid. System commands may need to preserve one across several state changes, such as keeping inventory from being oversold or a customer within their credit limit. section 9.1.1

A transaction groups operations behind a correctness boundary: it commits only when every operation succeeds and required invariants hold. On failure, it aborts or undoes its effects so partial changes do not leave the system inconsistent. 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

This transaction conditionally deducts customer credit only when enough is available, inserts the order, and commits the grouped changes. section 9.1.1

Concurrent sagas

Create Order and Cancel Order sagas can interleave so that an order is overwritten, credit is released without having been reserved, or an order succeeds despite insufficient available credit. These outcomes violate business invariants even when each service enforces local transaction rules. section 9.3.1

How Microservices Patterns, Second Edition covers it

How deep it goes
Defines transaction commit and abort conditions, then illustrates saga anomalies that violate invariants without detailing remedies.
Examples
Uses oversold inventory, customer credit limits, and concurrent create-order and cancel-order sagas.
What you can do after
Evaluate whether a transaction meets its required invariants and recognize saga interleavings that violate them.
How it connects
Connects invariants to transactions, ACID transactions, concurrency, and saga isolation anomalies.

Also explained in

Compare how 18 books explain invariant →

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.