Idempotent payout requests: why a retry must never pay twice
Published
When a payout request times out, your platform cannot tell whether it arrived. Idempotency is what makes it safe to send the same request again: however many times it arrives, it never creates a second transfer.
A timeout does not say whether the request arrived
Your platform sends a payout request to an application programming interface, or API, and waits for the response. The response does not come, and the HTTP client gives up with a timeout. At that point the platform cannot tell what happened. The request may never have reached the API. It may have reached it and been processed, with the response lost on the way back. Or it may still be in progress.
From the platform’s side, the three cases look the same: a connection that closed, or a wait that ran out, and nothing more. Timeouts have ordinary causes, such as a slow network, a proxy that closes idle connections or a service restarted in the middle of a request, and a platform that sends payouts every day will meet them.
How a retry becomes a second payment
The natural response to a timeout is to send the request again. For a request that only reads data, that is harmless. For a request that creates a payout, the outcome depends on what the receiving API does with the second copy. If the first request was processed and the API treats the copy as a new instruction, there are now two transfers for one payment, and the beneficiary can be paid twice.
Second copies also come from places that do not look like retries: a job queue that runs a failed job again, an HTTP library set to retry on its own, a person who clicks twice, an operator who re-enters a payment that seemed to fail. Each one sends the same payment a second time, but not always as the same request. An HTTP library repeats the request it already built; a job that runs again may rebuild it, and a person who clicks twice or re-enters the payment can create a new one.
A cross-border payment made twice is hard to undo. The money has already reached a bank account in another country, and getting it back depends on the beneficiary and their bank. It is far cheaper to prevent: an API on which a repeated request has no further effect, and a platform that, when it repeats something, always repeats the same request.
What idempotent means
The definition comes from RFC 9110 (RFC Editor website, opens in a new tab), the Internet standard on the semantics of HTTP, the protocol that carries requests and responses on the web. RFCs, or Requests for Comments, are the series of documents in which Internet standards are published. RFC 9110 defines the term for request methods: a method is idempotent if the intended effect on the server of several identical requests is the same as the effect of a single one. Of the methods the RFC defines, PUT, DELETE and the safe methods, such as GET, are idempotent. POST, which the same document gives, among other uses, the job of creating a new resource, is not among them.
The RFC also explains why the property matters. An idempotent request can be repeated automatically when a communication failure occurs before the client can read the response, because repeating it has the same intended effect even if the original request succeeded. The response to the repeat may differ; the effect does not. For any other request, a client should not retry automatically unless it has some means to know that the request is in fact idempotent, or to detect that the original request was never applied.
The definition covers only what was requested. A server is free to log each request separately, so an idempotent request can still leave a trace every time it arrives.
Extended from methods to operations, the idea stays the same: an operation is idempotent if performing it twice has the same effect as performing it once. For a payout, the effect that counts is the transfer. However many times the same request reaches the API, it never produces more than one transfer.
Retry rules for your platform
An idempotent API makes a retry safe only if what your platform sends really is a retry: the same request. A few rules follow from that.
- Build the request once and keep it. Every attempt sends the body that the first attempt sent, not one rebuilt from your database, where something may have changed in the meantime.
- Never create a new transfer in your own system for the same payment. A new transfer is a new instruction, and idempotency cannot recognize it as a repeat of the first.
- Wait longer before each new attempt, and add some randomness to the wait. Retries then do not pile onto a service that is recovering, and the retries of many platforms do not land at the same moment.
- Set a limit. When it is reached, the payment goes to a person, and nobody sends it again as a new transfer until the fate of the first attempt is known. The same rule applies to an operator about to re-enter a payment that seems to have failed.
- Keep one retry policy, in one place. HTTP libraries and job queues that retry on their own add attempts that nobody planned, and retries stacked in several layers multiply them.
- When requests are signed with a timestamp, as ours are, sign each attempt when it is sent. The body stays the same and the signature is new. The article on signed payout requests explains why.
A change to the amount or the beneficiary means a different instruction, one that must never be sent as a repeat of the first.
A repeated request at NubixPay
Our payout API is idempotent. If your platform sends the same request again, we return the first result and never create a second transfer. If the first attempt never reached us, the retry is simply the first request we receive.
Retries are calls like any other, and they appear in the API log that your team sees in the client portal. Each transfer also has its status in real time, through the API and in the portal, from when it is recorded until it is paid. After a timeout, that status shows whether the transfer was recorded. It is the first thing to check, before anyone thinks of sending a new one.
The other properties of the API are summed up on the payout API page, and the page on payment processing describes how each payment instruction is handled.
Sources
- RFC 9110: HTTP Semantics (RFC Editor website, opens in a new tab)www.rfc-editor.org