Skip to content
Security·2 min read·November 24, 2026

Distributed API Rate Limiting with Atomic Redis Lua Token Buckets

Protecting multitenant APIs from burst scraping and cascading origin overload using atomic Redis Lua token bucket scripts and clean RFC-compliant retry headers.

A
By Anjali
·November 24, 2026·2 min read
Architecture Note
Distributed API Rate Limiting with Atomic Redis Lua Token Buckets

## Fixed-Window Boundary Spikes Explained

Simple counter-per-minute rate limiters fail at window seams. If an abusive client fires 100 requests at 12:00:59 and another 100 at 12:01:01, two consecutive seconds absorb double the intended capacity, overwhelming connection pools on backend workers.

Separate GET + SET calls across stateless serverless Next.js routes also suffer from race conditions under concurrency, allowing parallel requests to read identical remaining counts and bypass quota limits. Our Cloud & DevOps Engineers solve both issues by computing token replenishment inside one atomic Redis Lua script.

Atomic Redis Lua Token Bucket Implementation

Redis executes Lua scripts single-threadedly, meaning refill math, remaining token subtraction, and TTL updates execute in one atomic round-trip with predictable memory usage.

-- Atomic Token Bucket: KEYS[1] = key, ARGV = [capacity, refillPerSec, nowSec, requested]
local data = redis.call("HMGET", KEYS[1], "tokens", "ts")
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])

local tokens = tonumber(data[1]) or capacity local last = tonumber(data[2]) or now tokens = math.min(capacity, tokens + math.max(0, now - last) * rate)

local allowed = 0 if tokens >= cost then tokens = tokens - cost allowed = 1 end

redis.call("HMSET", KEYS[1], "tokens", tokens, "ts", now) redis.call("EXPIRE", KEYS[1], math.ceil(capacity / rate) * 2) return { allowed, math.floor(tokens) } `

Standard RateLimit Headers & Failure Modes

Return accurate Retry-After and RateLimit-Remaining headers aligned with Redis Scripting Guidelines and RFC 6585 Status 429.

  • •Partition buckets by API key, tenant ID, and route risk class instead of global IP alone (corporate NAT gateways share IPs).
  • •Configure public read endpoints to fail open during a brief Redis blip while authentication endpoints fail closed or drop back to local memory LRU guards.
  • •Exclude static assets and health probes so autoscalers remain accurate during mitigated traffic spikes.

Need hardened API gateway protection before scaling enterprise partners? Review our security and infrastructure capabilities.

Senior Engineering Practice

Need architectural guidance on Security?

We partner with founders and engineering leaders to design, build, and optimize scalable web platforms, custom e-commerce engines, and Salesforce CRM ecosystems with 100% intellectual property ownership.

A

Written by Anjali

Senior Technical Architect & SEO Systems Engineer

Anjali is a Senior Technical Architect & SEO Systems Engineer focusing on technical SEO architecture, WCAG accessibility, Shopify OS 2.0 storefronts, and web security hardening.

Contextual Field Notes

Related Security & Architecture Notes

View all engineering notes
Questions & Details

Key Takeaways & FAQs

Protecting multitenant APIs from burst scraping and cascading origin overload using atomic Redis Lua token bucket scripts and clean RFC-compliant retry headers.

Direct Engineering Engagement

Have a project in mind? Send us the details and we’ll reply within a day.

We engineer scalable cloud architecture, custom web platforms, and Salesforce CRM systems for growing companies and global enterprises.

1-Day Reply SLADirect senior architect review
Strict Mutual NDA100% IP confidentiality
Full Code OwnershipNo vendor lock-in
Fixed Sprint BillingPredictable milestones