BayLearnBayAreaLa8s

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.

Program stops here

Panel 3

After (broken)

Maya

$70

Arjun

$20

Transfer stopped halfway

Maya has $100 and Arjun has $20. After a failed half-transfer, Maya shows $70 but Arjun still has $20 — $30 is missing from the story the accounts tell together.

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
  1. One system cannot undo changes in another system's database.
  2. Ask each system to hold its resource before confirming the package.
  3. The final decision might already be saved, even if its reply was lost.
0/4000
0/4000
0/4000

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.

How to walk the interviewer through the answer1The flight committed before the hotel voted2An app error does not free a committed seat3Prepare both, then commit or abort4Write the decision before you finish5Recovery repeats the recorded decision6This is not a database XA transaction
Walk these steps in order. Each box is one thing to say before you move on.
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

Interviewers listen for the boundary between the tempting shortcut and the rule you actually enforce.

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.