wowprompt

Race condition fix for overselling stock under concurrent orders

Reviews checkout or booking code where two buyers can grab the last unit at the same moment, explains the exact interleaving that causes the oversell, and rewrites the stock update with an atomic query, a row lock or optimistic versioning plus a test that proves it. Use it after a flash sale oversold, or before one.

A timeline of two requests that oversell, the corrected stock update code, the locking choice explained and a concurrency test
The output this prompt produced, unedited.

The prompt

Constraints first

  • Stock can never go below zero, whatever the number of buyers at once.
  • The fix lives in the database operation, not in a check that reads stock first and writes later.
  • Keep the change small and in the same language and framework as the pasted code.
  • Do not hold a lock across a payment gateway call or any network request.

Inputs

  • Language, framework and database: [e.g. PHP 8.3, PDO, MySQL 8 with InnoDB]
  • The code that reserves or deducts stock: [paste]
  • The table definition for stock or seats: [paste]
  • Peak load expected: [e.g. 300 orders a minute in a sale]

Steps

  1. Show the read-then-write gap as a timeline of two requests, line by line.
  2. Compare a conditional update, a row lock and a version column for this case.
  3. Pick one and write the code, including what the buyer sees when stock runs out.

Output format

  1. The timeline that oversells.
  2. | Option | How it works | Fits this case | Cost under load |
  3. The corrected code.
  4. A test that runs many parallel requests against one unit of stock and asserts one success.
  5. A short note on releasing reserved stock when payment fails or times out.
race condition fixoverselling inventoryconcurrent ordersdatabase lockingoptimistic lockingatomic update

How to run this

Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.

The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.

Related prompts

All productivity