Sagas

Steps that can be undone · Part of Databases

Key idea

Practice

6 questions · about 3 min

Explained step by step

Saga

A saga is a sequence of local transactions. Each one updates its own database and publishes a message or event that triggers the next. If one fails because it breaks a business rule, the saga runs compensating transactions that undo what the earlier ones did. It's like planning a trip step by step: you book the flight, then the hotel, then the car; if the car fails, you cancel the hotel and the flight.

The difference from 2PC is that in a saga each step commits right away in its own database, without waiting for the others. There's no global 'everyone ready'. That's why it doesn't block: each service carries on. The cost is that, for a while, some steps are done and others aren't. It's like a relay race: each runner runs their leg and passes the baton, without waiting for everyone to be on the track.

Sagas have problems. They're hard to debug, because the flow is spread over several services. There can be cyclic dependencies between participants. Since there's no isolation between them, others can see data halfway through, which complicates durability. And testing is hard, because every service must be running to simulate a transaction. It's like debugging an email chain among 6 people.

2PC and sagas solve the same problem in opposite ways. 2PC locks everyone until everyone is ready: consistent but blocking. A saga lets each step commit alone and fixes things with compensations if something fails: no blocking, but there's an inconsistent window. It's like paying as a group: either everyone waits until all cards are out (2PC), or each pays their share and accounts are settled later (saga).

Compensating transaction

A compensating transaction undoes the effect of an earlier saga step. It's not a database rollback: it's a new operation doing the opposite, like refunding a payment or releasing a booking. They run in reverse order. It's like canceling a trip: you return the car, cancel the hotel and ask for the flight refund. If step 3 of 3 fails, steps 2 and 1 are compensated.

Choreography

In choreography there's no director: each service, after finishing its local transaction, publishes an event, and the other services listen and react. Each knows what to do when it sees a certain event. It's like a circle dance: nobody gives orders, each moves when they see their neighbor move. It works well with few steps; with 10 services, following the flow gets complicated.

Orchestration

In orchestration there's an orchestrator that tells each participant which local transaction to run and in what order. If something fails, the orchestrator decides what to compensate. It's like an orchestra conductor: musicians play when they cue. The flow lives in 1 single place, easier to follow, but the orchestrator is one more piece to maintain.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Steps that can be undone