Stripe webhooks look straightforward during local development with the Stripe CLI. You verify the signature, parse event.type , update a row in PostgreSQL, and return 200 OK .
That approach breaks in production. Stripe guarantees at-least-once delivery, not exactly-once delivery. Under network retries, bursty checkout traffic, or overlapping events ( checkout.session.completed arriving milliseconds before or after invoice.paid ), a naive handler will double-credit user balances, create duplicate subscriptions, or trigger deadlocks.
Here is how to build an idempotent, concurrency-safe webhook handler in Node.js and PostgreSQL.
1. Track Processed Events with a Unique Constraint
In-memory deduplication ( Set or Redis with a short TTL) fails as soon as you run multiple Node.js instances or restart a container.…