<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>API design on LoomLoops</title>
    <link>https://loomloops.com/tags/api-design/</link>
    <description>API design for backend engineers: how to build endpoints that behave well when clients retry, time out or send the same request twice.</description>
    <language>en-us</language>
    <lastBuildDate>Thu, 04 Jun 2026 09:20:00 +0000</lastBuildDate>
    <atom:link href="https://loomloops.com/tags/api-design/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Idempotency Keys Explained: How to Make API Retries Safe</title>
      <link>https://loomloops.com/post/idempotency-keys-explained/</link>
      <pubDate>Thu, 04 Jun 2026 09:20:00 +0000</pubDate>
      <dc:creator>Rafi Loom</dc:creator>
      <guid>https://loomloops.com/post/idempotency-keys-explained/</guid>
      <description>&lt;p&gt;A customer taps &amp;ldquo;Pay&amp;rdquo;. Your app sends the payment request. The phone loses&#xA;signal for a moment and the request times out. The app does the sensible&#xA;thing and tries again. The customer gets charged twice.&lt;/p&gt;&#xA;&lt;p&gt;Nothing here was a bug in the usual sense. The network did what networks do,&#xA;and the client did what clients should do. The missing piece was a way for&#xA;the server to recognise &amp;ldquo;this is the same request I already handled&amp;rdquo;.&lt;/p&gt;&#xA;&lt;p&gt;That piece is called an &lt;strong&gt;idempotency key&lt;/strong&gt;. Stripe, Shopify, Adyen, PayPal&#xA;and most banking APIs use one. This post explains the idea, then builds the&#xA;server side step by step.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Short version:&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;The client makes up a unique key for each action, like &amp;ldquo;pay order 881&amp;rdquo;,&#xA;and sends it with every retry of that action.&lt;/li&gt;&#xA;&lt;li&gt;The server saves the key with the result. If the same key comes again, it&#xA;returns the saved result instead of doing the work again.&lt;/li&gt;&#xA;&lt;li&gt;Save the key before doing the work, in the same transaction when you can.&#xA;That is what makes the whole thing safe.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;the-problem-a-timeout-tells-you-nothing&#34;&gt;The problem: a timeout tells you nothing&lt;/h2&gt;&#xA;&lt;p&gt;When a request times out, the client cannot tell which of these happened:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/idempotency-keys-explained/timeout-two-cases.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/idempotency-keys-explained/timeout-two-cases.svg&#34; alt=&#34;Two timelines between a client and a server. Case A: the request is lost on the way, the server never sees it, the client times out, and a retry is correct. Case B: the server gets the request and charges the card, but the response is lost, the client times out, and a retry charges twice.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;388&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;From the client&amp;rsquo;s side, A and B look the same. If it never retries, case A&#xA;leaves a customer who wanted to pay with no order. If it always retries, case&#xA;B charges them twice. You need the server to make retries safe.&lt;/p&gt;&#xA;&lt;h2 id=&#34;what-idempotent-means&#34;&gt;What &amp;ldquo;idempotent&amp;rdquo; means&lt;/h2&gt;&#xA;&lt;p&gt;An action is idempotent when doing it twice has the same effect as doing it&#xA;once. Pressing a lift button is idempotent. Pressing it five more times does&#xA;not call five lifts.&lt;/p&gt;&#xA;&lt;p&gt;HTTP already defines some methods this way (&lt;a href=&#34;https://www.rfc-editor.org/rfc/rfc9110#name-idempotent-methods&#34;&gt;RFC 9110&lt;/a&gt;):&lt;/p&gt;&#xA;&lt;table&gt;&#xA;  &lt;thead&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;th&gt;Method&lt;/th&gt;&#xA;          &lt;th&gt;Idempotent?&lt;/th&gt;&#xA;          &lt;th&gt;Why&lt;/th&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/thead&gt;&#xA;  &lt;tbody&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;GET&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;Yes&lt;/td&gt;&#xA;          &lt;td&gt;Reading twice changes nothing&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;PUT&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;Yes&lt;/td&gt;&#xA;          &lt;td&gt;&amp;ldquo;Set the name to Sam&amp;rdquo; twice leaves the name as Sam&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;DELETE&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;Yes&lt;/td&gt;&#xA;          &lt;td&gt;Deleting a deleted thing leaves it deleted&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;POST&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;strong&gt;No&lt;/strong&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&amp;ldquo;Create a payment&amp;rdquo; twice creates two payments&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;PATCH&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;Not always&lt;/td&gt;&#xA;          &lt;td&gt;&amp;ldquo;Add 10 to the balance&amp;rdquo; twice adds 20&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;Idempotency keys exist to make &lt;code&gt;POST&lt;/code&gt; (and risky &lt;code&gt;PATCH&lt;/code&gt;) calls behave like&#xA;the safe ones.&lt;/p&gt;&#xA;&lt;h2 id=&#34;how-an-idempotency-key-works&#34;&gt;How an idempotency key works&lt;/h2&gt;&#xA;&lt;p&gt;The client sends a header with a value it generated, usually a random UUID:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;POST /payments&#xA;Idempotency-Key: 6f1c2a9e-4b7d-4d3e-9a51-0c8e2f3b7d10&#xA;Content-Type: application/json&#xA;&#xA;{&amp;#34;order_id&amp;#34;: 881, &amp;#34;amount&amp;#34;: &amp;#34;25.00&amp;#34;, &amp;#34;currency&amp;#34;: &amp;#34;EUR&amp;#34;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The server keeps a small table of keys it has seen:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/idempotency-keys-explained/how-a-key-works.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/idempotency-keys-explained/how-a-key-works.svg&#34; alt=&#34;Two flows side by side. First request: the client sends POST with key K, the server checks whether K was seen before (no), saves K as started, charges the card, saves the response with K, and returns 201 Created. Retry with the same key: the server finds that K was seen before (yes), reads the saved response, and returns 201 Created with the same body, so the card is charged once.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;484&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;There is an &lt;a href=&#34;https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/&#34;&gt;IETF draft for the &lt;code&gt;Idempotency-Key&lt;/code&gt;&#xA;header&lt;/a&gt;&#xA;that writes down the common behaviour, so you don&amp;rsquo;t have to invent your own&#xA;rules.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-four-cases-your-server-must-handle&#34;&gt;The four cases your server must handle&lt;/h2&gt;&#xA;&lt;p&gt;This table is the whole design. Print it out.&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/idempotency-keys-explained/four-cases.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/idempotency-keys-explained/four-cases.svg&#34; alt=&#34;A table of the four cases the server must handle. New key: run the request, save the result. Done key with the same body: return the saved result again, even if it was an error. Key still running: 409 Conflict, try again shortly. Key seen with a different body: 422, this key was used for a different request.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;368&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;The last row catches a real bug: a client that reuses one key for two&#xA;different actions. Without the check, the second payment would silently&#xA;return the first payment&amp;rsquo;s result, and nobody would notice until the&#xA;accounts don&amp;rsquo;t add up. To check it, store a hash of the request body with&#xA;the key.&lt;/p&gt;&#xA;&lt;h2 id=&#34;building-it-in-postgres&#34;&gt;Building it in Postgres&lt;/h2&gt;&#xA;&lt;h3 id=&#34;the-table&#34;&gt;The table&lt;/h3&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;CREATE&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;TABLE&lt;/span&gt; idempotency_keys (&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  account_id    &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;bigint&lt;/span&gt;      &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#ff79c6&#34;&gt;key&lt;/span&gt;           &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;text&lt;/span&gt;        &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  request_hash  &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;text&lt;/span&gt;        &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;,   &lt;span style=&#34;color:#6272a4&#34;&gt;-- sha256 of method + path + body&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;  status        &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;text&lt;/span&gt;        &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;,   &lt;span style=&#34;color:#6272a4&#34;&gt;-- &amp;#39;started&amp;#39; | &amp;#39;finished&amp;#39;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;  response_code &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;int&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  response_body jsonb,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  created_at    timestamptz &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;DEFAULT&lt;/span&gt; now(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#ff79c6&#34;&gt;PRIMARY&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;KEY&lt;/span&gt; (account_id, &lt;span style=&#34;color:#ff79c6&#34;&gt;key&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The key is scoped to the account. Two customers who happen to send the same&#xA;UUID don&amp;rsquo;t clash, and one customer cannot replay another customer&amp;rsquo;s response&#xA;by guessing their key.&lt;/p&gt;&#xA;&lt;h3 id=&#34;case-1-all-the-work-happens-in-your-database&#34;&gt;Case 1: all the work happens in your database&lt;/h3&gt;&#xA;&lt;p&gt;If &amp;ldquo;do the request&amp;rdquo; only means writing rows to the same database, this is&#xA;easy. Put everything in one transaction:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;BEGIN&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;INSERT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;INTO&lt;/span&gt; idempotency_keys (account_id, &lt;span style=&#34;color:#ff79c6&#34;&gt;key&lt;/span&gt;, request_hash, status)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;VALUES&lt;/span&gt; ($account, $&lt;span style=&#34;color:#ff79c6&#34;&gt;key&lt;/span&gt;, $hash, &lt;span style=&#34;color:#f1fa8c&#34;&gt;&amp;#39;started&amp;#39;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;ON&lt;/span&gt; CONFLICT (account_id, &lt;span style=&#34;color:#ff79c6&#34;&gt;key&lt;/span&gt;) &lt;span style=&#34;color:#ff79c6&#34;&gt;DO&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NOTHING&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;RETURNING &lt;span style=&#34;color:#ff79c6&#34;&gt;key&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;-- No row returned? The key exists. ROLLBACK, then read the saved row&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;-- and follow the four-case table.&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;INSERT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;INTO&lt;/span&gt; orders (account_id, order_id, amount) &lt;span style=&#34;color:#ff79c6&#34;&gt;VALUES&lt;/span&gt; (...);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;UPDATE&lt;/span&gt; idempotency_keys&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;SET&lt;/span&gt; status &lt;span style=&#34;color:#ff79c6&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f1fa8c&#34;&gt;&amp;#39;finished&amp;#39;&lt;/span&gt;, response_code &lt;span style=&#34;color:#ff79c6&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#bd93f9&#34;&gt;201&lt;/span&gt;, response_body &lt;span style=&#34;color:#ff79c6&#34;&gt;=&lt;/span&gt; $response&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;WHERE&lt;/span&gt; account_id &lt;span style=&#34;color:#ff79c6&#34;&gt;=&lt;/span&gt; $account &lt;span style=&#34;color:#ff79c6&#34;&gt;AND&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;key&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;=&lt;/span&gt; $&lt;span style=&#34;color:#ff79c6&#34;&gt;key&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;COMMIT&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Postgres quietly handles the hard race for you here. If two copies of the&#xA;same request arrive at the same moment, the second &lt;code&gt;INSERT ... ON CONFLICT&lt;/code&gt;&#xA;waits until the first transaction ends. If the first one commits, the second&#xA;sees the key and returns the saved response. If the first one rolls back,&#xA;the second goes ahead as if it came first. No double order either way.&lt;/p&gt;&#xA;&lt;h3 id=&#34;case-2-the-work-calls-someone-else&#34;&gt;Case 2: the work calls someone else&lt;/h3&gt;&#xA;&lt;p&gt;Now the hard one. Your endpoint charges a card through a payment provider.&#xA;That call is outside your database, so you cannot roll it back.&lt;/p&gt;&#xA;&lt;p&gt;You can&amp;rsquo;t hold a transaction open across a slow network call (it ties up a&#xA;connection and can block database cleanup). So you split the work into&#xA;steps and save progress between them:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/idempotency-keys-explained/provider-steps.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/idempotency-keys-explained/provider-steps.svg&#34; alt=&#34;Three steps for a request that calls a payment provider. Step 1: begin, insert the key as started, commit. The decision is now saved. Step 2: call the provider, passing your key along. Step 3: begin, save the providers answer and your order, mark the key finished with the response, commit.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;316&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;The important trick is in step 2: &lt;strong&gt;pass the idempotency key on to the&#xA;provider&lt;/strong&gt;. Most payment providers accept one. Now think about a crash:&lt;/p&gt;&#xA;&lt;table&gt;&#xA;  &lt;thead&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;th&gt;Crash happens&lt;/th&gt;&#xA;          &lt;th&gt;What the retry sees&lt;/th&gt;&#xA;          &lt;th&gt;What it does&lt;/th&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/thead&gt;&#xA;  &lt;tbody&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Before step 1 commits&lt;/td&gt;&#xA;          &lt;td&gt;No key&lt;/td&gt;&#xA;          &lt;td&gt;Starts fresh. Nothing was charged&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Between step 1 and 2&lt;/td&gt;&#xA;          &lt;td&gt;Key &lt;code&gt;started&lt;/code&gt;, old&lt;/td&gt;&#xA;          &lt;td&gt;Calls the provider. Not charged yet, so this charges once&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Between step 2 and 3&lt;/td&gt;&#xA;          &lt;td&gt;Key &lt;code&gt;started&lt;/code&gt;, old&lt;/td&gt;&#xA;          &lt;td&gt;Calls the provider again &lt;strong&gt;with the same key&lt;/strong&gt;. The provider returns the first charge instead of making a new one&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;After step 3&lt;/td&gt;&#xA;          &lt;td&gt;Key &lt;code&gt;finished&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;Returns the saved response&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;Every row ends with exactly one charge. That works only because the key&#xA;travels through every system that has a side effect.&lt;/p&gt;&#xA;&lt;p&gt;How does a retry know a &lt;code&gt;started&lt;/code&gt; key is &amp;ldquo;old&amp;rdquo; and not still running? Use&#xA;&lt;code&gt;created_at&lt;/code&gt;. If it is older than your request timeout (say 60 seconds), the&#xA;first attempt is dead and the retry can take over. If it is newer, answer&#xA;409 and let the client try again in a moment.&lt;/p&gt;&#xA;&lt;p&gt;Brandur Leach wrote a longer version of this with more steps, called&#xA;&lt;a href=&#34;https://brandur.org/idempotency-keys&#34;&gt;recovery points&lt;/a&gt;. It is worth reading&#xA;if your endpoint calls more than one outside system.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-client-side&#34;&gt;The client side&lt;/h2&gt;&#xA;&lt;p&gt;The server can only help if the client sends the key correctly. The most&#xA;common mistake:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/idempotency-keys-explained/client-keys.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/idempotency-keys-explained/client-keys.svg&#34; alt=&#34;Two ways a client can choose keys. Wrong, a new key per attempt: attempt 1 uses key a1b2 and times out, attempt 2 uses key c3d4 and is charged, attempt 3 uses key e5f6 and is charged again. Right, one key per action: attempt 1 uses key a1b2 and times out, attempts 2 and 3 reuse a1b2 and get the saved result.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;312&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;Generate the key when the user decides to act (taps &amp;ldquo;Pay&amp;rdquo;), store it with the&#xA;pending action, and reuse it for every retry. If the app restarts mid-retry,&#xA;it should pick up the same key from storage.&lt;/p&gt;&#xA;&lt;p&gt;Also:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Retry with growing waits plus a bit of randomness (for example 1s, 2s, 4s,&#xA;each plus up to 1s random). Without the randomness, thousands of clients&#xA;that failed together all retry at the same instant.&lt;/li&gt;&#xA;&lt;li&gt;Only retry on timeouts, connection errors, 409 and 5xx. A 400 or 422 will&#xA;fail the same way every time.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;how-long-to-keep-keys&#34;&gt;How long to keep keys&lt;/h2&gt;&#xA;&lt;p&gt;Keys don&amp;rsquo;t need to live forever. They only need to outlive any realistic&#xA;retry. &lt;a href=&#34;https://docs.stripe.com/api/idempotent_requests&#34;&gt;Stripe keeps them for at least 24&#xA;hours&lt;/a&gt;. Pick a window,&#xA;document it, and delete older rows with a daily job:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;DELETE&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;FROM&lt;/span&gt; idempotency_keys&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;WHERE&lt;/span&gt; created_at &lt;span style=&#34;color:#ff79c6&#34;&gt;&amp;lt;&lt;/span&gt; now() &lt;span style=&#34;color:#ff79c6&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;interval&lt;/span&gt; &lt;span style=&#34;color:#f1fa8c&#34;&gt;&amp;#39;24 hours&amp;#39;&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;mistakes-i-see-often&#34;&gt;Mistakes I see often&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Saving the key after the work.&lt;/strong&gt; If you crash in between, the retry finds&#xA;no key and does the work again. Save the decision first.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Keeping keys in a cache that can lose them.&lt;/strong&gt; If the key lives in Redis&#xA;and Redis restarts, your protection is gone. Keep the key in the same&#xA;database as the data it protects.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Not saving errors.&lt;/strong&gt; If the first attempt failed validation, a retry with&#xA;the same key should get the same error, not a fresh run. Some APIs choose&#xA;not to save 5xx errors so a retry can succeed. Either is fine if you&#xA;document it.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;No body hash.&lt;/strong&gt; Reusing a key for a different request should fail loudly,&#xA;not return the wrong result.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Global keys.&lt;/strong&gt; Scope keys to the caller, or one client can read another&#xA;client&amp;rsquo;s responses.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;you-dont-always-need-a-key&#34;&gt;You don&amp;rsquo;t always need a key&lt;/h2&gt;&#xA;&lt;p&gt;Sometimes the request already carries something unique. Then a &lt;code&gt;UNIQUE&lt;/code&gt;&#xA;constraint does the job:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;A message consumer can store each message id in a &lt;code&gt;processed_messages&lt;/code&gt;&#xA;table with a unique index, and skip ids it has seen. This is the usual&#xA;partner for the &lt;a href=&#34;https://loomloops.com/post/transactional-outbox-pattern/&#34;&gt;transactional outbox&#xA;pattern&lt;/a&gt;, which delivers messages at&#xA;least once.&lt;/li&gt;&#xA;&lt;li&gt;A background job can use the job id the same way. Jobs in a &lt;a href=&#34;https://loomloops.com/post/postgres-job-queue-skip-locked/&#34;&gt;Postgres job&#xA;queue&lt;/a&gt; can run twice after a crash,&#xA;so they need this.&lt;/li&gt;&#xA;&lt;li&gt;&amp;ldquo;Create the invoice for order 881&amp;rdquo; can use &lt;code&gt;UNIQUE (order_id)&lt;/code&gt; on the&#xA;invoices table.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;An idempotency key is what you use when the request has no natural unique&#xA;value, which is the usual case for &amp;ldquo;create a new thing&amp;rdquo; endpoints.&lt;/p&gt;&#xA;&lt;h2 id=&#34;questions-people-ask&#34;&gt;Questions people ask&lt;/h2&gt;&#xA;&lt;p&gt;&lt;strong&gt;Who creates the key, client or server?&lt;/strong&gt;&#xA;The client. Only the client knows that attempt 2 is a retry of attempt 1.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Should the key be a UUID?&lt;/strong&gt;&#xA;Any random string with enough length works. UUID v4 is the easy choice. Don&amp;rsquo;t&#xA;build keys from timestamps or counters, because two devices can produce the&#xA;same one.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Is PUT enough instead of POST with a key?&lt;/strong&gt;&#xA;Sometimes. If the client can choose the new resource&amp;rsquo;s id (&lt;code&gt;PUT /payments/6f1c...&lt;/code&gt;), the id itself acts as the key. Many APIs prefer POST&#xA;plus a key so the server keeps control of ids.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Does this give &amp;ldquo;exactly once&amp;rdquo;?&lt;/strong&gt;&#xA;It gives &amp;ldquo;the effect happens once&amp;rdquo;, which is what users care about. Under the&#xA;hood, requests still arrive more than once. The key makes the extra copies&#xA;harmless.&lt;/p&gt;&#xA;&lt;h2 id=&#34;cheat-sheet&#34;&gt;Cheat sheet&lt;/h2&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/idempotency-keys-explained/cheat-sheet.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/idempotency-keys-explained/cheat-sheet.svg&#34; alt=&#34;Cheat sheet, idempotency keys in one page. Header: Idempotency-Key: uuid. Who: the client makes it, once per user action. Store: (account_id, key) PRIMARY KEY plus a body hash. New key: run, save response. Done key: return saved response. Running: 409, client retries soon. Body diff: 422, key reused for a different request. Order: save the key BEFORE the side effect. Outside: pass your key on to the payment provider. Keep: 24 hours or more, then delete.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;488&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;h2 id=&#34;further-reading&#34;&gt;Further reading&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/&#34;&gt;IETF draft: The Idempotency-Key HTTP Header Field&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://docs.stripe.com/api/idempotent_requests&#34;&gt;Stripe API docs: idempotent requests&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://brandur.org/idempotency-keys&#34;&gt;Brandur Leach: Implementing Stripe-like idempotency keys in Postgres&lt;/a&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;</description>
    </item>
  </channel>
</rss>
