Guided challenge · About 15 minutes · Optional Python lab · Distributed Systems
The Flight Was Booked. The Hotel Wasn't.
Maya booked a travel package. Her flight was confirmed, but the hotel had no room.
Answers stay in this browser until you save them on this device.
What happened?
Maya chooses a package with one flight seat and one hotel room.
The flight system confirms her seat.
The hotel then reports that no rooms are available.
Maya now has a flight without a hotel. The two systems use separate databases.
Panel 1
Before
Maya
$100
Arjun
$20
Panel 2
Subtract and save
Maya loses $30 and that change is saved on its own.
Panel 3
After (broken)
Maya
$70
Arjun
$20
Transfer stopped halfway
Your task
How would you make both systems agree before confirming the package?
For this challenge
- Both accounts are stored in the same database.
- All amounts are US dollars, stored as whole cents in the code.
- There are no fees, exchange rates, overdrafts, external banks, or payment providers.
- Each customer request is submitted once. Lost replies and repeated submissions are a later lesson.
- Use only fictional accounts and local data.
Optional hints
- One system cannot undo changes in another system's database.
- Ask each system to hold its resource before confirming the package.
- The final decision might already be saved, even if its reply was lost.
Related path: System Design Interview Sprint
Interview answer
Explain it in an interview
Maya's package confirmed flight 101 and then hotel 201 had no room. How do you keep the flight and the hotel together?
Maya chose one flight seat and one hotel room as trip 4821, using flight 101 and hotel 201. The flight system confirmed her seat. The hotel then said no room was available. She had a flight without a hotel, and the two bookings live in separate databases.
Step 1
The flight committed before the hotel voted
The coordinator finished the flight reservation and only then asked the hotel, which refused. There was no shared decision, so the seat on flight 101 stayed taken. Separate database files cannot undo that seat with one local commit. Maya was left holding half of the package.
Step 2
An app error does not free a committed seat
Once the flight has committed, a failure on the hotel call does not release the seat. Retrying the whole trip can reserve a second seat. I need both participants to promise they can commit before either one finalizes. Showing Maya an error while flight 101 stays booked repeats the bug.
Step 3
Prepare both, then commit or abort
I ask flight 101 and hotel 201 to prepare one unit for trip 4821. A yes vote stores a prepared reservation that counts against capacity. If the hotel votes no, or a prepare reply is lost before I have a decision, I abort. I commit only when both votes are yes and that decision is recorded.
Step 4
Write the decision before you finish
The coordinator's decision starts unset while prepare is in progress. Each participant vote is saved with the transaction. Prepared and committed rows consume capacity, and aborted rows do not. After I write commit or abort, a repeat of that decision for trip 4821 does not take a second seat or a second room.
Step 5
Recovery repeats the recorded decision
If I crash before a decision exists, recovery aborts trip 4821 so a prepared flight is not left reserved under this lab's rule. If I crash after commit is saved, recovery tells any participant that missed the message to commit. I do not allocate a new transaction id to clean up the first one.
Step 6
This is not a database XA transaction
The protocol is application code over three local SQLite files. It is not XA, and it is not a native distributed prepare inside the database engine. If the coordinator is gone for good, a participant can remain blocked in prepared. There are no real airlines, hotels, or payments in the lab.
Do not stop here
I would confirm flight 101 first, and if hotel 201 is full I would show an error and leave the seat booked.
Say this boundary
I prepare flight 101 and hotel 201 for trip 4821, and I abort both when the hotel cannot prepare a room.
Leave these claims out
- This is a database XA transaction
- The coordinator can never leave a participant blocked
- A lost prepare reply means the flight already committed
I would tell trip 4821 as prepare, then a hotel no, then abort on both sides. Recovery repeats the decision that was saved, instead of booking the package a second time.
Now say the same idea in your own words, using the story from this challenge.