Skip to content
tezvyn:

The Saga Pattern: Transactions Across Microservices

Source: learn.microsoft.comHardHow cards are made

The Saga Pattern: Transactions Across Microservices

The Saga pattern manages transactions across services by chaining local operations. If a step fails, compensating actions undo previous work. It's common in booking systems.

Why it exists

In a microservices architecture, each service typically owns its database. This makes it impossible to use a traditional, all-or-nothing ACID transaction across multiple services. The Saga pattern was created to manage data consistency for operations that span these distributed systems, ensuring a process either fully completes or is fully undone.

The mental model

Think of a saga as a business process with a built-in contingency plan. Each step in the process is a local transaction within a single service, and each step has a corresponding "undo" action, called a compensating transaction. If you book a flight and the subsequent hotel booking fails, the saga automatically runs the "cancel flight" action to roll back the entire operation.

How it works

A saga breaks a large distributed transaction into a sequence of smaller, local transactions. Each local transaction completes its work atomically within its own service, updates its database, and then triggers the next transaction, usually via an event or message. If any transaction in the sequence fails, the saga executes compensating transactions in reverse order to undo the changes made by the preceding successful transactions. This ensures the system eventually returns to a consistent state.

When to use it

Use the Saga pattern for long-running business processes that need to coordinate multiple services, like an e-commerce checkout that involves payment, inventory, and shipping services. It's ideal when you need eventual consistency and can't lock resources for a long time, making a traditional two-phase commit impractical.

When not to use it

Avoid sagas if you require strict ACID guarantees, especially isolation. Because a saga commits local transactions along the way, other services can see an intermediate, inconsistent state before the entire process succeeds or is rolled back. If this is unacceptable, a saga is the wrong choice. For simple workflows contained within a single service, a standard database transaction is better.

One canonical example

Booking a vacation package involves a flight service, a hotel service, and a car rental service. The saga starts by booking the flight. On success, it triggers the hotel booking. If the hotel booking succeeds, it triggers the car rental. If the car rental fails, the saga executes compensating transactions: it first cancels the hotel booking, and then it cancels the flight booking, leaving the system in its original state with no partial bookings.

Interview question

Which statement accurately describes how the Saga pattern manages data consistency in a distributed system?

  • a.It ensures immediate consistency by holding distributed locks on all involved resources until the entire process completes.
  • b.It maintains strong consistency by replicating data across all services, making all changes immediately visible.
  • c.It achieves eventual consistency by executing compensating transactions to undo changes if any step in the process fails.Correct
  • d.It enforces strict ACID properties across all participating microservices through a two-phase commit.
Why?

The Saga pattern is designed for eventual consistency, where local transactions commit independently and compensating transactions are used to roll back the overall process if a step fails. It explicitly avoids strict ACID guarantees and long-held locks across services, which are characteristic of traditional distributed transactions like two-phase commit.

Just read this? Test yourself on what you have been reading.

Read the original → learn.microsoft.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on distributed systems — each one lists the topics its interview covers.

See open roles