HeadlinesBriefing favicon HeadlinesBriefing.com

Fixing Stripe Webhook Race Conditions with Queues

DEV Community •
×

Developers at a high‑traffic SaaS discovered that users were receiving duplicate credits, subscriptions lingered in “pending”, and some purchases vanished after a Redis hiccup. The culprit was an eager‑sync pattern that let both the checkout return handler and the Stripe webhook write to the same wallet, creating a classic race condition. Their controller had ballooned to 3,769 lines.

The team replaced the monolithic handler with a thin endpoint that only verifies the signature and pushes the event onto a BullMQ queue. A single worker then processes each job, using MongoDB’s unique stripeSessionId check for true idempotency. Code shrank to 47 lines, duplicate transactions vanished, and Stripe‑retry windows are now respected.

Eliminating the race condition matters because payment glitches erode trust and inflate support costs for any platform handling recurring or one‑time Stripe charges. With a single writer, teams can monitor queue health, add back‑off policies, and focus on downstream features. Next up, the engineers will tackle wallet balance races.