# Design a Distributed Rate Limiter

> Design a rate limiting service shared by many API servers, covering algorithm choice, Redis-backed counters, race conditions, rules and multi-region behaviour....

Canonical: https://www.internhack.in/roadmaps/tracks/system-design/case-distributed-rate-limiter
Last updated: 2026-11-28

Design a rate limiting service shared by many API servers, covering algorithm choice, Redis-backed counters, race conditions, rules and multi-region behaviour.

Intermediate · about 4 hours · difficulty 3/5

Learn first: [Rate Limiting Algorithms](https://www.internhack.in/roadmaps/tracks/system-design/rate-limiting), [Redis in Practice](https://www.internhack.in/roadmaps/tracks/system-design/redis-in-practice)

Knowing the algorithms is step one. The case study asks you to make them work across a fleet of servers handling lakhs of requests per second.

### Requirements

- Limit requests per user, API key or IP, with different rules per endpoint (OTP: 5 per hour per phone; search: 100 per minute per user).
- Very low added latency (a millisecond or two), highly available, accurate enough.
- Clear 429 responses with Retry-After.

### Where to place it

- In the **API gateway** or as middleware in each service, calling a shared counter store.
- Or as a **sidecar** or library with local counters synced periodically (faster, less accurate).

### Shared state in Redis

Each server checks and updates a counter in Redis keyed by rule and client (rl:otp:+9198xxxx). Use:

- **Fixed or sliding window counters** with INCR and EXPIRE.
- **Token bucket** storing tokens and last refill time per key.

The read-modify-write must be **atomic**: wrap it in a **Lua script** (or use MULTI/EXEC) so two servers cannot both take the last token.

### Scaling and failure

- Shard keys across a Redis Cluster; hot keys (a huge customer) may need local pre-aggregation.
- **Fail open or fail closed?** If Redis is down, most APIs fail open (allow traffic) to stay available; sensitive endpoints like login or OTP may fail closed.
- **Multi-region**: per-region limits are simple and fast; global limits need syncing counters across regions (eventually consistent, slightly over-permissive).

### Rules management

Store rules in config or a rules service, cached in each limiter, so you can change limits without deploys. Log and meter throttled requests to tune them.

### Pitfalls

- Non-atomic GET then SET races letting bursts through.
- A synchronous network hop to a far-away Redis adding 20 ms to every request.

You are ready to move on when you can draw the architecture, write the Lua logic for a token bucket in pseudocode, and explain your fail-open policy per endpoint.

## Key concepts

- **Rules per endpoint and client**
- **Gateway vs middleware placement**
- **Redis counters**
- **Atomicity with Lua scripts**
- **Fail open vs fail closed**
- **Multi-region limits**
- **Local pre-aggregation for hot keys**

## Mini project

Build a rate limiter middleware backed by Redis with a token bucket implemented as a Lua script, configurable rules from a YAML file, and 429 responses with headers. Run two API instances and a load test that proves the limit holds across both.

## Check yourself

Two API servers check the same user's token count at the same moment and both see 1 token left. What goes wrong and how does a Lua script fix it?

## Free resources

- [Scaling your API with rate limiters](https://stripe.com/blog/rate-limiters) (Stripe)
- [Redis documentation](https://redis.io/docs/latest/) (Redis)
- [ByteByteGo System Design 101](https://github.com/ByteByteGoHq/system-design-101) (ByteByteGo)

Previous: [Design a URL Shortener (TinyURL, bit.ly)](https://www.internhack.in/roadmaps/tracks/system-design/case-url-shortener) · Next: [Design a Notification System](https://www.internhack.in/roadmaps/tracks/system-design/case-notification-system)

## More in HLD Case Studies: Core Building Blocks

- [Design a URL Shortener (TinyURL, bit.ly)](https://www.internhack.in/roadmaps/tracks/system-design/case-url-shortener)
- [Design a Distributed Rate Limiter](https://www.internhack.in/roadmaps/tracks/system-design/case-distributed-rate-limiter)
- [Design a Notification System](https://www.internhack.in/roadmaps/tracks/system-design/case-notification-system)
- [Design a Distributed Cache](https://www.internhack.in/roadmaps/tracks/system-design/case-distributed-cache)
- [Design a Web Crawler](https://www.internhack.in/roadmaps/tracks/system-design/case-web-crawler)
- [Design Search Autocomplete (Typeahead)](https://www.internhack.in/roadmaps/tracks/system-design/case-search-autocomplete)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [System Design Roadmap](https://www.internhack.in/roadmaps/tracks/system-design)
