Episode 2 of 9 · Stripe
Idempotency keys: how a retry stops being a second charge
Vertical version.
Chapters
Narration
You are buying something online. You tap pay, the spinner turns, and then nothing comes back. No success page. No error. Just the spinner. Here is the question. Did the charge go through, or did it die on the way? From where you are sitting, those two look exactly the same. That one moment is what this episode is about. And the question to keep in your head is simple. How do you press pay again, safely, when you cannot tell what happened? Why should you care? Every payment is a request that can be lost, and the people who build shops know it. A phone drops off wifi. A server restarts. A reply is slow. If the shop cannot retry, it loses sales. If it retries carelessly, it charges you twice. Both are bad, and one of them costs real money. Stripe, the payments company, solved this with one small header on every request. Here is the plan. First, the silence, and why a retry is forced on us. Second, the stamp, Stripe's fix. Third, the race, where the fix itself can break. And fourth, the trade, measured, with numbers. Stop one, the silence. Follow the request. It crosses a few hops to reach the card network, and the answer has to cross them all again on the way back. Now picture a failure at each end. If the request dies on the way out, no money moved. Nothing happened at all. But if the request arrives, the money moves, and then the reply dies on the way home, the money moved and nobody told you. Two failures. One silence. From the shop's window, you cannot tell which one you are in. So what is the obvious move? Send it again. In fact, you have no choice, because silence is not an answer. Watch what that does. The first slip might have worked. The second is a new slip, so the server charges again. The third is the same. Five hundred rupees, meant once, and the book now has three rows. Three times five hundred is fifteen hundred rupees. Retrying is forced, and that means duplicates are guaranteed. People say they want exactly once delivery. No network can promise that. It may lose a message, or it may repeat one. So the duplicate is not a bug to remove. It is something to plan for. Stop two, the stamp. The fix is an old habit from paper forms. Before you send a payment, you give it a ticket number, chosen by you, the client. The server does not pick it. You do, once, for this one payment. Then every attempt for that payment carries the same ticket number. Stripe calls it an idempotency key. That long word means one thing. Doing it twice has the same effect as doing it once. In practice it is one header on the request, with a random code in it. Now what does the server do with that ticket? It keeps a table, with three columns. The key. A fingerprint of the request, which is a short summary of what was asked. And the saved response. The first time a key arrives, the server runs the charge, and saves the answer next to the key. Every time after that, it finds the key, and replays the saved answer. It never touches the card. One more rule. If the same key comes back with a different body, a different amount, say, the server refuses it. The fingerprint is how it notices. And keys expire after twenty four hours, so the table does not grow forever. Stop three, the race. Look closer at that table. Checking if the key is there, and then inserting it, are two separate steps. Between them there is a gap. Now imagine a double click. Two requests, with the same key, arrive at the same moment. Both look in the drawer. Both find nothing. Both then run the charge. That is one thousand rupees charged, for five hundred owed. The key was there, the table was there, and it still failed. The problem is the gap between looking and filing. The fix is to close the gap, so that looking and filing become one single step that cannot be split. A database gives you exactly that, a unique index on the key column. Think of a turnstile in front of the drawer. Only one request gets through with a given key. The next one is stopped, and gets a four oh nine, a conflict, instead of a second charge. So the database is the referee, not the application code. This is also why the table lives in Postgres and not in a cache like Redis. A cache is fast, but it can be flushed, and a flush would quietly reopen the double charge hole. Durable beats fast here. Stop four, the trade. What does all this buy? Here is a run from the lab on this site. It uses an example: twelve payments of five hundred rupees, thirty percent of replies lost, and four retries. Without a key, those four retries each charge again. Fourteen charges, times five hundred, is seven thousand rupees. With a key, every retry is replayed. Twelve charges, times five hundred, is six thousand. That is a thousand rupees nobody owed, or nothing. The price is a table to keep, a lock to hold, and an answer stored for a day. Three things to keep. One. A lost request and a lost reply look the same, so the client must retry, and duplicates are guaranteed. Two. The client chooses one key per payment, and the server saves the first answer and replays it. Three. The check and the insert must be one step, so a unique index in a durable database is the referee. So the second attempt costs exactly nothing. Delivery stays at least once, because it always will be. The handling becomes idempotent, and that is all exactly once processing has ever meant.
Based on the board The API & Idempotency.
Try it in the lab: Charged twice.
Related papers: The Google File System, Kafka: a Distributed Messaging System for Log Processing, End-to-End Arguments in System Design, The Chubby lock service for loosely-coupled distributed systems and Conflict-free Replicated Data Types.
Covered in Stripe, the case study.
Saved in this browser, and to your account when you sign in.