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.
The trap
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.
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 customer.subscription.updated 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.
Two problems that look alike
- Duplicates: same event, delivered twice. Fix: remember event ids.
- Reordering: different events, wrong order. Fix: don't trust the payload.
Here is the reordering case, first with only event ids, then with a re-fetch and a revision check:
What I do instead
Treat the event as a signal that a subscription changed, not as the new state.
- Verify the signature on the raw body.
- Record the event id (this handles duplicates).
- Re-fetch the subscription from Stripe by id.
- Write it only if the row hasn't changed since you read it.
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:
UPDATE subscriptions
SET status = $1, revision = revision + 1
WHERE stripe_subscription_id = $2 AND revision = $3;
If that updates 0 rows, someone else wrote first. Re-read and retry, or return a 5xx so Stripe redelivers.
What I measured
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 failed, 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.
Update (28 Nov)
Two readers improved this in the comments, and their points are in the repo now.
- Sunnatulla compared a per-subscription advisory lock with this approach. I added the same comparison to my load test: one subscription hit by 100 connections while 50 others also get events, a pool of 20 connections. The lock had 0 failures on the busy subscription, but its waiters held every connection and the other 50 subscriptions went from about 5,100 writes to 100. Compare-and-swap failed 57% on the busy one and left the others alone. Numbers: LOAD_TEST.md. Tradeoff: WHY_NOT_A_LOCK.md.
-
Arthur pointed out the other failure: a webhook that never arrives, so nothing triggers a fix. There is now
reconcileRecent({ since }), which you run on a schedule. It lists Stripe's subscription events since a time and repairs each subscription once (example). He also noted that one Stripe fetch per event can hit rate limits during a bulk change. That is now written down in KNOWN_LIMITATIONS.md. I haven't built the queue he suggested.
Limits
- It is a reference implementation, not an audited library.
- The current code (v0.2.0, released 27 Nov) 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.
- It ignores Connect events and keeps live and test mode separate.
It's open source under the MIT license. Code and tests: https://github.com/navalpro8-lgtm/stripe-subscription-sync
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.

Top comments (17)
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.
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.
Update: I added a lock variant to the load test, set up like yours: 100 connections on one subscription, 50 others getting events at the same time, a pool of 20, fake Stripe at 20-80ms. The lock had 0 failures on the hot subscription, but the other 50 got 100 writes instead of about 5,100. The CAS version failed 57% on the hot one and left the rest alone. So same result as yours. You're credited for the comparison.
Numbers: github.com/navalpro8-lgtm/stripe-s...
Tradeoff: github.com/navalpro8-lgtm/stripe-s...
I didn't try a separate pool for lock waiters or a try-lock with a timeout. Did you test either of those?
Ran both on the same setup. A waiter pool of 3 behind a try-lock kept the cold subs fast at p50 55ms, but only 503 writes instead of about 2,700, because any cold event that collided queued behind the hot waiters (p99 5s). lock_timeout 200ms was worse, cold fell to 216 since waiters still hold a main pool connection while they wait. Plain try-lock and a 503 on busy matched CAS on cold (2,860) with 0 stale rows, but the hot sub bounced 71k times, so it only works if Stripe's redelivery backoff is the queue.
This is exactly the comparison I was missing, thanks for running it.
The numbers make sense once I think about where the cost moved. With pool=3, cold events that collide with hot waiters queue behind them, so part of the "safety" is just borrowed from cold traffic instead of the main pool, hence your p99 5s. lock_timeout 200ms being worse makes sense too: the waiter still holds a main connection while it counts down, so it's pure overhead with no payoff.
The plain try-lock plus a 503 on busy is the one I want in the repo. Matching CAS on cold writes with 0 stale rows is a real option, and "only works if Stripe's redelivery backoff is the queue" is the honest caveat, since 71k bounces on the hot subscription needs Stripe to not give up or rate-limit the endpoint. I'll add all three variants to LOAD_TEST.md with these numbers and credit you. Want to see the draft before I publish it?
yeah sure, drop the link here when it's up.
One thing I'd put next to the 71k number. In my test a bounced event came straight back, but live Stripe retries a failed delivery with exponential backoff for up to 3 days, so an event that got a 503 on the hot sub can land minutes or hours later. The CAS still keeps it from going stale, it's just that the row for that customer lags behind until the retry shows up, and that's worth one line in LOAD_TEST.md.
Good catch, 3 days is way longer than I pictured when I wrote "can lag until then." Putting that number in WHY_NOT_A_LOCK.md instead of leaving it vague. Appreciate you pushing on this.
And here's the draft, forgot to drop it above: github.com/navalpro8-lgtm/stripe-s... and github.com/navalpro8-lgtm/stripe-s... — all three variants in there with your numbers and credit. Let me know if I got anything wrong.
@sunnatulla_dev one question when you have a minute. Do you run Stripe subscriptions in production, and what did getting webhook handling right cost you in hours or bugs? I'm trying to work out if a tested starter that handles this out of the box is worth paying for, say $30. A plain no helps me as much as a yes.
Honest answer: no, not in production for a client. I hit it in a small test setup, where an old subscription.updated event arriving after a newer one flipped the status back, so I can't give you real hours or bug counts. The part that cost me the most time was the ordering check, not the signature or dedupe. If you build the starter, I'd test that case first.
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.
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?
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.
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.
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.
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.
Agreed, re-fetching is the one I went with. I skipped the timestamp compare because
createdis 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.