Build the safeguards
Eight explained steps, tradeoffs, pitfalls and a checkpoint before moving on.
Read the tutorialPHP / Laravel components / SQL transactions
One item. Two shoppers. A repeated request. Follow the database records through the failure, then try the same event with a stronger design.
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
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
Your result and database records will appear here.
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.
Keep exploring
Eight explained steps, tradeoffs, pitfalls and a checkpoint before moving on.
Read the tutorialChoose a pattern and an environment for the problem you actually have.
Compare approachesA beginner route, a local setup and an extension challenge.
Open the how-to guideNo. Start with the default example, read the trace and compare the two implementations. The tutorial introduces the code after you have seen the result.
No. Examples use invented records and simulated downstream work. Each lab keeps its own transient environment; resetting it stops the experiment.
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.
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.
Lab discussion / PHP Transaction Lab
Ask about the code, compare an approach, or describe an edge case. Reader comments are reviewed before publication.
Code walkthrough
These short excerpts connect the lab’s behavior to its implementation. Each example links to the full tutorial step for its context, tradeoffs and checkpoint. Names and dependencies belong to that source; the excerpts are not standalone programs.
The guarded statement subtracts stock only if enough stock exists at the instant of the update. The affected-row count is the result of that decision. An accepted order is written only when one inventory row changed. This avoids the stale application check demonstrated in Step 1. It is appropriate for this single-SKU counter.
UPDATE inventory
SET stock = stock - ?
WHERE sku = ? AND stock >= ?;
A retry key identifies one logical request, not one network attempt. reserve checks the prior request inside the transaction before checking current availability. It compares a hash of the normalized body, so the same key cannot silently become a different purchase. A unique database key backs up that identity.
if ($previous) {
if (!hash_equals($previous["body_hash"], $hash)) {
throw new DomainException("Different request");
}
return $originalDecision;
}
Choose Failure before commit. The injected exception occurs after stock, order and outbox writes. The catch path rolls back the transaction before the exception leaves the service. The detail panel preserves the after-failure snapshot, then shows a retry. Without the transaction, stock can disappear without an order.
$db->beginTransaction();
try { /* inventory + order + outbox */ $db->commit(); }
catch (Throwable $e) {
if ($db->inTransaction()) $db->rollBack();
throw $e;
}
An outbox row records a committed intention to notify another system. It shares the order transaction. A later worker can find undelivered rows. The browser demo does not contact a service; it exercises a local receiver whose inbox uses the event key as a uniqueness boundary. Lost acknowledgments can cause redelivery even after useful work succeeded.
INSERT INTO outbox (event_key, request_key) VALUES (?, ?);
-- The receiving fixture records a unique event_key in inbox.
The query exercise creates 5,000 invented history rows on every run. Without the matching index, inspect the scan and ordering work. With it, inspect the indexed lookup. The native regression compares result IDs before and after the index. Improving a plan while changing the answer is not an optimization of the same query.
CREATE INDEX history_customer_status_recent
ON quote_history(customer, status, created DESC);