Introduction
In a microservices architecture, each service usually owns its own database. This approach improves scalability and loose coupling, but it introduces a major challenge:
How do we keep data consistent when a single business operation updates multiple services?
In a monolithic application, this problem is easy to solve using a single ACID database transaction. In microservices, however, a single transaction cannot span multiple databases.
This is where the SAGA pattern is used.
In this article, you will learn:
Why distributed transactions are difficult in microservices
Why two-phase commit (2PC) is not recommended
What the SAGA pattern is and how it works
Compensating transactions
Choreography vs orchestration
A real-world create order example
Key points to remember
From Monolithic to Microservices
Why Transactions Are Easy in Monolithic Applications
In a monolithic architecture, most operations run inside a single database transaction.
Example: CreateOrder()
Check customer
Validate order
Deduct stock
Process payment
Save order
All these steps run in the same database, allowing:
COMMIT on success
ROLLBACK on failure
This approach is simple and reliable.
Why Distributed Transactions Are Hard in Microservices
In microservices, the same business operation is spread across multiple services.
Example: Create Order workflow
Order Service (Order DB)
Customer Service (Customer DB)
Inventory Service (Inventory DB)
Payment Service (Payment DB)
Each service owns its data, and the data is distributed.
The challenge becomes maintaining consistency when each service commits independently.
Why Not Use Distributed Transactions (2PC / XA)
A traditional solution is the two-phase commit (2PC) protocol.
In theory, it works as follows:
All services prepare to commit
All services commit together
If one fails, all roll back
In real microservices systems, this approach causes serious problems.
1. Technology Limitations
Many modern platforms and tools do not fully support XA transactions, including:
Kafka
RabbitMQ
Many NoSQL databases
2. Availability Issues
If a transaction spans four services, all four must be available at the same time. This significantly reduces system availability.
3. Performance and Locking
Distributed transactions lock resources for extended periods, leading to poor performance and reduced scalability.
For these reasons, 2PC is generally avoided in modern microservices.
What Is the SAGA Pattern?
The SAGA pattern is a microservices design pattern used to maintain data consistency without relying on distributed transactions.
A SAGA breaks a long business transaction into a sequence of local transactions.
Each step:
Executes within a single microservice
Commits changes to its own database
Publishes an event or sends a command to trigger the next step

Join the conversation! Your thoughts help the community grow.