Eliminating Race Conditions in High-Throughput Webhook Ingestion: Atomic Redis Lua vs. Database Row Locks
Back to Insights •Systems Architecture

Eliminating Race Conditions in High-Throughput Webhook Ingestion: Atomic Redis Lua vs. Database Row Locks

K
Kaprin Systems Lab
•Oct 21, 2026•14 min read

High-volume digital networks face a brutal architectural contradiction. On one side stand upstream publishers delivering hundreds of concurrent webhook requests per second, enforcing strict sub-500 millisecond response SLAs. On the other side stand downstream enterprise buyers who enforce strict hourly and daily volume caps, refusing to pay for even a single lead delivered past their contracted limit.

Under baseline traffic, traditional architectures function acceptably. But when a major publisher pushes a marketing surge of 150 to 200 concurrent webhooks per second, naive database architectures collapse under one of two critical failure modes:

  • Failure Mode A (The Race Condition / Cap Leakage): The application performs a non-locking query (such as SELECT COUNT(*) FROM leads WHERE buyer_id = 12 AND date = TODAY). Twenty parallel worker processes execute the query simultaneously, all see an available balance of 2 leads, and all 20 deliver their leads downstream. The buyer receives 18 leads over their cap and refuses payment.
  • Failure Mode B (The Deadlock & Latency Explosion): To prevent over-delivery, the engineering team adds pessimistic database locking (SELECT ... FOR UPDATE). Now, 200 concurrent workers serialize behind the lock. Database connection pools exhaust, p95 latency blows past 4 seconds, and upstream publishers drop the connection, triggering catastrophic traffic penalties.

The Concurrency Matrix: Evaluating Architectural Approaches

Here is how four different concurrency strategies perform under high-throughput burst conditions:

Concurrency Strategy Ingestion Latency (p95) Cap Leakage Risk Database Load Production Viability at 200 req/sec
Naive SELECT COUNT(*) 120ms to 250ms Severe (15% to 40% over-delivery) High (Table scans on count queries) Fails (Severe financial loss)
Pessimistic Row Lock (FOR UPDATE) 1,800ms to 5,500ms Zero Catastrophic (Lock contention & pool exhaustion) Fails (Breaches publisher latency SLAs)
Distributed Locking (Redlock) 450ms to 900ms Low (< 0.5%) Moderate Fragile (High network round-trip overhead)
Atomic Redis Lua Leases (Kaprin) < 220ms (p95) / < 380ms (p99) 0.0% (Zero leakage) Near Zero (Asynchronous settlement) Production Proven (Solidex Engine)

The Production Blueprint: Two-Phase Atomic Routing

To eliminate this bottleneck in high-throughput production (such as the Solidex platform engineered by Kaprin, which processes 40,000 to 200,000 daily events), we decouple real-time cap reservations from permanent database storage.

Phase 1: In-Memory Static Rule Filtering

Before touching any counter, candidate buyers are filtered in memory against static rules: geographic jurisdiction, publisher blacklists, and timezone-aware operating schedules. This eliminates 60% of unnecessary cap checks instantly.

Phase 2: Atomic Cap Leases via Redis Lua

Because Redis is single-threaded, a Lua script executes atomically. No other command can run while the script executes. The script checks the current count against the active cap limit, increments the counter if available, and sets an expiration timestamp in a single atomic step that executes in under 2 milliseconds.

-- Redis Lua Script: Atomic Cap Verification & Reservation
local cap_key = KEYS[1]
local limit = tonumber(ARGV[1])
local ttl = tonumber(ARGV[2])

local current = redis.call('GET', cap_key)
if current and tonumber(current) >= limit then
    return 0 -- Cap reached, reject lease
else
    local new_val = redis.call('INCR', cap_key)
    if new_val == 1 and ttl > 0 then
        redis.call('EXPIRE', cap_key, ttl)
    end
    return 1 -- Lease granted atomically
end

Phase 3: Circuit Breaking and Atomic Refund Compensation

When the application receives a successful lease from Redis, it dispatches the webhook payload downstream to the winning buyer with a strict 1.5-second HTTP timeout. If the downstream API times out, returns a 500 error, or rejects the lead, the application immediately executes an atomic decrement (refund) on the Redis counter and cascades to the next buyer in the waterfall.

Phase 4: Asynchronous Relational Ledger Settlement

The HTTP intake worker immediately returns a 200 OK response to the upstream publisher in under 250 milliseconds. Heavy database writes (storing raw payloads, updating audit logs, recording publisher commission balances) are pushed to background queues (e.g. BullMQ / Laravel Queue workers). A scheduled background cron reconciles Redis counters against relational MySQL rows hourly to eliminate any drift.

Production Stress Test Results

During architectural vetting on the Solidex platform, the system was subjected to an aggressive chaos test: an advertiser was configured with a strict limit of 100 leads, and hammered with 1,000 concurrent webhooks across 50 threads over 5 seconds.

Exactly 100
Delivered Leads

Zero cap leakage. Exactly 100 leads accepted, 900 cleanly cascaded down the waterfall.

< 220ms
p95 Intake Latency

Intake endpoint returned HTTP 200 in sub-300ms throughout the entire traffic burst.

0.0%
Relational Drift

Zero variance between Redis atomic counters and relational database audit rows.

Conclusion: Architecture at Scale

High-throughput engineering is not about throwing larger servers at bad queries. It is about understanding the boundaries between in-memory atomic speed and persistent relational storage. Decoupling real-time cap gating from background ledger writes is how resilient modern systems process hundreds of concurrent events without latency penalties or unbillable over-delivery.

Ready to transform your business?