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.
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
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.
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
@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
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.
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.
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?
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.
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.
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.
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
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.
We're a place where coders share, stay up-to-date and grow their careers.
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.
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 lineI 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.
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
@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 demoshows itThanks 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.
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.
your ack and 429 notes ended up in KNOWN_LIMITATIONS. if you have a minute,
npm run demoin 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?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.
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.
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.
you suggested comparing
created. i went with a revision check on the row instead. if you want to try to break it,npm run demoreplays a late and a duplicate event in about a minute, and i'd like to know the case wherecreatedwould have caught something mine doesn'tAgreed, 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.