← Projects

Preventing Duplicate Order Processing in a .NET API

An idempotency and concurrency guard that serialises duplicate requests for the same order, while leaving unrelated orders free to run in parallel.

C# / .NETREST APIsIdempotencyConcurrency Control
Problem
The API could receive the same order more than once, including nearly simultaneous requests from different clients. Without protection, both requests could pass the initial validation and execute the same business workflow, creating duplicate processing and inconsistent downstream state.
Approach
I added an idempotency and concurrency guard around the critical processing path so only one request for the same order could execute at a time. I considered relying only on an initial database existence check, but rejected that approach because two concurrent requests could perform the check before either one committed its changes, creating a race condition. The locking strategy was scoped by order identifier, so unrelated requests could continue processing concurrently.
Outcome
Duplicate concurrent requests for the same order were serialised instead of executing the workflow twice. This removed the race condition while preserving concurrency for different orders, and made the endpoint safer under real multi-client traffic.