Quick revision
The short answer
End-to-end idempotency has two links. From the client, we accept its idempotency key and store it with a unique constraint in a shared database, never in pod memory, so all 12 pods agree. Towards the provider, we create our own payment id once, persist a state machine CREATED, SUBMITTED, CONFIRMED or FAILED, save SUBMITTED before calling, and derive the provider key deterministically from the payment id so every retry reuses it. On a timeout we retry with the same key or query the provider, never creating a new payment id; a reconciliation job resolves payments stuck in SUBMITTED, and key retention outlives the longest client retry window.
What to remember
In this answer, we built idempotency from client to provider as one chain. The client's key is stored with a unique constraint in the shared database, so all 12 pods agree and a retry maps to the same payment id. The payment follows a state machine with SUBMITTED persisted before the provider call, and the provider key is derived from our payment id so every retry reuses it. Timeouts lead to retries with the same key or a status query, never a new payment id, a reconciliation job resolves payments stuck in SUBMITTED, and key retention is longer than the longest client retry window.
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.
End-to-end idempotency has two links. From the client, we accept its idempotency key and store it with a unique constraint in a shared database, never in pod memory, so all 12 pods agree. Towards the provider, we create our own payment id once, persist a state machine CREATED, SUBMITTED, CONFIRMED or FAILED, save SUBMITTED before calling, and derive the provider key deterministically from the payment id so every retry reuses it. On a timeout we retry with the same key or query the provider, never creating a new payment id; a reconciliation job resolves payments stuck in SUBMITTED, and key retention outlives the longest client retry window.
Why this question is asked
A payment passes through three parties: the client calling our API, our service running on many pods, and the external provider that actually moves money. Each link can time out, and each side may retry. If any link treats a retry as a new request, someone is charged twice. Interviewers ask this design question at the ten-year level because it requires connecting two idempotency mechanisms into one chain and thinking through every partial failure. In this answer, we will look at the chain as a whole, design the client side across 12 pods, create the payment and its state machine, derive the provider key, handle timeouts and partial failures, and finish with reconciliation and retention.
1The Chain We Need to Protect
There are two hops. The client sends POST /payments to us with an Idempotency-Key header. We call the provider's charge API, which also accepts an idempotency key. A duplicate can be born at either hop: the client retries because our response was slow, or our own code retries because the provider was slow. The design must make both kinds of retry collapse into one charge.
The principle that ties them together is simple: one logical payment has one id in our system, created exactly once, and everything downstream is derived from that id. If we ever create a second id for the same payment, the chain breaks.
9 messages in order. Play them, or step through them one at a time.
The retry to the provider reuses pay-P, so the provider returns the first charge instead of a new one.
2The Client Side Across 12 Pods
With 12 pods behind a load balancer, a client's retry will usually land on a different pod from the first request. So the record of seen keys cannot live in pod memory or a local cache; it must be in shared storage with an atomic uniqueness check. The natural place is the payments database itself, in a table with a primary key on client id and idempotency key, storing the request hash and the payment id it maps to.
The first request inserts the key and, in the same transaction, creates the payment row. A retry on any pod hits the unique constraint, loads the row, and either returns the stored response or, if the payment is still in flight, a 409. Because the database decides who was first, two retries arriving at two pods in the same millisecond still produce one payment.
3The Payment State Machine
Our payment moves through explicit states. CREATED means we have accepted the request but not contacted the provider. SUBMITTED means we have sent, or are about to send, a charge request. CONFIRMED and FAILED are final outcomes from the provider. The crucial rule is that SUBMITTED is persisted before we call the provider.
The reason is crash safety. If a pod dies after the provider charged the card but before we recorded anything, and the payment were still CREATED, a recovery process could not tell whether the provider was ever called. With SUBMITTED committed first, we know a charge may exist, and the only safe actions are to retry with the same provider key or ask the provider. A payment in SUBMITTED is never re-created and never charged with a new key.
| State | Meaning | Safe next action |
|---|---|---|
| CREATED | accepted, provider not called | submit with key pay-<id> |
| SUBMITTED | provider may have charged | retry same key or query provider |
| CONFIRMED | provider charged | return stored response |
| FAILED | provider declined or rejected | return stored error; new payment needs new key |
4Deriving the Provider Key
Towards the provider, we do not generate a fresh UUID per call. We derive the key from our payment id, for example pay-<paymentId>, so that every attempt for this payment, on any pod, after any restart, sends the same key. The provider then deduplicates our retries exactly as we deduplicate the client's.
Notice that we do not pass the client's key straight through. Clients choose their keys, and two clients could choose the same value; our payment id is unique by construction. Deriving the provider key from our own id also means that if we ever need to retry from a recovery job that has no access to the original HTTP request, we can still compute the key.
5Timeouts and Partial Failures
When the provider call times out, the outcome is unknown: the charge may or may not have happened. We have two safe choices. Retry the charge with the same provider key, within the provider's idempotency window, and take whatever result comes back, the original charge if it existed. Or query the provider for the status of the charge by our reference. What we must never do is mark the payment failed and let the client create a new one, or create a new payment id ourselves, because the first charge might have succeeded.
If our pod dies after the provider responded but before we stored CONFIRMED, the payment stays SUBMITTED and the client's retry gets 409 or waits. The same recovery path applies: retry or query with the same key, then store the result. Each failure mode ends in the same place, which is what makes the design easy to reason about.
6Reconciliation and Retention
The job above is the reconciliation loop for the live system: it resolves every payment stuck in SUBMITTED by asking the provider, so no payment stays unknown for long, and its counter tells us how often our happy path fails. We also run a daily reconciliation against the provider's settlement report, comparing every charge they made with a CONFIRMED payment of ours, to catch anything the live loop missed.
Retention ties the two hops together. Client keys must be kept longer than the maximum time a client may retry, which we state in the API documentation, for example 24 hours if clients retry for up to a few hours. If keys expired sooner, a late retry would create a second payment. On the provider side, we check how long the provider honours its keys, which is the next question.
7Follow-up questions
What happens if the provider's idempotency window is shorter than your retry window?
Then a retry of the same payment after the provider's window has expired would look like a new charge to the provider. So we never rely on the provider key alone for late retries. After the provider's window, or as a rule before every resubmission of a SUBMITTED payment, we query the provider's status by our reference first, and only resubmit when the provider confirms there is no charge. We also keep our own retries well inside the provider's window and alert on payments that stay SUBMITTED long enough to approach it. The client-facing retention is ours to control, so the client key keeps mapping to the same payment id however long the client retries.
Could a distributed cache such as Redis hold the client keys instead?
It can serve as a fast first check, but the authority should be the database that holds the payment, so that the key and the payment are created in one transaction. With Redis alone, a key could be recorded while the payment insert fails, or the key could be lost on failover, and both lead to duplicates or stuck requests.
8Conclusion
In this answer, we built idempotency from client to provider as one chain. The client's key is stored with a unique constraint in the shared database, so all 12 pods agree and a retry maps to the same payment id. The payment follows a state machine with SUBMITTED persisted before the provider call, and the provider key is derived from our payment id so every retry reuses it. Timeouts lead to retries with the same key or a status query, never a new payment id, a reconciliation job resolves payments stuck in SUBMITTED, and key retention is longer than the longest client retry window.
Open with
I would treat it as one chain with two links tied together by our own payment id. On the client side, I accept the Idempotency-Key header and store it in the payments database with a unique constraint on client id and key, never in pod memory, because with 12 pods a retry usually lands on a different pod.
Then, if they want more
The first request inserts the key and creates the payment with a new id in the same transaction; any retry hits the constraint and gets the existing payment. The payment has a state machine, CREATED, SUBMITTED, CONFIRMED, FAILED, and I persist SUBMITTED before calling the provider, so after a crash I know a charge may exist.
Towards the provider I derive the idempotency key deterministically from the payment id, like pay plus the id, so every retry from any pod sends the same key. On a timeout I either retry with that same key or query the provider status; I never create a new payment id or mark it failed blindly.
A reconciliation job finds payments stuck in SUBMITTED and resolves them by asking the provider, and a daily comparison with the provider's settlement report catches the rest. Finally, client keys are retained longer than the maximum client retry window, and I check the provider's window too, querying before resubmitting if it might have expired.
About this answer
- Last updated
- 12 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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Cloud documentation, Resilience4j documentation
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 "Your payment gateway integration calls an external provider that supports idempotency...": https://scrivio-interview-library.netlify.app/spring-boot/distributed-data/543214cdfb/