Agent Wranglers

ECM-23Recommended

Hand out each licence key once

Assign licence keys, gift card codes and other single-use codes to an order line with one conditional update. Revoke the key when the line is refunded.

How to do it

Two orders that read the same free key both send it, and two customers share one licence. Claim a key with an update that sets the order line only where it is still empty. Skip locked rows so buyers do not wait on each other. Alert when unused keys fall below a set number, such as 50. Store keys encrypted or hashed where you only need to check them. On refund or withdrawal, mark the key revoked, so it stops working. The race is the one in Make each change happen exactly once.

Examples

SQL example

UPDATE licence_key SET order_line_id = $1, assigned_at = now()
WHERE id = (
  SELECT id FROM licence_key WHERE product_id = $2 AND order_line_id IS NULL
  ORDER BY id LIMIT 1 FOR UPDATE SKIP LOCKED
)
RETURNING code;

How to check

A test sends 50 orders at once against a pool of 50 keys. Every order gets one key and no key goes to two orders. Refunding one order marks its key revoked.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ecommerce/orders-and-fulfilment/licence-keys-once">Agent Wranglers e-commerce and fulfilment playbook</a>. In code you write, add a comment next to it with the same link.