Files Checkpoint

Files Checkpoint

Hands-on Checkpoint

Save bookings to a JSON file that survives five customers booking the same slot at the same moment — with a lock, an atomic write and a real race test — on your own computer.

40–55 min

The Challenge

Until the database arrives in Act 18, data/bookings.json is BookBay's only record of who booked what. Marcus wants proof that it can't lose a booking or give one slot to two customers — not a promise, a test that starts five bookings at the same instant.

This checkpoint runs on your own computer, because locks only matter between real processes, and the PHP Playground runs one script at a time. You need PHP 8.2 or newer in a terminal. First build the simple version and watch the test fail; then make it safe.

What You Need to Do

  • Create a folder with src/BookingFile.php, bin/book.php, bin/race-test.php and an empty data/ folder, and build every path from __DIR__ (chapter 1).
  • In BookingFile, write all(), which returns the bookings from data/bookings.json (an empty array if the file doesn't exist yet), reading with JSON_THROW_ON_ERROR (chapters 2 and 5).
  • Write add(string $customer, string $slot): int, which throws a SlotTakenException if the slot is already booked, and otherwise saves the booking with the next id (starting at 4811) and returns that id. Keep a usleep(100_000) just before saving, to stand in for real work (chapter 2).
  • Write bin/book.php, which books the customer and slot given as its two arguments and prints either "booked #id" or the exception's message (chapters 3 and 4 of Act 14).
  • Write bin/race-test.php, which deletes the data file, starts book.php five times at once with proc_open() — five customers, the same slot — waits for all of them, prints their answers, and prints how many bookings the file really contains. Run it against your first version and note the result (chapter 6).
  • Make add() safe: hold an exclusive lock on a separate bookings.json.lock file for the whole read-check-write, and write the new content atomically with a temporary file in the same folder and rename() (chapter 6).
  • Run the race test several times: every run must print exactly one "booked" line, four "already booked" lines, and one booking in the file. No temporary files may be left in data/.
Stuck? A Few Hints
  • proc_open([PHP_BINARY, __DIR__ . '/book.php', $customer, $slot], [1 => ['pipe', 'w']], $pipes) starts a process without waiting; read $pipes[1] with stream_get_contents() afterwards (chapter 4).
  • If the safe version still lets two customers book the slot, check that you read the file after flock(), not before (chapter 6).
  • Release the lock in a finally block, so a SlotTakenException doesn't leave the lock held (Act 14).
  • tempnam(dirname($path), 'bookings-') keeps the temporary file on the same disk, so rename() is atomic (chapter 6).
  • On Windows, start the test from PowerShell or Git Bash — proc_open() works the same on every system.

Before You Move On

If five simultaneous bookings always produce exactly one, you've solved one of the hardest problems in web development — concurrent writes — with files. The same idea, making competing operations take turns, is what database transactions do at scale in Act 18. Next: HTTP. Customers can finally book from a real form — and a customer who refreshes the page after booking is about to book the same slot twice.