Quick revision
The short answer
The refund compensation was not idempotent. The first CancelPayment succeeded, but its reply was slow, so the order service timed out, assumed failure and retried, and the payment service refunded again. We fix it in three layers: store processed commands by command id and return the earlier result for duplicates, model the refund as a state transition from CAPTURED to REFUNDED so a second refund is rejected or ignored, and pass a deterministic idempotency key to the payment provider so even its side cannot refund twice.
What to remember
In this answer, we saw that the double refund came from a non-idempotent compensation meeting a retry after a timeout, where the first command had actually succeeded. A timeout cannot tell success from failure, so retries are necessary and the receiver must be safe to repeat. We fixed it by recording processed command ids with their results, modelling the refund as a conditional state transition from CAPTURED to REFUNDED, and passing a deterministic idempotency key to the payment provider.
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 refund compensation was not idempotent. The first CancelPayment succeeded, but its reply was slow, so the order service timed out, assumed failure and retried, and the payment service refunded again. We fix it in three layers: store processed commands by command id and return the earlier result for duplicates, model the refund as a state transition from CAPTURED to REFUNDED so a second refund is rejected or ignored, and pass a deterministic idempotency key to the payment provider so even its side cannot refund twice.
Why this question is asked
Double refunds are among the most expensive saga bugs, because the money leaves the company and getting it back from a customer is slow and awkward. The bug is common because the code looks correct on its own: a handler that refunds when asked. Interviewers ask about it because the answer shows whether we understand what a timeout really tells us and how to make an operation safe to repeat. In this answer, we will reconstruct what happened, explain why a timeout is not a failure, and then fix it with a processed-commands table, a state transition on the payment, and an idempotency key at the provider.
1Reconstructing What Happened
In the order saga, a later step failed, so the order service sent CancelPayment, a compensation command, to the payment service. The payment service called the card provider, the provider refunded the charge, and the payment service sent a reply. But the provider was slow that day and the whole thing took 6 seconds, while the order service waited only 5. It timed out, decided the command had failed, and sent CancelPayment again. The payment service had no memory of the first command, so it called the provider again, and the customer got two refunds.
8 messages in order. Play them, or step through them one at a time.
The first command worked. The retry repeated the side effect because nothing remembered it.
2Why a Timeout Is Not a Failure
A timeout means only that we did not get an answer in time. The command may never have arrived, may have failed, or may have succeeded with the reply lost or late. The caller cannot tell which. Retrying is still the right thing to do, because otherwise a lost message would leave the saga stuck, but a retry is only safe if the receiver treats a repeated command as the same command.
So the real bug is on the receiving side: the refund was not idempotent. An idempotent operation has the same effect whether it runs once or many times. The handler below is not, because every call refunds.
Note that the retry need not come from the order service at all. Kafka redelivers a command after a consumer rebalance if its offset was not committed, which produces the same double refund.
3Fix 1: Remember Processed Commands
Every command carries a unique command id, set once by the sender and kept on retries. The payment service records each processed command id in a table with a unique constraint, together with the result it returned, in the same database transaction as its own state change. When a command arrives whose id is already there, it does nothing new and returns the stored result.
If two copies arrive at the same moment, both pass the lookup, but only one can insert the row; the other fails on the unique constraint and its transaction rolls back. That is one reason the dedup row and the state change must be in one transaction. But there is a catch: the provider call is not part of our database transaction, so a crash after the provider refunds but before our commit would still let a retry refund again. That is what the next two fixes close.
4Fix 2: Make the Refund a State Transition
The payment has a lifecycle, and a refund is a move from CAPTURED to REFUNDED. If we write it that way, the state itself protects us, regardless of command ids. We first move the payment to REFUND_PENDING with a conditional update, call the provider, then move it to REFUNDED.
A second CancelPayment for a payment already REFUNDED simply replies with PaymentCancelled, since the desired state is already reached. A payment stuck in REFUND_PENDING after a crash is resolved by asking the provider what happened, never by refunding blindly. Notice how this turns repeated commands into a question about the current state, which is far more robust than relying on the caller.
5Fix 3: Give the Provider an Idempotency Key
Card providers usually accept an idempotency key on the refund request: if they see the same key again, they return the original result instead of refunding again. The key must be deterministic, derived from something stable such as refund-<paymentId>, not a random UUID per attempt, or every retry would look new to the provider.
With all three fixes in place, a retry is harmless at every layer: the command table catches duplicates of the same command, the state transition catches any second refund for the payment, and the provider key catches a repeated call after a crash.
6Follow-up questions
Should we also make the order service's timeout longer?
It helps to set it sensibly, above the payment service's p99 including the provider call, so retries are rarer. But it cannot be the fix, because any timeout can be exceeded, and redeliveries happen without timeouts. The receiver must be idempotent anyway, and then the timeout only affects how quickly we retry.
Why store the result and not just the command id?
Because the caller retried to get an answer. If we only skip duplicates without replying, the order service waits forever. Returning the same result as the first time lets the saga continue exactly as if the first reply had arrived.
How long should processed command ids be kept?
Longer than the longest time a duplicate can arrive, which is the retry window of the sender plus the broker's retention for redelivery. Days to weeks is typical. After that a scheduled job can delete old rows.
7Conclusion
In this answer, we saw that the double refund came from a non-idempotent compensation meeting a retry after a timeout, where the first command had actually succeeded. A timeout cannot tell success from failure, so retries are necessary and the receiver must be safe to repeat. We fixed it by recording processed command ids with their results, modelling the refund as a conditional state transition from CAPTURED to REFUNDED, and passing a deterministic idempotency key to the payment provider.
Open with
The cause is that the refund compensation was not idempotent. The first CancelPayment actually succeeded, but the provider was slow, so the reply arrived after the order service's timeout.
Then, if they want more
A timeout only means no answer yet; the command may have worked. The order service retried, which is correct behaviour, but the payment service treated the retry as a new command and refunded again. A Kafka redelivery after a rebalance would cause the same thing. I would fix it in three layers.
First, every command has a stable command id, and the payment service stores processed ids with the result in the same transaction as its state change, so a duplicate returns the earlier result instead of doing the work again.
Second, I model the refund as a state transition on the payment, a conditional update from CAPTURED to REFUND_PENDING and then REFUNDED, so only one request can ever start a refund and a later one just confirms the current state.
Third, because the provider call is outside our database transaction, I pass the provider a deterministic idempotency key, such as refund plus the payment id, so even a retry after a crash cannot refund twice. With that, the order service can retry as often as it needs to.
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 "In an order saga, the payment service refunded a customer twice when the order service...": https://scrivio-interview-library.netlify.app/spring-boot/distributed-data/01532dfcc9/