Idempotency 00:00:01 Two attempts, one intended charge A bank transfer reaches the payment service, the balance changes, and the response disappears. The customer taps again. The second request looks like a brand-new message; it should still mean the same purchase. The customer clicks once. The request reaches the API and the card is charged. The network fails on the way back. The customer sees a spinner, then an error. For the server this is a second, separate request. It charges the card again. A third click makes it $299.97. The server did nothing wrong: nobody told it these were the same payment. Separate the identity of a network attempt from the identity of the customer's purchase. A second intentional purchase must be allowed even if its amount happens to match the first one. 00:00:49 Record a result for the repeated key The client makes one key per intention. The server claims it, works once, stores the answer and replays it. A random UUID, created when the customer presses Pay and kept for every retry of this payment. Here it is k1. The server inserts k1 as processing. The unique constraint lets only one request win. It charges the card and passes the same key on, so the card network can remove duplicates too. The payment, the status code and the body are saved and k1 becomes completed. The network drops the response. The client waits, then retries with the same k1. The insert finds k1 completed. The stored answer is returned and the card network is not called. Validate the key in the scope of the customer and operation, and compare the relevant arguments. Returning an earlier result for a reused key with different details would conceal a conflicting request. 00:01:48 One key arrives concurrently A read followed by a write has a gap. A unique insert has none. Requests A and B arrive together. Both look for k1 and both find nothing. Each is sure it is the first. The card is charged twice even though a key was sent. Now both try to insert k1. The database accepts one insert and refuses the other. A goes on. B answers 409 with a short wait, retries, and gets the stored answer. Use a uniqueness rule or atomic claim before executing the side effect. A read followed later by an unconditional insert gives two workers an opportunity to believe they are first. 00:02:28 Crash between stages The server dies after claiming the key. What the next retry may do depends on where it died. k1 is processing. The charge has just been sent. It crashed after the card network said yes and before the answer was stored. k1 is still processing, so the client gets 409 and waits. Nothing is charged twice. A job finds keys that are processing for too long. It asks the card network about k1. The charge exists, so its result is stored. The next retry is an ordinary replay. Write down which step can be recovered from durable evidence. An external processor requires its own stable operation identity and reconciliation because a local database cannot atomically include the provider's action by itself. 00:03:16 Read the method's semantics The HTTP methods one by one, and how to write a PATCH that may be repeated. GET reads. Repeating it changes nothing. PUT sets a whole resource. DELETE removes it: the second answer may be 404, but the state is the same. Adding 100 twice gives 200. Setting to 100 twice gives 100. The method name promises nothing. Two calls make users 123 and 124. This is the one that needs an idempotency key. HTTP idempotency describes the intended effect of repeating a request, not necessarily identical response codes or identical logging. An application can supply a repeat-safe contract for a specific creation or payment operation. 00:04:09 The customer returns after a timeout The old payment simulator as a story: one new key, then retries with the same key, then a new purchase. New key: processed, 201. Total charged $99.99, duplicates blocked 0. Both are replays of the first answer. Total still $99.99, duplicates blocked 2. A new intention gets a new key and is charged. Total $199.98, and this time that is right. Explain how long the service retains its record of the intent. After that window, blindly replaying an old key may carry different risk and may warrant checking the original business record. A stable identity, matching parameters and a durable result make the intent recoverable. A retry count by itself says nothing about whether money moved during an earlier attempt.