Pattern Playbook

Course › Day 1 · Launch week › Scene 4 of 49 · 10 min

Two guests, same night, same second

Transactions, lockForUpdate(), Cache::lock(), why SQLite hides the bug, a real concurrency test

On this page
  1. The incident
  2. The broken code
  3. Why not a unique index?
  4. The fix: lock the property row
  5. The alternative: a Redis lock
  6. Why every test stayed green
  7. Laravel inside
  8. Tests
  9. Remember

#incidentsThursday 19:47

Alert · bookingsTwo bookings for Nyhavn flat, 10–13 July. Created at 19:46:58.112 and 19:46:58.116.

Sara · tech leadThe overlap check from yesterday is in place and every test is green. So how did both get through?

The broken code

Nothing in yesterday’s code is wrong on its own. The problem is the gap between two lines:

app/Bookings/CreateBooking.php (from scene 3)the bug
if ($property->bookings()->overlapping($stay)->exists()) {   // 1. check
    throw BookingConflict::for($stay);
}
// ... a few milliseconds ...
$property->bookings()->save($booking);                        // 2. insert

Two requests run this at the same time, each in its own PHP worker:

TimeRequest A (Mona)Request B (Omar)
.112checks: no overlap ✓
.113checks: no overlap ✓ (A hasn’t saved yet)
.114inserts its booking
.116inserts its booking

Each request saw a free calendar, because each one checked before the other saved. This is a race condition: the result depends on timing, so it never shows up when you click through the app alone, and only appears under real traffic.

Why not a unique index?

For "one row per X", a unique index is the best backstop there is: the database refuses the second insert no matter what the code does. But overlap isn’t equality. 10–13 July and 11–14 July are different rows that share two nights, and MySQL has no index that can say "these ranges must not overlap". (PostgreSQL can, with an exclusion constraint. MySQL can’t.)

So the fix is to make the check and the insert happen one request at a time per property.

How the fix works, in one picture

Before

request Arequest Bfree?free?yesyesinsertinsertboth checked before either inserted

Two requests in the same millisecond both ask “free?” before either has inserted. Both hear yes.

After

request Arequest Blockwait…free? yesfree? noinsert409commit releases the lock

The first request locks the property row. The second waits, then checks again and finds the nights taken.

The fix: lock the property row

