DEV Community

Deduplicating Stripe events doesn't fix out-of-order subscription updates

naval on November 27, 2026

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. ...
Collapse
 
sunnatulla_dev profile image
Sunnatulla •

On the per-subscription lock, I modeled it with PG17, a pool of 20 and a fake Stripe at 20-80ms. With 100 workers on one subscription, an advisory lock held across the fetch had 0 failures there, but its waiters took every pool connection and 50 other subscriptions fell from 2705 writes to 60. Your CAS loop failed 53% on the hot one and left the rest alone.

Collapse
 
navalpro8lgtm profile image
naval •

you said to test the ordering case first. the demo does that: it delivers a late update after a newer one and checks the row. git clone https://github.com/navalpro8-lgtm/stripe-subscription-sync && cd stripe-subscription-sync && npm ci && npm run demo, about a minute, no stripe account. does it match what you saw in your test setup? tell me what's missing, even if it's one line

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

I read demo.mjs rather than running it. Case 2 is the cancel one, and the tombstone is what refuses the late event there. Mine never involved a cancel: past_due to active, then an older update put past_due back. Your stale_read_contradicting_the_event test covers exactly that, so I'd show it as case 4 in the demo, since there's no tombstone to save you in that version.

Collapse
 
navalpro8lgtm profile image
naval •

you're right, case 2 is the cancel one so the tombstone is what saves it there. the past_due flip has no tombstone, which makes it the better demo. the stale read test already covers it, so i'll add it as case 4

Collapse
 
navalpro8lgtm profile image
naval •

@sunnatulla_dev case 4 is in now. past_due to active, the first two reads from stripe still say past_due, nothing stale gets written and there's no tombstone involved. npm run demo shows it

Collapse
 
navalpro8lgtm profile image
naval •

Thanks for actually running it. Your 53% on the hot subscription is close to the 56% I got in my own load test, which is useful to see from someone else's model. The part I hadn't measured is the cost of the advisory lock: waiters eating the pool and dropping the other 50 subscriptions from 2705 writes to 60. That is a real tradeoff against my CAS loop. Is your script somewhere I can read? I'd like to run the same comparison against the repo and add it to the load-test notes, with a credit to you.

Collapse
 
keyharbor profile image
Arthur | Keyharbor •

Treating the event as a signal and re-fetching is the pattern I'd recommend too, and the revision check is the part most write-ups skip.

Two additions. Re-fetching on every event can run into Stripe's API rate limits during bursts, like a bulk price change, so a short per-subscription queue that collapses several pending events into one fetch helps. And a scheduled reconcile job that lists recently updated subscriptions catches the other failure mode, where a webhook never arrives at all and your row stays stale with nothing to trigger a fix.

Collapse
 
navalpro8lgtm profile image
naval •

your ack and 429 notes ended up in KNOWN_LIMITATIONS. if you have a minute, npm run demo in the repo runs the late, duplicate and unlinked cases against a real postgres, no stripe account. anything you'd expect it to cover and it doesn't?

Collapse
 
navalpro8lgtm profile image
naval •

Update: both are in now. There's a new reconcileRecent({ since }) that lists Stripe's customer.subscription.* events since a time and reconciles each subscription once. It skips rows that already match Stripe. The README has an hourly cron example that looks back two hours. One limit: Stripe keeps events about 30 days, so anything older needs reconcileCustomer. The example typechecks but I haven't run it on Vercel yet.

Rate limits are written up in KNOWN_LIMITATIONS, with a test that pins it at one fetch per event. I didn't build the merging queue.

Reconcile: github.com/navalpro8-lgtm/stripe-s...
Rate limits: github.com/navalpro8-lgtm/stripe-s...

If listing events is the wrong way to find what changed, I'd like to hear what you use.

Collapse
 
navalpro8lgtm profile image
naval •

Both fair, and neither is solved in the repo yet. The reconcile functions exist, but nothing calls them on a schedule, and I haven't documented that clearly. You have to wire up the cron yourself. I'll add a README section with an example job. A per-subscription queue that collapses pending events isn't built either. With the order shortcut off, each event does its own fetch, so a bulk price change could hit rate limits. Have you seen that happen in practice, and how many events per second before it hurt?

Collapse
 
keyharbor profile image
Arthur | Keyharbor •

Honest answer: I don't have a measured number from my own setup. What I'd go by is Stripe's documented default limit, around 100 operations per second in live mode and 25 in test mode, shared across every API call the account makes, with a 429 when you go over.

So steady traffic is rarely the issue. The risky case is a burst: a price migration that touches a few thousand subscriptions emits that many events in a short window, and each re-fetch competes with your checkout calls. Retrying 429s with backoff, and acknowledging the webhook before doing the fetch, gets you most of the way before a collapsing queue is worth building.

Thread Thread
 
navalpro8lgtm profile image
naval •

That helps, thanks. Acknowledging before the fetch makes sense, though then a failed fetch never gets redelivered, so that's where the scheduled reconcile earns its keep. I'll add the 429 backoff to the known-limitations doc.

Collapse
 
bedirhandev profile image
Bedirhan Elibol •

Deduplication only handles replays. For ordering, fetch the current object from the Stripe API inside the handler and write that state, so a late event can't overwrite a newer one. Comparing the event's created timestamp with the stored one is the other common guard.

Collapse
 
navalpro8lgtm profile image
naval •

you suggested comparing created. i went with a revision check on the row instead. if you want to try to break it, npm run demo replays a late and a duplicate event in about a minute, and i'd like to know the case where created would have caught something mine doesn't

Collapse
 
navalpro8lgtm profile image
naval •

Agreed, re-fetching is the one I went with. I skipped the timestamp compare because created is in whole seconds, so two updates in the same second tie. Even with a re-fetch, two handlers can race on the write, so the update also checks a revision number and retries if someone wrote first.