ASP.NET Core Web API Interview Question #185

How do you prevent duplicate order or payment creation when clients retry?

Senior API Design, Architecture, Scalability & Production Scenarios Senior Advanced

Quick Interview Answer

Use an idempotency mechanism for operations that may be retried. The client sends a unique idempotency key, and the server atomically associates that key with the operation and its outcome.

Detailed Explanation

Use an idempotency mechanism for operations that may be retried. The client sends a unique idempotency key, and the server atomically associates that key with the operation and its outcome.

A repeated request with the same key and equivalent semantics should return the prior outcome rather than creating a second effect. Database uniqueness constraints or transactional storage are important to prevent races.

Code Example

POST /api/orders
Idempotency-Key: 70d6c1...

// Persist a unique key with the operation.
// Repeated equivalent requests reuse the result.