うつろい EC で「初回限定 50% OFF」キャンペーン。クーポンコード FIRST50 は1人1回限りのはず。サーバの実装は:
function redeemCoupon($code, $order_id, $user_id) {
// ① 未使用チェック
$coupon = SELECT * FROM coupons WHERE code = $code AND used = false;
if (!$coupon) return error('使用済み');
// ② 注文に割引適用(数百ミリ秒かかる処理)
apply_discount($order_id, $coupon->amount);
// ③ クーポンを使用済みに
UPDATE coupons SET used = true WHERE id = $coupon->id;
}
① と ③ が分離している間に並行リクエストが入ると、両方が「未使用」と判定されて二重適用されます。