Quick revision
The short answer
The gateway gave up after 2.5 seconds, but the first instance kept working and created the order; the gateway then retried on another instance, which created a second one. A timeout does not mean the request failed. Gateways should retry only idempotent requests by default, order creation should be made idempotent with a client-supplied idempotency key stored under a unique constraint, the gateway timeout should exceed the service's own deadline while the service stops work when its deadline passes, and the 400 duplicates must be found and reconciled.
What to remember
In this answer, we saw that the gateway's 2.5-second timeout abandoned requests that were still running, and its retry on another instance created a second order, because a timeout does not mean failure. Gateways should retry only idempotent requests by default, as NGINX and Spring Cloud Gateway do, order creation should be idempotent with a client-generated key and a unique constraint, timeouts should follow a budget with the gateway above the service's own deadline, and the duplicates should be reconciled and monitored.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The gateway gave up after 2.5 seconds, but the first instance kept working and created the order; the gateway then retried on another instance, which created a second one. A timeout does not mean the request failed. Gateways should retry only idempotent requests by default, order creation should be made idempotent with a client-supplied idempotency key stored under a unique constraint, the gateway timeout should exceed the service's own deadline while the service stops work when its deadline passes, and the 400 duplicates must be found and reconciled.
Why this question is asked
Duplicate orders are an expensive kind of bug: real customers are charged twice, finance spends days reconciling, and the code that caused it looks perfectly reasonable in review. Interviewers ask this because it tests one of the most important ideas in distributed systems, that a caller cannot tell a slow success from a failure. In this answer, we will reproduce how the duplicates appeared, look at how gateways should treat retries, make order creation idempotent, set timeouts from a latency budget, and clean up the damage.
1How the Duplicates Were Created
At peak, POST /orders takes about 3 seconds. The gateway waits 2.5 seconds for a response, then gives up and treats the attempt as failed. But giving up at the gateway does not stop anything on the first instance. That instance is still running the request: it inserts the order, charges the card and commits, about half a second later. Meanwhile, the gateway has already sent the same POST to a second instance, which creates a second order. The client sees one successful response; the database has two orders.
8 messages in order. Play them, or step through them one at a time.
The first attempt was slow, not failed; the retry created a second order.
Notice where the false assumption lives. The gateway's retry logic assumes that a timeout means nothing happened. For a read, that assumption is harmless, because reading twice changes nothing. For a POST that creates something, it is wrong, and every retry is a chance to create a duplicate. With thousands of orders at peak taking longer than 2.5 seconds, 400 duplicates in a day is exactly what we would expect.
2Retry Only Idempotent Requests at the Gateway
A request is idempotent when sending it twice has the same effect as sending it once. GET, PUT and DELETE are defined as idempotent in HTTP, while POST is not. A gateway that cannot know what the service does should retry only idempotent methods by default, and well-designed proxies do exactly that. NGINX will not pass a POST to the next upstream after it has been sent unless proxy_next_upstream includes the non_idempotent flag. Spring Cloud Gateway's Retry filter retries only GET by default. So the configuration that caused this incident was an explicit choice someone made to widen retries.
Retries for POST can still be safe, but only once the service itself makes the operation idempotent. That is the real fix, because clients and mobile networks retry too, and no gateway setting can stop a user's phone from resending a request after a dropped connection.
3Make Order Creation Idempotent
The client generates an idempotency key, a random UUID, once per order the user means to place, and sends it with every attempt in a header such as Idempotency-Key. The service stores the key with the order under a unique constraint. The first attempt inserts the order. Any repeat, whether from the gateway, the app or a user double-tap, hits the constraint, and the service returns the order that already exists instead of creating a new one.
Notice that the unique constraint does the real work. Checking first and then inserting is not enough on its own, because two attempts can run at the same moment, as they did in this incident. The database guarantees only one insert can win. Any side effect, such as charging the card, must follow the same rule: pass the same key to the payment provider, which most providers support for exactly this purpose.
4Set Timeouts From a Latency Budget
The second mistake was a gateway timeout shorter than the time the service legitimately needs. Timeouts should come from a budget: decide how long the service is allowed to take, say 4 seconds for order creation at peak, make the service enforce that deadline itself, and set the gateway timeout a little above it, for example 5 seconds. Then the service either finishes or gives up before the gateway does, and the gateway almost never abandons work that is still running.
Inside the service, enforcing the deadline means query timeouts, timeouts on calls to payment and inventory, and a check before starting expensive steps, so a request that has already used its budget stops instead of finishing work nobody is waiting for.
5Clean Up and Watch for It Again
The 400 duplicates need a reconciliation job: find orders from the same customer with the same items and total created within a few seconds of each other, confirm them against gateway logs showing a retry, and cancel and refund the extra one, with a message to the customer. Then add monitoring: a counter of unique-key hits on the idempotency constraint, which shows how often repeats arrive and are safely absorbed, and the gateway's retry count per route, with an alert if retries on a write route ever appear.
6Follow-up questions
Who should generate the idempotency key: the browser, the mobile app or the gateway?
The client that represents the user's intent, the browser or the mobile app, should generate it once per intended order and reuse it on every retry of that order, including retries after an app restart if possible. The gateway cannot do it properly, because it cannot tell a retried request from a genuinely new order with the same content; a key it generates protects only against its own retries, not against the client resending after a dropped connection.
Is it enough to make the gateway timeout very long?
No. It reduces gateway retries, but users and apps still retry when they see a spinner for too long, and connections still drop after the server has committed. Idempotency is the only fix that covers every source of duplicates.
7Conclusion
In this answer, we saw that the gateway's 2.5-second timeout abandoned requests that were still running, and its retry on another instance created a second order, because a timeout does not mean failure. Gateways should retry only idempotent requests by default, as NGINX and Spring Cloud Gateway do, order creation should be idempotent with a client-generated key and a unique constraint, timeouts should follow a budget with the gateway above the service's own deadline, and the duplicates should be reconciled and monitored.
Open with
The duplicates came from treating a timeout as a failure. At peak, POST /orders took about 3 seconds, but the gateway gave up at 2.5 seconds and retried on another instance.
Then, if they want more
The first instance did not know the gateway had given up, so it finished and created the order, and the retry created a second one. That happened about 400 times. On the gateway, I would retry only idempotent requests by default.
That is the normal behaviour: NGINX will not resend a POST to the next upstream unless non_idempotent is set, and Spring Cloud Gateway's Retry filter only retries GET unless configured otherwise, so someone widened it.
The real fix is making order creation idempotent: the client generates an idempotency key per intended order and sends it on every attempt, and the service stores it with a unique constraint, so a repeated request returns the first order, and the same key goes to the payment provider.
For timeouts, I would work from a budget: the service enforces its own deadline, say 4 seconds, and the gateway waits a bit longer, so the gateway does not abandon work that is still running. Finally, I would reconcile the 400 duplicates with refunds and alert on retries for write routes and on idempotency key conflicts.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how the duplicates were created, and how gateway retries and timeouts should be...": https://scrivio-interview-library.netlify.app/architecture/load-balancing-networking/643f0ef6ad/