โ Back to Sagas and distributed transactions
๐งณ Sagas and distributed transactions: cheat sheet
๐จ The analogyBooking a flight, hotel and car with three companies, and cancelling the earlier bookings if a later one falls through.
๐ The regular explanationKeeping data consistent across services that each own their database. Two-phase commit locks everyone until a coordinator decides; a saga runs a chain of local transactions and undoes the finished ones with compensating actions when a later step fails.
- You book the flight first. The airline writes it in its own booking book and says "done".
- Then the hotel. Another company, another booking book, another "done".
- Now the car. The car company says "sorry, none left".
- The flight and hotel are still booked. Nobody can undo them for you, so you're left with half a trip.
- The agent rings all three companies and asks each to hold a place for you.
- All three say "yes, it's held". They've promised to wait for the agent's answer.
- Then the agent's phone dies, before the "confirm" call. The companies can't confirm and can't cancel. They just wait.
- Another traveller wants that hotel room. The hotel has to say no, because the room is held for a call that may never come.
- When the agent's phone works again, the agent checks their notes and rings round to confirm. The holds become bookings, and the waiting is over.
- The agent books the flight. It's real now, and the agent writes it in their notebook.
- Next the hotel. Also real, also written down.
- The car company says "none left". The plan can't go on.
- So the agent cancels, newest first. The hotel cancels your room and sends you a cancellation note.
- Then the airline cancels and gives your money back. You still got two emails, the booking and the cancellation, because cancelling is a new action, not an eraser.
- There's no agent this time. You pin a note on the town notice board, "trip wanted".
- The airline sees it, books a seat and pins up "flight booked".
- The hotel sees "flight booked", books a room and pins up "hotel booked".
- The car company sees "hotel booked", but every car is gone. It pins up "no cars".
- The hotel and the airline both see "no cars" and cancel on their own. Nobody told them to; they know the routine, and they pin up that they've cancelled.
- The agent books your hotel room, but the hotel writes it in pencil, marked "held for trip 42".
- Another traveller asks for that room. The hotel gives an honest answer, "it's on hold, try again soon", instead of selling it twice or calling it booked.
- Meanwhile the agent books the car, and this time there is one.
- The whole trip worked, so the agent rings the hotel to turn the pencil into ink.
- Now the other traveller gets a clear answer straight away. That room is taken, so they pick another one.
- The clerk writes your room in the booking book and, in the same go, puts the note to send into the outbox tray.
- Both are written together, or neither is. There's no way to have the room without the note.
- The messenger picks up the note and pins it on the town notice board.
- Oops! The messenger trips before ticking the note off.
- Back on their feet, the messenger sees an unticked note and pins it up again. Now it's there twice, so readers check the trip number and ignore the repeat.
๐งฉ Key questions
- The agent has heard "yes, it's held" from all three companies, then their phone dies before the confirm call. What do the companies do?
- They wait with the places held, unable to confirm or cancel, until the agent calls back. Each company only knows its own answer, so neither confirming nor cancelling is safe. That waiting is the weak spot.
- Your flight and hotel are booked when the car falls through. What does the plan do next?
- Cancels the hotel, then the flight, newest first. The bookings were real, so they're put right with new cancel actions, not erased.
- While your trip is half booked, a friend checks the hotel. What stops them being told the wrong thing?
- Writing the room in pencil, marked as held for your trip, until the trip is confirmed. Pencil tells everyone the room is on hold, not free and not finally booked.
- Why does the hotel write the note into the outbox tray on the same page as the booking, instead of pinning it up straight away?
- So a stumble between booking and pinning can't leave a booked room nobody hears about. Both are written together. The messenger may pin a note twice, so readers ignore repeats.
- The agent asks the hotel to book your room but the phone line goes silent, with no answer. What should the agent do?
- Ask the hotel whether trip 42 is booked, then carry on or cancel depending on the answer. Silence doesn't mean yes or no. Checking first avoids cancelling a booking that never existed, or leaving one behind.