← BACK_TO_LOGS

System Design: Decoupling Payment Webhooks from Database Writes with Queues

3 min readKANAT NAZAROV
System Design: Decoupling Payment Webhooks from Database Writes with Queues

When building high-concurrency payment infrastructure, backend engineers face a fundamental tension: API Response Speed vs. Data Safety.

Payment providers and card networks enforce strict SLA timeouts on webhooks. If your server takes too long to process an incoming payload—or fails to respond during peak traffic—the provider assumes a failure, leading to dropped events, network retries, or outright declined transactions.

If your backend attempts to process incoming payment events synchronously within a heavy database transaction, you risk connection pool exhaustion, database lock contention, and latency spikes.

To solve this, modern backends use an Asynchronous Queue Pattern powered by in-memory stores like Redis and job orchestrators like BullMQ.

🛠️ The Architecture: Fast Path vs. Safe Path

Instead of hitting the primary database inside the HTTP execution loop, the system splits processing into two decoupled steps:

The Architecture: Fast Path vs. Safe Path


Code Architecture (NestJS + BullMQ Example)

1. The Ingestion Layer (Fast Path)

The HTTP controller handles idempotency, validates the request, pushes the payload to the queue, and returns an immediate response.

typescript

2. The Persistence Layer (Safe Path)

In the background, dedicated worker processes pull jobs off the queue and write them to the persistent relational database inside a strict transaction.

typescript

🔑 Core Benefits of This Pattern

  • Predictable Latency: Webhook responses drop from hundreds of milliseconds to under 20ms, preventing webhook provider timeouts.
  • Database Isolation: Sudden traffic spikes are buffered in the queue rather than overwhelming database connection pools.
  • Fault Tolerance: If the database undergoes maintenance or experiences transient errors, incoming events remain safely queued in Redis and are automatically retried via exponential backoff.
  • Strict Idempotency: Combining Redis key locks with deterministic queue jobId parameters ensures events are never processed or credited twice.

Leave a comment

Kanat Nazarov

Kanat Nazarov

Systems & Full-Stack Engineer
Kanat Nazarov is a product-minded Full-Stack Engineer. He is the co-founder of GetFusionChat, an enterprise-grade multi-tenant communication SaaS platform. Beyond architecting low-latency infrastructures and advanced AI systems, Kanat is a dedicated student of peak performance—optimizing his lifestyle through rigorous fitness, precise nutrition, and creative production in music. He operates on the core philosophy that breakthrough technical results stem from a high-functioning, deeply disciplined life.