Rate Limiter Service
A shared limiter.
Earlier, we built a token bucket in one process. But Shoply has twenty API servers. If each keeps its own count, a user could get twenty times their limit. The limiter needs a shared memory.

- Twenty API servers
- Separate counts don't work
- Share the state
A shared limiter
The usual design puts the counters in Redis. When a request arrives, the gateway runs a small atomic script in Redis, that refills and takes a token in one step. If a token was available, the request goes on. If not, the gateway answers four two nine straight away.
- Request
- Gateway
- Atomic Redis script
- Token? Continue
- No token: 429

Design details
A few details matter. The update must be atomic, or two servers could take the last token at the same time. Keys should expire, so idle users don't use memory. If Redis is unreachable, decide whether to fail open, allowing traffic, or fail closed. And put limits in config, so they change without deploys.
| key | value |
|---|---|
| Atomic | refill + take in one step |
| Expiring keys | idle users cost nothing |
| Redis down | fail open or closed? |
| Config | change limits live |
Summary
A shared rate limiter keeps counters in Redis, updated atomically, so every server sees the same limit. Next, a bigger case study. A chat app.
Key takeaways- Counters in Redis
- Atomic updates
- Choose fail open or closed