The morning after our November campaign, at 08:40, my technical contact at our payment provider called. The tone of their voice said it all: "In your traffic last night, the rate of repeat transactions from the same card for the same amount was nine times normal. Did something change on your end?" We looked at the call center dashboard; since midnight, 843 customers had called to complain about being charged twice. On a platform processing 1.1 million payments a day, a rate of 7 in 100,000 looks small on paper, but each one is a person whose card was charged twice and whose trust was shaken. What we learned that week was that in distributed systems, the sentence "the payment failed" is very often a lie.
First, We Blamed the Other Side
Our first hypothesis was a comfortable one: the payment provider was double-processing. We requested the reconciliation file, matched it against our transaction logs and saw the embarrassing truth: for every double-charged transaction, two separate authorize requests had gone out from our side. The second hypothesis was the mobile app: "Users must be tapping the button twice." We looked at the client logs event by event; most users had tapped only once. Debounce was already in place. The problem was deeper, and it was in our own backend.
Once we stopped assigning blame and started counting, the picture became clear. We classified every double charge from that night one by one; it was a tedious but invaluable exercise. 71% of the cases came from server-side timeout retries, 22% from the "try again" button on the error screen, and 7% from a path we had never thought of: the mobile app, when sent to the background and reopened, silently re-triggering an unfinished payment flow. That third group could never have been found without building a taxonomy. Ever since, the first task in every payment incident is producing a breakdown table; searching for a single root cause often means ignoring two small ones.

A Timeout Is Not an Answer
Here's how the actual chain worked: on campaign night, the payment provider's response times grew longer. Our payment service's HTTP client had a 30-second timeout and a "retry once on network error" policy. The provider successfully processed the request at second 31; we gave up at second 30 and sent the same request again. From the provider's point of view, these were two separate, perfectly legitimate transactions. A timeout doesn't tell you the operation failed; it only tells you that you don't know the result. Blindly firing a retry on top of an unknown result is the most classic recipe for a double charge. We found a second leak, too: the "try again" button on the error screen created a brand-new payment request with every tap; nobody was asking what had happened to the previous attempt.

The Right Scope for the Key
The backbone of the solution was an idempotency key, but the real issue was deciding what the key represents. In the first draft we had derived the key from the order number; during code review, a colleague asked a fair question: "What happens if the user's first card is declined and they try another card?" An order-based key would have blocked that legitimate second attempt, too. So we introduced a concept in between: the payment intent. One intent per order, and one attempt tied to the intent for every charge attempt. The idempotency key is generated at the attempt level, on the client side; retries of the same attempt carry the same key, while the user's deliberate new attempt gets a new key. The key lives both on our side and in the request sent to the provider, because our provider also supports idempotency via a header, and two layers of protection are always better than one.
On the server side, the key is held in two places at once. In Redis, a 15-minute operation lock with SETNX: if a second request arrives with the same key, we return the stored result of the first request without hitting the database, and if the operation is still in flight, we say "wait" with a 409. In the persistent layer, the payment_attempts table has a unique index on the key; even during a Redis failover, the last line of defense is the database itself. I want to stress this distinction in particular: a distributed lock is a performance optimization, not a correctness guarantee. Correctness comes from the unique constraint.

We were cautious when rolling the new design out to production, too. For the first two weeks, the idempotency layer ran in shadow mode: keys were generated and conflicts were logged, but no requests were blocked. During this period we manually reviewed the transactions the system would have flagged as "duplicate" and caught two false-positive scenarios: one where the key mistakenly stayed the same when the installment option changed, and another where a page refresh in a webview legitimately resent the same attempt. We fixed both before we started blocking. In staging we also ran a chaos scenario: we deliberately injected 31-second delays into a fake PSP and verified with an automated test that the retry chain now produced an inquiry instead of an authorize. In payment code there's no such category as "it'll probably work"; either there's a test for it or there isn't.
The State Machine and the Inquiry Reflex
We rewrote the retry policy from scratch, too. Attempts now live in a strict state machine: CREATED, SENT, SUCCEEDED, FAILED, UNKNOWN. A timed-out operation moves not to FAILED but to UNKNOWN, and a new authorize is never sent for an attempt in the UNKNOWN state; first, the provider's inquiry endpoint is asked, "have you seen a transaction with this key?" Until the answer comes back, we show the user an honest screen: "We're checking the status of your payment." We were afraid this screen would hurt conversion; we measured it, and the cancellation rate didn't change. People don't get angry about uncertainty; they get angry about being charged twice.
So what happened to those 843 customers? Our first decision on the morning of the incident was to call them before they called us. We pulled the affected transactions from the reconciliation data and started the refunds in bulk, and sent each customer a notification explaining the situation along with a small apology coupon. 90% of the refunds were reflected on cards within 24 hours. We measured it afterward: the order frequency over the following 90 days of the customers we proactively contacted was not meaningfully lower than that of a control group unaffected by the incident. Engineering pays the bill for technical debt, but the whole company pays the bill for trust debt; early and honest communication is the only line item that can shrink that bill.
Reconciliation: The Last Line of Defense
Despite all these layers, a reconciliation job runs every night: it does a three-way match between the provider's daily reconciliation file and our own attempt table. Every record that looks successful on our side but is missing from the file, is in the file but stuck in UNKNOWN on our side, or has a mismatched amount lands in a Slack channel at 07:00 in the morning. In the first months, this job caught 30-40 discrepancies a day; as we closed the edge cases one by one, the number dropped to one a week. Reconciliation had a side benefit, too: the same job also catches the reverse of a double charge, "ghost" payments that look failed on our side but were actually collected. This second kind never comes in as a customer complaint, because the customer sees the money taken from their card and assumes the order is on its way; the order, however, was never created. We now catch these silent cases, which could take weeks to notice, the next morning and rescue the order manually; on average, 15-20 customers a month carry on without ever knowing what happened to them. Our double-charge rate dropped from 7 in 100,000 to a measurable zero over the last six months. Payment-related calls to the call center fell by 31%, and I'm not even counting the cost of the refund operation.
If I had to squeeze what this incident taught me into a single sentence: engineering in payment systems isn't about coding the happy path; it's about making the "I don't know" state a first-class citizen. Any system that mistakes a timeout for an error will, under enough traffic, sooner or later charge someone's card twice. If you can't remember the last time you read through your platform's retry policies end to end, read them this week; if a scenario keeps nagging at you, reach out via the contact page. As someone who has wrestled with reconciliation files, I'd be glad to talk it through.