Home / patterns

Race conditions let one coupon, gift card or credit be used twice

A developer at a night-time desk with two monitors of code and a database table, a gift card and paper coupon beside the keyboard and a sticky note on the monitor, no brand logos
Quick answer: AI-generated checkout, credits and referral code usually works in three steps: check the code hasn’t been used, apply it, then mark it used. Send the same request twice at the same moment and both checks can pass before either write lands—so a one-time coupon, gift card balance or free AI credit gets used twice or more. MITRE files this under CWE-362; PortSwigger calls it a limit-overrun race. The fix is to make the check and the update one atomic database operation.

Last updated November 13, 2026

Steps

  1. List every “only once” or “only up to X” rule in the app: one-time coupons, gift card and store-credit balances, free-trial or AI credit counters, referral bonuses, inventory for limited drops, votes and ratings, and withdrawal or payout limits.
  2. For each, find the code. The risky shape is read-then-write: SELECT (or ORM find) to check the limit, some application logic, then a separate UPDATE or save().
  3. Replace it with one conditional write the database enforces—for example UPDATE coupons SET used_at = now() WHERE code = $1 AND used_at IS NULL RETURNING id—and only apply the discount if a row came back.
  4. For balances, decrement in the same statement with a guard (… SET balance = balance - $amt WHERE id = $id AND balance >= $amt), or wrap the read and write in a transaction that locks the row (SELECT … FOR UPDATE in PostgreSQL).
  5. Add database constraints as a backstop: a UNIQUE index on (coupon_id, user_id) or on redemption records, and CHECK (balance >= 0).
  6. Don’t use session variables, in-memory flags or a client-side “disabled” button to enforce the limit—they don’t stop parallel requests.
  7. For payment and credit-granting endpoints, accept an idempotency key so retries and double-clicks are processed once (Stripe’s API supports idempotent requests).
  8. Retest in staging by firing 20–30 identical requests in parallel at the redeem endpoint. Exactly one should succeed.

Why it matters

MITRE CWE-362 describes code that relies on exclusive access to a shared resource while a timing window lets another thread change it. PortSwigger’s Web Security Academy shows how a one-time discount code can be claimed many times inside a fraction-of-a-second race window, and lists gift cards, ratings, balance withdrawals and rate limits as common targets. Its prevention advice: make state changes on sensitive endpoints atomic using the datastore’s concurrency features, use uniqueness constraints as defense in depth, and don’t use one storage layer (such as sessions) to secure another.

Sources

MITRE CWE-362: Concurrent Execution using Shared Resource with Improper Synchronization — https://cwe.mitre.org/data/definitions/362.html

PortSwigger Web Security Academy: Race conditions — https://portswigger.net/web-security/race-conditions

PostgreSQL documentation: Explicit locking — https://www.postgresql.org/docs/current/explicit-locking.html

Stripe API reference: Idempotent requests — https://docs.stripe.com/api/idempotent_requests

Checked at the source on November 7, 2026.

Related patterns

Found a problem you'd rather not fix yourself?

Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.

Get it fixed by StorenTech

Vibe Security Weekly

One email a week with new failure patterns and how to fix them.

By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.