(Note · · 5 min read)

Two buyers received the same code

Selling something physical, the worst case is a delay. Selling a code, a licence key or a ticket, the worst case is giving the same one to two people — and because delivery is instant, you find out from the second buyer rather than from a warehouse.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

Allocation is the hard part, not delivery

Sending an email is easy. Deciding which code belongs to which order, exactly once, under concurrency, is the problem. Those are separate steps and should be separate in the code.

Keep each item as a row with a state and an owner. Allocation is a conditional update claiming an unsold row for an order, and the database settles any race over the last one.

Claim one, atomically
UPDATE codes SET order_id = $1, state = 'allocated', allocated_at = now()
 WHERE id = (SELECT id FROM codes
              WHERE product = $2 AND state = 'available'
              ORDER BY id LIMIT 1
              FOR UPDATE SKIP LOCKED)
 RETURNING code;
-- no row returned = sold out, not an error to retry blindly

Skipping locked rows is what lets concurrent buyers each claim a different code rather than queueing behind one another. Selecting a code and then updating it in a second statement is the version that sells one twice.

Get the order right: reserve, confirm, allocate, deliver

Reserve at checkout so stock cannot vanish while a buyer pays, with an expiry. Confirm the payment. Allocate the specific item. Deliver. Record delivery as its own state.

Allocating before payment confirms means abandoned checkouts consume inventory. Allocating after delivery is a contradiction. The sequence matters more than any individual step.

Make the buyer's view the reliable path

Email is not a delivery guarantee: it is delayed, filtered and occasionally never arrives. Show the code on screen immediately and keep it available in the order, with email as a convenience rather than the mechanism.

On a gift card shop I built, the code appears on screen the moment payment clears and is also emailed. The on-screen path is the one that works when the mail is in a spam folder — and that is the support request you avoid having at all.

Deliver idempotently

A buyer refreshing, a retried job, or a duplicate webhook must not allocate a second item. Key delivery on the order, check whether it is already allocated, and return the existing code rather than claiming another.

Log every time a code is shown or resent, with who and when. For a dispute about whether something was delivered, that log is the answer, and for a code resent repeatedly it is a sign worth looking at.

Treat the inventory as secret material

Unsold codes are bearer instruments. Restrict who can read them, log access, and keep them out of logs, error reports and admin exports. An error that attaches request state is a plausible route for unsold inventory to end up in a monitoring tool.

Show a buyer only their own item, and never allow one order's identifier to return another's code — the ordinary ownership check, applied to the one piece of data where a mistake is immediately monetisable.

Know when you are nearly out

Running out means taking payments you cannot fulfil. Alert on remaining count per product well before zero, and stop accepting orders when the available count reaches it — refusing a sale is much cheaper than refunding one.

Common questions

How do I make sure a code is sold only once?

Keep each item as a row with a state and owner, and allocate with a single conditional update that claims an available row for the order, skipping locked rows. Selecting a code and updating it in a second statement is what sells one twice.

What is the right order of operations?

Reserve at checkout with an expiry, confirm payment, allocate the specific item, then deliver and record delivery as its own state. Allocating before payment lets abandoned checkouts consume inventory.

Is email good enough for delivering a code?

No. Mail is delayed, filtered and sometimes never arrives. Show the code on screen immediately and keep it in the order, with email as a convenience rather than the delivery mechanism.

How should unsold codes be protected?

As bearer instruments: restrict read access, log it, and keep them out of logs, error reports and exports — an error handler attaching request state can copy unsold inventory into a monitoring tool. Never let one order's identifier return another's code.

Need something like this built?