TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† 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.
One trip, three databasesStep 4 of 4
๐Ÿง‘Youhalf a tripโœˆ๏ธAirlinebooked๐ŸจHotelbooked๐Ÿš—Car hiresold out
  1. You book the flight first. The airline writes it in its own booking book and says "done".
  2. Then the hotel. Another company, another booking book, another "done".
  3. Now the car. The car company says "sorry, none left".
  4. The flight and hotel are still booked. Nobody can undo them for you, so you're left with half a trip.
Two-phase commit blocksStep 5 of 5
๐Ÿง‘โ€๐Ÿ’ผTravel agentโœˆ๏ธAirlinebooked๐ŸจHotelbooked๐Ÿš—Car hirebooked๐ŸงณAnother traveller
  1. The agent rings all three companies and asks each to hold a place for you.
  2. All three say "yes, it's held". They've promised to wait for the agent's answer.
  3. Then the agent's phone dies, before the "confirm" call. The companies can't confirm and can't cancel. They just wait.
  4. Another traveller wants that hotel room. The hotel has to say no, because the room is held for a call that may never come.
  5. 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.
A saga undoes with compensationsStep 5 of 5
Agent's notebookflikey flight booked: flight0hotkey hotel booked: hotel1carkey car rejected: car failed2x-hkey hotel cancelled: x-hotel3x-fkey flight cancelled: x-flightโœฆโœฆโœฆโœฆโœฆ4๐Ÿง‘โ€๐Ÿ’ผTravel agentโœˆ๏ธAirlinerefunded๐ŸจHotelcancelled๐Ÿš—Car hiresold out
  1. The agent books the flight. It's real now, and the agent writes it in their notebook.
  2. Next the hotel. Also real, also written down.
  3. The car company says "none left". The plan can't go on.
  4. So the agent cancels, newest first. The hotel cancels your room and sends you a cancellation note.
  5. 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.
Choreography with eventsStep 5 of 5
Town notice boardreqkey TripRequested: req0flikey FlightBooked: flight1hotkey HotelBooked: hotel2carkey CarRejected: car failed3x-hkey HotelCancelled: x-hotelโœฆโœฆโœฆโœฆโœฆ4x-fkey FlightCancelled: x-flightโœฆโœฆโœฆโœฆโœฆ5๐Ÿง‘Youโœˆ๏ธAirlinerefunded๐ŸจHotelcancelled๐Ÿš—Car hiresold out
  1. There's no agent this time. You pin a note on the town notice board, "trip wanted".
  2. The airline sees it, books a seat and pins up "flight booked".
  3. The hotel sees "flight booked", books a room and pins up "hotel booked".
  4. The car company sees "hotel booked", but every car is gone. It pins up "no cars".
  5. 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.
Pending states as a semantic lockStep 5 of 5
๐Ÿง‘โ€๐Ÿ’ผTravel agent๐ŸจHotelconfirmed๐Ÿš—Car hirebooked๐ŸงณAnother traveller
  1. The agent books your hotel room, but the hotel writes it in pencil, marked "held for trip 42".
  2. 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.
  3. Meanwhile the agent books the car, and this time there is one.
  4. The whole trip worked, so the agent rings the hotel to turn the pencil into ink.
  5. Now the other traveller gets a clear answer straight away. That room is taken, so they pick another one.
The transactional outboxStep 5 of 5
๐Ÿข Hotel officecommittedBooking bookt42key trip 42 room 12: t420123Outbox traye1key HotelBooked trip 42: e10123sentTown notice boarde1key HotelBooked trip 42: e10e1key HotelBooked trip 42 again: e1โœฆโœฆโœฆโœฆโœฆ123๐ŸจHotel clerk๐ŸƒMessenger
  1. The clerk writes your room in the booking book and, in the same go, puts the note to send into the outbox tray.
  2. Both are written together, or neither is. There's no way to have the room without the note.
  3. The messenger picks up the note and pins it on the town notice board.
  4. Oops! The messenger trips before ticking the note off.
  5. 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.