Designing Failover That Doesn't Double-Charge
Idempotency keys, settlement windows and the reconciliation checks that make automatic retries safe.
Paisape Engineering
Engineering · 12 August 2026
The Silent Nightmare of Double Charges
In the high-stakes environment of digital payments, few things erode customer trust as rapidly as a double charge. A user initiates a transaction, the screen spins indefinitely, and eventually, an error message suggests the payment failed. Naturally, the user tries again. Hours later, their bank statement reveals two identical deductions for a single purchase. This scenario isn't just a poor user experience; it's a profound failure in distributed systems design that leads to operational overhead, increased chargeback ratios, and regulatory scrutiny.
At Paisape, we process millions of transactions daily across diverse networks with varying latencies. Dealing with network timeouts, gateway outages, and delayed bank responses is a routine engineering challenge. When a transaction status is ambiguous—neither definitively successful nor explicitly failed—how does a system decide whether to retry the payment or halt the process? The answer lies in robust failover design, meticulous state management, and the rigorous application of idempotency.
Understanding the Anatomy of a Timeout
To solve the double-charge problem, we must first understand why it occurs. A typical payment flow involves multiple actors: the merchant application, the payment gateway (like Paisape), the acquiring bank, the card network (or UPI switch), and the issuing bank.
When a customer clicks "Pay," a synchronous or asynchronous chain of API calls is triggered. If any node in this chain experiences a transient failure or high latency, a timeout occurs. Crucially, a timeout only means that the response was not received within the expected window. It does not mean the action failed to execute at the destination.
For example, if the acquiring bank sends the authorization request to the issuing bank, but the connection drops before the issuing bank's "Success" response can be relayed back, the transaction is functionally complete at the bank level, but the merchant and gateway remain unaware. If the merchant application blindly treats this timeout as a failure and initiates a retry, a second, entirely distinct transaction is spawned. The customer is charged twice.
Idempotency Keys: The First Line of Defense
The foundational concept for preventing double-charges in distributed systems is idempotency—the property that an operation can be applied multiple times without changing the result beyond the initial application. In payments, this is achieved through Idempotency Keys.
An Idempotency Key is a unique identifier generated by the client (the merchant's backend) and passed along with the payment request to the gateway. When the gateway receives a request, it first checks if it has seen this key before.
- First Request: The gateway processes the transaction, records the result against the Idempotency Key, and returns the response.
- Subsequent Requests (Retries): If the gateway receives another request with the same Idempotency Key, it does not initiate a new transaction with the banking networks. Instead, it retrieves the cached result of the original transaction and returns that identical response.
By enforcing idempotency, even if a merchant's system automatically retries a request due to a timeout, the payment gateway guarantees that only one charge will ever be processed. At Paisape, our idempotency layer is backed by a highly available, strongly consistent datastore (like Redis or DynamoDB) to ensure atomic checks and updates across our distributed server fleet.
Navigating the 'Pending' Purgatory
While idempotency protects against duplicate requests, it doesn't solve the problem of unknown transaction states. Often, a payment gateway itself will encounter a timeout when communicating with downstream banking partners. In these cases, the gateway cannot definitively return a 'Success' or 'Failure' to the merchant. The transaction enters a state we call 'Pending'.
The 'Pending' state is purgatory for payment routing. If a transaction is pending, our dynamic routing engine must not attempt to failover to a backup gateway or acquiring bank. Why? Because the original transaction might still be crawling through the banking network and could eventually succeed. A failover at this stage would guarantee a double charge if the first transaction eventually settles.
Our rule is absolute: Never failover a terminal action if the previous state is unknown.
Instead, the system must rely on asynchronous reconciliation. At Paisape, we employ a multi-tiered status polling mechanism:
- Immediate Polling: For the first few minutes after a timeout, our background workers aggressively poll the downstream bank's status APIs.
- Exponential Backoff: If the status remains unknown, the polling frequency decreases exponentially (e.g., after 15 minutes, 1 hour, 4 hours).
- Batch Reconciliation: Many older banking systems lack robust real-time status APIs. For these, we rely on end-of-day or intra-day batch reconciliation files (often transmitted via SFTP) to finalize the state of lingering pending transactions.
Until a definitive 'Failed' status is confirmed by the downstream entity, the transaction is locked. The merchant must instruct the user to wait, and the user interface should reflect that the payment is currently processing.
Smart Routing and Safe Failovers
So, when is failover safe? Failover routing is only permissible when a transaction fails synchronously and definitively with an error code that indicates the failure occurred before funds were captured.
For example, if a primary acquiring bank returns an immediate HTTP 503 Service Unavailable, or a specific error code indicating "Invalid Card Details" or "Insufficient Funds," we know the transaction has failed. In the case of a 503 error, our smart routing engine can transparently and immediately re-route the payment payload to a secondary acquiring bank using a new internal transaction ID, providing a seamless experience for the user without the risk of double-charging.
To build this safely, our routing logic relies on an exhaustive mapping of thousands of disparate banking error codes into a standardized internal taxonomy. Only specific categories of definitive failures are whitelisted for automatic failover.
The Role of Auto-Reversals and Customer Communication
Despite the best engineering efforts, edge cases persist. In the UPI ecosystem in India, for instance, a transaction might deduct funds from the user's account but fail to reach the merchant's account due to a timeout at the NPCI (National Payments Corporation of India) switch.
In these scenarios, the banking guidelines mandate auto-reversals. If a transaction is dropped in transit after debiting the customer, the issuing bank is responsible for identifying the orphaned transaction and crediting the funds back to the user within a specific SLA (typically T+1 or T+2 days).
For engineering teams, this highlights the importance of transparent user communication. When a transaction is in an ambiguous state, it is far better to display a message like, "Your payment is being verified. If deducted, it will be automatically refunded within 48 hours," rather than allowing the user to panic and hit the 'Pay' button again. Combining technical safeguards like idempotency keys with clear, empathetic UX is the only complete solution to the double-charge dilemma.