<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdulrahman Aldahhas | ART OF INTELLIGENCE</title>
    <description>The latest articles on DEV Community by Abdulrahman Aldahhas | ART OF INTELLIGENCE (@aldahhasglobalboop).</description>
    <link>https://dev.to/aldahhasglobalboop</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4285512%2F6a7b4ce7-a02a-4bec-8417-999005aaaf6e.png</url>
      <title>DEV Community: Abdulrahman Aldahhas | ART OF INTELLIGENCE</title>
      <link>https://dev.to/aldahhasglobalboop</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aldahhasglobalboop"/>
    <language>en</language>
    <item>
      <title>Never send twice: an idempotency pattern for workflows that touch the outside world</title>
      <dc:creator>Abdulrahman Aldahhas | ART OF INTELLIGENCE</dc:creator>
      <pubDate>Sun, 06 Dec 2026 00:23:22 +0000</pubDate>
      <link>https://dev.to/aldahhasglobalboop/never-send-twice-an-idempotency-pattern-for-workflows-that-touch-the-outside-world-18cf</link>
      <guid>https://dev.to/aldahhasglobalboop/never-send-twice-an-idempotency-pattern-for-workflows-that-touch-the-outside-world-18cf</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem.&lt;/strong&gt; A workflow that sends an email, posts a message, creates an invoice or books a slot can run twice for the same item: a webhook is retried, an execution is resumed after a crash, two triggers overlap, or someone re-runs a failed execution. The result is a duplicate email to a customer, a double booking or a second charge.&lt;/p&gt;

&lt;p&gt;Retry settings do not solve this. Retrying is exactly what creates the duplicate when the first attempt &lt;em&gt;did&lt;/em&gt; reach the other system but the reply was lost.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pattern: reserve -&amp;gt; act -&amp;gt; confirm
&lt;/h2&gt;

&lt;p&gt;Give every side effect an &lt;strong&gt;idempotency key&lt;/strong&gt; and record it in a table &lt;em&gt;before&lt;/em&gt; acting.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Key.&lt;/strong&gt; Build a deterministic key from what makes the action unique, for example &lt;code&gt;channel | recipient | business id&lt;/code&gt; (&lt;code&gt;email|ana@acme.com|invoice-1042&lt;/code&gt;). Hash it if it is long.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reserve atomically.&lt;/strong&gt; Insert the key into a table with a UNIQUE constraint. Only the execution whose insert succeeds may act. Everyone else stops.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Act.&lt;/strong&gt; Send the email / call the API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confirm.&lt;/strong&gt; Mark the row &lt;code&gt;sent&lt;/code&gt; with the provider's id (message id, booking id).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unknown outcome = do not retry blindly.&lt;/strong&gt; If the execution dies between 3 and 4, the row stays &lt;code&gt;reserved&lt;/code&gt;. After a timeout, treat it as &lt;code&gt;uncertain&lt;/code&gt;: first &lt;strong&gt;check the other system&lt;/strong&gt; (is the email in Sent? does the booking exist?), then either confirm it or mark it &lt;code&gt;failed&lt;/code&gt; and allow a new attempt.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Postgres
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;side_effects&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;key&lt;/span&gt;        &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;state&lt;/span&gt;      &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'reserved'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;-- reserved | sent | failed | uncertain&lt;/span&gt;
  &lt;span class="n"&gt;proof&lt;/span&gt;      &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Reserve: returns a row only for the ONE execution that wins&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="nf"&gt;side_effects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Confirm&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;side_effects&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'sent'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;proof&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Stale reservations become 'uncertain' (verify before any retry)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;side_effects&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'uncertain'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'reserved'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'2 hours'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  In n8n
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code / Crypto node:&lt;/strong&gt; build the key (&lt;code&gt;{{$json.channel}}|{{$json.to}}|{{$json.businessId}}&lt;/code&gt;), optionally SHA-256 it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Postgres node (Execute Query):&lt;/strong&gt; the &lt;code&gt;INSERT ... ON CONFLICT DO NOTHING RETURNING key&lt;/code&gt; above.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IF node:&lt;/strong&gt; continue only if the query returned a row. The other branch ends quietly (it is a duplicate).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your action node&lt;/strong&gt; (Gmail, HTTP Request, Calendly ...). Turn on "Continue on fail" so you can record the outcome.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Postgres node:&lt;/strong&gt; confirm &lt;code&gt;sent&lt;/code&gt; with the provider id, or &lt;code&gt;failed&lt;/code&gt; only when the response proves nothing was sent (for example a 4xx validation error). A timeout is &lt;strong&gt;not&lt;/strong&gt; proof of failure: leave it for the uncertain check.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A small scheduled workflow&lt;/strong&gt; runs the stale -&amp;gt; uncertain update and, for each uncertain row, checks the other system before allowing a retry.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No Postgres? The same works with any store that has an atomic "insert if absent" (a unique index in MySQL/SQLite, a Redis &lt;code&gt;SET key value NX&lt;/code&gt;, an Airtable/Sheets lookup is &lt;strong&gt;not&lt;/strong&gt; atomic under parallel runs).&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it holds under concurrency
&lt;/h3&gt;

&lt;p&gt;The UNIQUE constraint is enforced by the database, not by the workflow, so two parallel executions cannot both win the insert. We tested the same reserve/confirm design with 12 separate processes racing for one recipient, five rounds: exactly one winner each round, and every later attempt was refused.&lt;/p&gt;




&lt;p&gt;If a workflow of yours is sending duplicates or you are not sure what happens on a retry, we fix this at a fixed, written scope: ART OF INTELLIGENCE, &lt;a href="https://art-of-intelligence.web.app/en/services/api-integration/?utm_source=devto&amp;amp;utm_medium=content&amp;amp;utm_campaign=zc-2026-12&amp;amp;utm_content=never-send-twice" rel="noopener noreferrer"&gt;https://art-of-intelligence.web.app/en/services/api-integration/?utm_source=devto&amp;amp;utm_medium=content&amp;amp;utm_campaign=zc-2026-12&amp;amp;utm_content=never-send-twice&lt;/a&gt;&lt;/p&gt;

</description>
      <category>automation</category>
      <category>n8n</category>
      <category>postgres</category>
      <category>webhooks</category>
    </item>
  </channel>
</rss>
