<?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: naval</title>
    <description>The latest articles on DEV Community by naval (@navalpro8lgtm).</description>
    <link>https://dev.to/navalpro8lgtm</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%2F4268260%2F58d3d8e1-00c6-4530-a048-f680c29702b2.png</url>
      <title>DEV Community: naval</title>
      <link>https://dev.to/navalpro8lgtm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/navalpro8lgtm"/>
    <language>en</language>
    <item>
      <title>Deduplicating Stripe events doesn't fix out-of-order subscription updates</title>
      <dc:creator>naval</dc:creator>
      <pubDate>Fri, 27 Nov 2026 06:31:02 +0000</pubDate>
      <link>https://dev.to/navalpro8lgtm/deduplicating-stripe-events-doesnt-fix-out-of-order-subscription-updates-ign</link>
      <guid>https://dev.to/navalpro8lgtm/deduplicating-stripe-events-doesnt-fix-out-of-order-subscription-updates-ign</guid>
      <description>&lt;p&gt;I'm building an open source Stripe subscription sync for Postgres (MIT license) and kept hitting the same trap, so here it is written down.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trap
&lt;/h2&gt;

&lt;p&gt;The usual advice is to store each Stripe event id and skip ones you've already processed. That works for one problem: the same event delivered twice.&lt;/p&gt;

&lt;p&gt;It does nothing for a different problem. Stripe doesn't guarantee delivery order, and it retries failed deliveries for up to three days. So an older &lt;code&gt;customer.subscription.updated&lt;/code&gt; can arrive after a newer one. They have different event ids. Your dedupe table says "never seen this one", the handler writes the status from the payload, and the newer state is overwritten. The handler returns 200 both times, so nothing alerts you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two problems that look alike
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Duplicates:&lt;/strong&gt; same event, delivered twice. Fix: remember event ids.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reordering:&lt;/strong&gt; different events, wrong order. Fix: don't trust the payload.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the reordering case, first with only event ids, then with a re-fetch and a revision check:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frxjxj9xlq12jpy4w0az2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frxjxj9xlq12jpy4w0az2.gif" alt="Animation. With only event ids, an old &amp;quot;active&amp;quot; event overwrites a newer &amp;quot;canceled&amp;quot; one. With a re-fetch from Stripe and a revision check, the row stays canceled." width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I do instead
&lt;/h2&gt;

&lt;p&gt;Treat the event as a signal that a subscription changed, not as the new state.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Verify the signature on the raw body.&lt;/li&gt;
&lt;li&gt;Record the event id (this handles duplicates).&lt;/li&gt;
&lt;li&gt;Re-fetch the subscription from Stripe by id.&lt;/li&gt;
&lt;li&gt;Write it only if the row hasn't changed since you read it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 4 matters. Re-fetching alone still has a race: two deliveries can both fetch, the newer one writes first, and the older read writes last. A conditional write closes it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;subscriptions&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&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="n"&gt;revision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;revision&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;stripe_subscription_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;revision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If that updates 0 rows, someone else wrote first. Re-read and retry, or return a 5xx so Stripe redelivers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I measured
&lt;/h2&gt;

&lt;p&gt;I ran a synthetic load test: 100 connections sending events for one subscription at the same time. 56% of the events ran out of retries and were left as &lt;code&gt;failed&lt;/code&gt;, to be redelivered by Stripe. Spread over 50 subscriptions, only 0.5% failed. A real subscription usually gets a burst of 2 to 5 events, not 100, so this is a worst case, not what you'd see in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a reference implementation, not an audited library.&lt;/li&gt;
&lt;li&gt;The current code (v0.2.0, not released yet) is tested in CI and locally on Postgres 17 with Node 22 and 24. I ran the older v0.1.0 by hand against a Stripe sandbox and a Supabase project. I have not repeated that run for v0.2.0.&lt;/li&gt;
&lt;li&gt;It ignores Connect events and keeps live and test mode separate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's open source under the MIT license. Code and tests: &lt;a href="https://github.com/navalpro8-lgtm/stripe-subscription-sync" rel="noopener noreferrer"&gt;https://github.com/navalpro8-lgtm/stripe-subscription-sync&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you've handled this differently (row locks, queues, a per-customer lock), I'd like to hear what held up. Break it if you can.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>stripe</category>
      <category>postgres</category>
      <category>webhooks</category>
    </item>
  </channel>
</rss>
