How to Stop Webhook Replay Attacks in Node.js & PostgreSQL
Learn how to build a multi-layer defense against webhook replay attacks and duplicate payment processing using Node.js, Express, and PostgreSQL.

Stock photo for illustration only, not from the actual event
- Signature validation alone is not enough to stop webhook replay attacks.
- Using PostgreSQL with unique constraints guarantees database-level isolation against duplicates.
- Creating a dedicated webhook tracking table and indexes ensures lightning-fast event lookups.
- Express middleware inspects incoming payloads securely before executing touch points.
Handling payment webhooks sounds straightforward—until network retries hit your server three times in a row, or an attacker intercepts a valid payload and tries to credit their balance twice. Validating signatures is step one, but it won't protect you from a replay attack where a valid, signed payload gets resent. Here is how to set up a clean, multi-layer defense in Node.js with PostgreSQL to ensure every webhook payload runs exactly once.
You could store processed event IDs in Redis, but if your cache flushes or a container restarts during a high-traffic spike, you lose state. Using PostgreSQL with a unique constraint guarantees database-level isolation. If two identical requests hit your backend at the exact same millisecond, Postgres handles the lock and drops the duplicate.
Choosing a relational database like PostgreSQL over an in-memory cache like Redis for idempotency keys ensures strict data durability. Even during unexpected server restarts or traffic spikes, your transaction history remains intact, eliminating the risk of double-crediting users in critical financial workflows.
To implement event tracking, developers can deploy the following relational schema to log and filter incoming webhook requests:
- Create the processed_webhooks table containing id, event_id, signature, and created_at columns.
- Enforce a UNIQUE constraint on the event_id column to block duplicate entries.
- Build a database index on the event_id column for rapid incoming event verification.

Stock photo for illustration only, not from the actual event
The Express Middleware security check handles three things before touch point execution. Structuring webhook security this way protects your system even if payment providers trigger aggressive retry loops or malicious actors try resending old traffic.
Source: Dev.to
Found something wrong in this article? Report an issue with this article
Comments
Leave a Comment