Handling In-Progress Requests
What you'll learn: How to detect and handle duplicate requests that arrive while the original request is still being processed.
The Problem
You've implemented idempotency keys to prevent duplicate operations, but what happens when a client retries while the first request is still running? If you simply check whether the idempotency key exists, you might miss that the operation hasn't completed yet. The client could receive inconsistent responses or trigger race conditions.
The Core Pattern
When a duplicate request arrives mid-processing, you have two main strategies:
1. Return a "Processing" Status
Store the idempotency key immediately when processing begins, marking it as "in-progress." When a duplicate arrives, check this status:
- Status = in-progress: Return
HTTP 409 ConflictorHTTP 202 Acceptedwith a message like "Request is being processed" - Status = completed: Return the cached result from the first request
2. Wait and Return the Actual Result
Some systems hold the duplicate request open until the original completes, then return the same result to both. This requires tracking pending requests and notifying waiting clients when processing finishes.
Storage Pattern
Your idempotency key record needs at least three states:
processing(request started but not finished)completed(finished successfully, result cached)failed(finished with error, may allow retry)
Analogy: Think of a restaurant kitchen with order tickets. If a waiter tries to submit the same order twice while the first is still cooking, the system should say "already cooking" rather than starting a duplicate meal.
Key Takeaway: Handling in-progress requests requires tracking request lifecycle states beyond just "done or not done," preventing race conditions and giving clients meaningful feedback during concurrent retries.