Understanding the Saga Pattern for Distributed Transactions in Microservices
The article explains why traditional two‑phase commit fails in microservice architectures, introduces the Saga pattern as a solution, compares choreography and orchestration implementations, illustrates a step‑by‑step e‑commerce example, and discusses rollback handling, benefits, and drawbacks.
Two‑phase commit is one of the most powerful transaction types, where the commit of the first transaction depends on the completion of the second; it is useful when multiple entities must be updated atomically, such as confirming an order and updating inventory.
In microservice environments, each service has its own database, making local two‑phase commit impractical and turning traditional RDBMS into a poor storage choice; many companies therefore adopt NoSQL databases for higher performance.
The article presents a high‑level microservice architecture for an e‑commerce system and explains that a single ACID transaction cannot cover order placement, payment, inventory update, and delivery, necessitating a distributed transaction.
It introduces the Saga pattern—first described in 1987—as a popular solution for distributed transactions, consisting of a series of local transactions coordinated either by event‑driven choreography or by a central orchestrator.
Choreography (Event‑driven) implementation: each service performs its local transaction and publishes an event; other services listen to these events and trigger their own transactions. The process ends when the last service completes without publishing further events.
In the e‑commerce example, the flow is:
Order service creates an order, sets status to pending, and publishes ORDER_CREATED_EVENT .
Payment service listens, charges the customer, and publishes BILLED_ORDER_EVENT .
Stock service updates inventory and publishes ORDER_PREPARED_EVENT .
Delivery service ships the product and publishes ORDER_DELIVERED_EVENT .
Order service marks the order as completed.
Rollback in a distributed saga is not free; compensating transactions are required. For example, if the stock service fails, it emits PRODUCT_OUT_OF_STOCK_EVENT , prompting the payment service to refund the customer and the order service to mark the order as failed.
Defining a shared transaction ID is crucial so that all listeners can correlate events to the same saga.
Advantages of choreography: simple, easy to understand, loosely coupled, and suitable for short sagas with 2‑4 steps.
Disadvantages of choreography: can become chaotic as steps increase, hard to track event listeners, may introduce circular dependencies, and testing requires running all services.
The article promises a follow‑up discussing the orchestration (command) approach to address many of these challenges.
Architects Research Society
A daily treasure trove for architects, expanding your view and depth. We share enterprise, business, application, data, technology, and security architecture, discuss frameworks, planning, governance, standards, and implementation, and explore emerging styles such as microservices, event‑driven, micro‑frontend, big data, data warehousing, IoT, and AI architecture.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.