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.
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?
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.
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.
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 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.
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.
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.