By Chason ArthurRevision 118.063Published Updated

PHP / Laravel components / SQL transactions

Sell the last item once.

One item. Two shoppers. A repeated request. Follow the database records through the failure, then try the same event with a stronger design.

The idea in plain language

A transaction keeps related database writes together. A guarded update protects availability. An idempotency key identifies a repeated decision. These mechanisms solve different parts of the same ordering problem.

The browser executes actual PHP and SQLite through the open-source PHP.wasm runtime from WordPress Playground. Each run creates a new memory database. The last-item example replays a chosen stale-read order; it does not measure MySQL concurrency or InnoDB row locks.

Your independent testing environment

Make the failure visible.

Choose “Two shoppers, one item.” Run without safeguards and inspect the two saved orders. Turn safeguards on and see the second reservation declined. Reset starts over and stops pending work.

Ready to try

—Items left
—Orders saved
—Outbox events
—Delivery receipts

Follow the decision

    Inspect the result

    Your result and database records will appear here.

    Why the change matters

    Draw the stale-read timeline. Identify the invariant each database mechanism protects. Explain why an outbox event may be delivered more than once and what the receiver must guarantee.

    Use the code viewer to inspect the original source that produced this release. Each file is checked against the release manifest before it is displayed. The complete download includes the runnable example, tests, setup instructions and third-party notices.

    Download this lab

    Keep exploring

    Follow the question you have next.

    Build the safeguards

    Eight explained steps, tradeoffs, pitfalls and a checkpoint before moving on.

    Read the tutorial

    Compare the choices

    Choose a pattern and an environment for the problem you actually have.

    Compare approaches

    Questions before you start

    Do I need to know how to code?

    No. Start with the default example, read the trace and compare the two implementations. The tutorial introduces the code after you have seen the result.

    Does this use real customer data or send anything?

    No. Examples use invented records and simulated downstream work. Each lab keeps its own transient environment; resetting it stops the experiment.

    What can this experiment prove?

    The browser executes actual PHP and SQLite through the open-source PHP.wasm runtime from WordPress Playground. Each run creates a new memory database. The last-item example replays a chosen stale-read order; it does not measure MySQL concurrency or InnoDB row locks.

    Can I use or change the source?

    The original lesson and application source are MIT-licensed. Third-party runtimes retain their own licenses. Download the package and follow GETTING_STARTED before changing it.