ArchitectAI resilience reviews are here. See what's new
Back to all articles
High Level Design Oct 02, 2026 13 min read

Designing a Distributed Rate Limiter: Token Bucket, Sliding Window & Redis

Protecting microservices from DDoS, scraper bots, and cascading overload. Detailed comparison of Token Bucket vs Sliding Window Counter with atomic Redis Lua scripts.

HLDRate LimiterRedisLuaSecurity
HFHLDForge Architecture TeamStaff Infrastructure Architect

1. Why Rate Limiting is Critical in Modern Systems

Rate limiters protect backend services from abusive traffic, credential stuffing, scraping bots, and unintentional retry storms. When limits are exceeded, the system returns HTTP 429 Too Many Requests with informative headers (`Retry-After`, `X-RateLimit-Remaining`).

2. Algorithm Comparison: Token Bucket vs Sliding Window

Token Bucket: Tokens are added at a constant rate $r$ up to capacity $b$. Allows bursts up to capacity, memory efficient ($O(1)$ state per user), but requires accurate timestamp arithmetic.

Sliding Window Log: Keeps timestamps of all requests in a sorted set. Precise down to the millisecond, but consumes high memory under bursty traffic.

Sliding Window Counter: Combines fixed window counters from the current and previous windows using a weighted formula. Offers 99% accuracy with tiny $O(1)$ memory usage.

3. Distributed Architecture with Redis & Atomic Lua Scripts

In a distributed cluster of application servers, local in-memory counters fail because requests from the same user hit different instances. We centralize rate limit state in Redis.

To eliminate race conditions between reading and writing counter values, the entire token refill and decrement operation is executed inside an atomic Redis Lua script, avoiding expensive network round-trips.

4. Deployment Topology: Edge Gateway vs Microservice Middleware

For maximum efficiency, rate limiting is placed at the edge (Envoy / Kong API Gateway or Cloudflare Workers) before requests reach backend compute instances. Multi-region deployments synchronize rate buckets asynchronously using CRDTs or regional Redis replicas.

Interactive Modeling ArchitectAI

Model this architecture on the HLDForge Canvas

Turn static box diagrams into living, collaborative systems. Drag and drop microservices, Kafka clusters, and cache tiers. Run automated SPOF detection and STRIDE threat modeling in real time.

HF

Written by HLDForge Architecture Team

Staff Infrastructure Architect

The HLDForge Architecture Team publishes practical engineering guides, real-world case studies, and high-concurrency blueprints to help engineers build scalable and fault-tolerant software.

Free forever plan

Ready to master distributed system design?

HLDForge gives you the tools, templates, and AI reviews to design systems like a Staff Engineer. Get started free.

SOC 2 Type II · ISO 27001 aligned · SSO on every paid plan