Engineering

Handling 500 Webhooks/Sec: Redis Mutexes vs Postgres Advisory Locks for Paystack Idempotency

C
Chidi OkekeVP of Frontend Engineering
October 21, 20265 min read
Handling 500 Webhooks/Sec: Redis Mutexes vs Postgres Advisory Locks for Paystack Idempotency

When high-volume payment webhooks collide under high concurrency, standard unique database constraints fall apart. Here is how we engineered a zero-duplicate ledger engine for a Nigerian fintech handling 500 Paystack webhooks per second.

During flash sales and end-of-month salary drops in Nigeria, payment providers like Paystack, Flutterwave, and Monnify process hundreds of thousands of transactions within tight time windows. To notify merchant systems of successful payments, these gateways fire HTTP webhooks. If your endpoint takes longer than two to three seconds to acknowledge receipt—or if a transient network packet drop occurs on the gateway's edge—the payment provider automatically retries the delivery.

At 10:00 AM on a Friday payday, a client operating a high-volume merchant collection platform hit a wall. Paystack fired 450 webhooks per second into their ingestion service. Due to a momentary microservice slowdown, Paystack's delivery engine assumed the webhooks had timed out and immediately re-sent them. The client's ingestion system received identical payment events separated by less than 12 milliseconds.

Because their idempotency logic relied on a simple SELECT query followed by an INSERT, both concurrent workers saw that no ledger entry existed for transaction reference tx_ref_994821. Both workers proceeded to credit the user's digital wallet. Within 45 minutes, the system double-credited ₦14.2 million across 380 user accounts.

This case study details how Neobot Tech diagnosed this distributed race condition, why naive locking mechanisms fail at scale, and the architectural pattern we implemented to guarantee exactly-once ledger execution without crippling database throughput.


The Situation & Architectural Constraints

The client's architecture was straightforward: an API gateway routed inbound webhooks to a cluster of four Go application instances running on AWS ECS. These instances parsed the incoming payload, validated the signature header, verified the transaction status, and recorded a double-entry ledger credit to PostgreSQL.

[ Paystack Webhook Engine ]
            │
            ▼ (450 req/sec + retry spikes)
   [ AWS ALB Gateway ]
            │
      ┌─────┴─────┐
      ▼           ▼
 [ Go Worker 1 ] [ Go Worker 2 ]
      │           │
      └─────┬─────┘
            ▼
  [ PostgreSQL Ledger DB ]

The target constraints for our re-engineering effort were non-negotiable:

  1. Zero Double-Credits: The system must process exactly one credit event per reference identifier provided by Paystack's Webhook API, regardless of how many duplicate payloads hit the edge.
  2. Sub-100ms Latency: The webhook ingestion worker must respond to Paystack with an HTTP 200 within 100 milliseconds to prevent Paystack's retry scheduler from escalating retry frequencies.
  3. PostgreSQL Connection Pool Efficiency: Lock management could not exhaust the database connection pool or starve read operations under 20,000 concurrent user sessions.
  4. Graceful Handling of Edge Timeouts: If a worker node crashes mid-transaction, locks must automatically expire without leaving payments in an orphaned or locked state indefinitely.

What We Tried First (And Why It Failed)

Attempt 1: PostgreSQL UNIQUE Constraints and Catching Code 23505

The initial developer attempt was to rely on a UNIQUE constraint on the ledger_entries.reference column. The logic was simple: attempt to insert the record directly; if PostgreSQL returns error code 23505 (unique_violation), swallow the error and return an HTTP 200 to Paystack.

While this prevented duplicate rows from being saved, it created severe collateral damage:

  • Generating unique constraint errors inside active multi-statement database transactions aborts the entire transaction block (PGERROR 25P02: current transaction is aborted, commands ignored until end of transaction block).
  • When thousands of duplicate webhooks hit the endpoint simultaneously, PostgreSQL was spent managing lock contention and index row verification rather than executing useful work.
  • HTTP response codes intermittently defaulted to 500 during unhandled exception rollbacks, causing Paystack to categorize the endpoint as unhealthy and amplify retry backoffs.

Attempt 2: Naive Redis SETNX Locks

To offload locking overhead from PostgreSQL, we implemented a distributed mutex using Redis SET key value NX PX 5000 via the standard distributed lock pattern documented in the Redis Distributed Locks Guide.

1. LockKey = 

Neobot Engineering Standard

Every system deployed by Neobot Tech incorporates enterprise baseline practices. We continuously audit our database topologies, REST API query paths, and frontend modular bundles to prevent latency spikes and ensure top-tier security posture.

Tags:#Engineering#PostgreSQL#Redis#Paystack#Distributed Systems#Go#System Design

Discussion

Comments Coming Soon

We are currently migrating our discussion engine to a new real-time database schema. Check back shortly to join the conversation.