app/Bookings/CreateBooking.php (after)changed since scene 3: +17 −12
<?php namespace App\Bookings; use App\Enums\BookingStatus;use App\Models\Booking;use App\Models\Property;use Illuminate\Support\Facades\DB; final class CreateBooking{    public const HOLD_MINUTES = 30;     /** @param array{guest_name: string, guest_email: string, guests: int} $guest */    public function handle(Property $property, Stay $stay, array $guest): Booking    {        if ($property->bookings()->overlapping($stay)->exists()) {            throw BookingConflict::for($stay);        }         $booking = new Booking($guest);        $booking->stay = $stay;                                                // scene 2        $booking->total = $property->nightly_rate->times($stay->nights());     // scene 1        $booking->status = BookingStatus::Pending;        $booking->expires_at = now()->addMinutes(self::HOLD_MINUTES);        $property->bookings()->save($booking);                                 // sets property_id         return $booking;        return DB::transaction(function () use ($property, $stay, $guest): Booking {            Property::whereKey($property->id)->lockForUpdate()->firstOrFail();   // others wait here             if ($property->bookings()->overlapping($stay)->exists()) {                throw BookingConflict::for($stay);            }             $booking = new Booking($guest);            $booking->stay = $stay;                                                // scene 2            $booking->total = $property->nightly_rate->times($stay->nights());     // scene 1            $booking->status = BookingStatus::Pending;            $booking->expires_at = now()->addMinutes(self::HOLD_MINUTES);            $property->bookings()->save($booking);                                 // sets property_id             return $booking;        }, attempts: 3);                                                   // retried on a deadlock    }}

lockForUpdate() sends SELECT … FOR UPDATE. MySQL gives this transaction an exclusive lock on the property’s row. A second request for the same property reaches the same line and waits until the first transaction commits. When it continues, the first booking is there, the check sees it, and the guest gets a 409. Requests for other properties don’t wait at all.

Why lock the property and not the bookings? The rows that would clash don’t exist yet, and you can’t lock a row that isn’t there. The property row is the one thing both requests share.

Run it again

TimeRequest A (Mona)Request B (Omar)
.112locks the flat, checks: free ✓
.113tries to lock the flat… waits
.114inserts, commits, lock released
.115gets the lock, checks: taken → 409

The alternative: a Redis lock

When there is no shared row to lock (the work spans two databases, or calls an API in the middle), an atomic lock in Redis does the same job. block(5) waits up to 5 seconds for the lock; the 10 is a safety expiry, so a crashed worker can’t hold it forever.

an alternative to the row lock (not used here)
return Cache::lock("property:{$property->id}:booking", 10)->block(5, function () use ($property, $stay, $guest): Booking {
    // the same check and insert, inside DB::transaction()
});

The row lock is the better fit here: it lives and dies with the transaction, needs nothing extra running, and can’t expire halfway through. A Redis lock that expires while the work is still running lets the next request in, and Redis has to be up for anyone to book.

Why every test stayed green

A normal test sends one request at a time, so the race can’t happen in it. And on SQLite it can’t happen at all: SQLite lets only one writer touch the whole database at a time, and it ignores FOR UPDATE. A test suite on SQLite would pass with the lock and without it. That’s why the foundation runs tests on MySQL, and why the test below starts two real processes.

Laravel inside

lockForUpdate() is one line of SQL

The query builder only records that you want a lock; the grammar of each database turns it into SQL. The SQLite grammar returns an empty string, which is the silent no-op from above.

Illuminate/Database/Query/Grammars/MySqlGrammar.php
protected function compileLock(Builder $query, $value)
{
    if (! is_string($value)) {
        return $value ? 'for update' : 'lock in share mode';   // lockForUpdate() / sharedLock()
    }

    return $value;
}

DB::transaction() retries deadlocks

With attempts: 3, a transaction that fails because of a deadlock (or another error the database reports as a concurrency conflict) is rolled back and run again from the start. Anything else is rolled back and rethrown at once, including a lost connection: if the connection drops during the commit, the commit may already have happened, and running the closure again could book the nights twice. That case is handled by the unique key and the idempotency of the caller, not by a retry.

Illuminate/Database/Concerns/ManagesTransactions.php (simplified)
public function transaction(Closure $callback, $attempts = 1)
{
    for ($currentAttempt = 1; $currentAttempt <= $attempts; $currentAttempt++) {
        $this->beginTransaction();

        try {
            $result = $callback($this);
        } catch (Throwable $e) {
            $this->handleTransactionException($e, $currentAttempt, $attempts);   // retries only concurrency errors
            continue;
        }

        $this->commit();

        return $result;
    }
}
TestsTwo real processes book the same nights at the same moment

This test forks two child processes that start at the same instant and both try to book 10–13 July. With the lock, exactly one booking exists afterwards. Remove lockForUpdate() and it goes red on most runs; ->repeat(20) runs it twenty times so a lucky timing can’t hide the bug.

It can’t use RefreshDatabase: that wraps each test in a transaction the child processes can’t see. It uses DatabaseTruncation, which empties the tables between tests instead, so it lives in its own folder.

tests/Concurrency/CreateBookingRaceTest.phpnew file
<?php

use App\Bookings\BookingConflict;
use App\Bookings\CreateBooking;
use App\Bookings\Stay;
use App\Models\Booking;
use App\Models\Property;
use Illuminate\Foundation\Testing\DatabaseTruncation;
use Illuminate\Support\Facades\DB;

uses(Tests\TestCase::class, DatabaseTruncation::class);

it('lets only one of two simultaneous requests book the same nights', function () {
    $property = Property::factory()->create();
    $startAt = microtime(true) + 0.5;                       // both children start at this instant
    $children = [];

    DB::disconnect();                                       // no open connection to share: each process opens its own

    foreach (['Mona', 'Omar'] as $guest) {
        $pid = pcntl_fork();
        if ($pid === 0) {                                   // in the child process
            time_sleep_until($startAt);
            try {
                app(CreateBooking::class)->handle($property, Stay::fromDates('2027-07-10', '2027-07-13'), [
                    'guest_name' => $guest, 'guest_email' => strtolower($guest) . '@example.com', 'guests' => 2,
                ]);
            } catch (BookingConflict) {
                // the expected outcome for the slower child
            }
            posix_kill(getmypid(), SIGKILL);                // exit without running the test runner's shutdown
        }
        $children[] = $pid;
    }

    foreach ($children as $pid) {
        pcntl_waitpid($pid, $status);
    }

    expect(Booking::where('property_id', $property->id)->count())->toBe(1);
})->repeat(20);

Include the folder in the full suite

tests/Pest.php applies RefreshDatabase to Feature and Unit only, so this folder sets up its own traits. Add it to phpunit.xml so ./vendor/bin/pest runs it with everything else. It needs the pcntl and posix extensions from the PHP image.

phpunit.xmlchanged since the foundation: +3
10 unchanged lines<?xml version="1.0" encoding="UTF-8"?><phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true"> <testsuites> <testsuite name="Unit"> <directory>tests/Unit</directory> </testsuite>
<testsuite name="Feature"> <directory>tests/Feature</directory> </testsuite> <testsuite name="Concurrency"> <directory>tests/Concurrency</directory> </testsuite> </testsuites> <source> <include>
14 unchanged lines <directory>app</directory> </include> </source> <php> <env name="APP_ENV" value="testing"/> <env name="DB_CONNECTION" value="mysql"/> <env name="DB_DATABASE" value="pms_test"/> <env name="QUEUE_CONNECTION" value="sync"/> <env name="CACHE_STORE" value="array"/> <env name="SESSION_DRIVER" value="array"/> <env name="MAIL_MAILER" value="array"/> <env name="BCRYPT_ROUNDS" value="4"/> </php></phpunit>

Remember

  • A check-then-insert is a race: lock what you check (lockForUpdate() in a transaction) or let a unique key decide.
  • Prove a race with a real concurrent test on the real database; SQLite hides it.

Bookings are safe. Friday morning, finance finds 312 payments in the live Stripe account, made by our own test suite.