Chapter 4. Managing transactions with sagas


This chapter covers

  • Why distributed transactions aren’t a good fit for modern applications
  • Using the Saga pattern to maintain data consistency in a microservice architecture
  • Coordinating sagas using choreography and orchestration
  • Using countermeasures to deal with the lack of isolation

When Mary started investigating the microservice architecture, one of her biggest concerns was how to implement transactions that span multiple services. Transactions are an essential ingredient of every enterprise application. Without transactions it would be impossible to maintain data consistency.

ACID (Atomicity, Consistency, Isolation, Durability) transactions greatly simplify the job of the developer by providing the illusion that each transaction has exclusive access to the data. In a microservice architecture, transactions that are within a single service can still use ACID transactions. The challenge, however, lies in implementing transactions for operations that update data owned by multiple services. For example, as described in chapter 2, the createOrder() operation spans numerous services, including Order Service, Kitchen Service, and Accounting Service. Operations such as these need a transaction management mechanism that works across services.

4.1. Transaction management in a microservice architecture

4.2. Coordinating sagas

4.3. Handling the lack of isolation

4.4. The design of the Order Service and the Create Order Saga