Managing data consistency in a microservice architecture using Sagas - part 1
sagas transaction managementThis is the first in a series of posts that expands on my recent MicroCPH talk on Managing data consistency in a microservice architecture using Sagas (slides, video).
The other posts in this series are:
Why sagas?
A distinctive characteristic of the microservice architecture is that in order to ensure loose coupling each service’s data is private. Unlike in a monolithic application, you no longer have a single database that any module of the application can update. As a result, one of the key challenges that you will face is maintaining data consistency across services.
Consider, for example, the customers and orders example application from my presentation. It consists of two services:
Order Service
- Manages orders
- Operations include
createOrder()
andcancelOrder()
Customer Service
- Manages customer information including the customer’s available credit
- Operations include
createCustomer()
When the Order Service
creates an Order
it must ensure that there is sufficient credit available.
Specifically, the createOrder()
command must update data in both the Order Service
and the Customer Service
.
In a traditional application, you might consider using distributed transactions a.k.a. two phase commit (2PC). However, using 2PC is generally a bad idea a microservice architecture. It’s a form of synchronous communication that results in runtime coupling that significantly impacts the availability of an application.
What is a saga?
The solution is to implement commands, such as createOrder()
, using a saga.
A saga is a sequence of local transactions in each of the participating services.
For example, here is the definition of the Create Order Saga
, which is initiated by the createOrder()
command:
Step | Participant | Transaction | Compensating Transaction |
---|---|---|---|
1 | Order Service |
createPendingOrder() |
rejectOrder() |
2 | Customer Service |
reserveCredit() |
- |
3 | Order Service |
approveOrder() |
- |
The purpose of each step is as follows:
createPendingOrder()
- create theOrder
in aPENDING
statereserveCredit()
- attempt to reserve creditapproveOrder()
- change the state of theOrder
toAPPROVED
rejectOrder()
- change the state of theOrder
toREJECTED
The sequence for the happy path is as follows:
Order Service
:createPendingOrder()
Customer Service
:reserveCredit()
Order Service
:approveOrder()
The sequence for the path when there is insufficient credit is as follows:
Order Service
:createPendingOrder()
Customer Service
:reserveCredit()
Order Service
:rejectOrder()
What are compensating transactions?
The rejectOrder()
command is an example of a compensating transaction.
Unlike ACID transactions, sagas cannot automatically undo changes made by previous steps since those changes are already committed.
Instead, you must write compensating transactions that explicitly undo those changes.
Each step of a saga that is followed by a step that can fail (for business reasons) must have a corresponding compensating transaction.
In the Create Order Saga
, createOrder()
has the rejectOrder()
compensating transaction because the reserveCredit()
step can fail.
The reserveCredit()
step does not need a compensating transaction because the approveOrder()
step cannot fail.
And, the approveOrder()
step does not need a compensating transaction because it’s the last step of the saga.
What is the semantic lock counter-measure?
You might be wondering why createOrder()
creates the order in a PENDING
state, which is then changed to APPROVED
by approveOrder()
.
The use of a PENDING
state is an example of what is known as a semantic lock counter-measure.
It prevents another transaction/saga from updating the Order
while it is in the process of being created.
To see why this is necessary consider the following scenario where the cancelOrder()
command is invoked while the Order
is still being created:
Create Order Saga | Cancel Order Saga |
---|---|
createOrder() - state=CREATED |
|
cancelOrder() - state=CANCELLED |
|
reserveCredit() |
|
approveObject() - state=APPROVED |
In this scenario, the cancelOrder()
command changes the status of the order to CANCELLED
, and the approveOrder()
command overwrites that change by setting the status to APPROVED
.
The customer would be quite surprised when the order is delivered!
The PENDING
state prevents this problem.
The cancelOrder()
command will only cancel an Order
if its state is APPROVED
.
If the state is PENDING
, cancelOrder()
returns an error to the client indicating that it should try again later.
The semantic lock counter-measure is a kind of application-level locking.
As I describe in the presentation, it’s a way to make sagas, which are inherently ACD, ACID again.
In a later post, I’ll describe how to implement this saga.
- Read the other posts in this series:
- Read my Microservices patterns book, which includes a comprehensive discussion of sagas
- Read or watch MicroCPH talk on Managing data consistency in a microservice architecture using Sagas (slides, video)
- Talk to me about my microservices consulting and training services.
- Learn more about microservices at adopt.www.prc.education