Lesson 8 of 15
32. Design a Rate Limiter as a Service

32.7 How do counters work in Redis?

3 min read+10 XP

One script per check. A Lua script runs atomically on the Redis server: no other command runs in the middle. The token-bucket script:

lua

Details that matter:

  • One round trip per check (32.2), and the script is loaded once and called by hash (EVALSHA).
  • TTL on every key, so 10 million idle keys don't live forever.
  • Shard by key across a Redis Cluster; one key's state lives on one shard.
  • Replicas for failover. Losing a shard briefly resets some buckets to full, which over-allows for a moment; acceptable for API limits, and one reason security rules keep a strict local fallback.
  • Hot keys: one giant tenant hammering one key concentrates load on one shard. Give very large tenants local token batches: the client takes, say, 50 tokens at once and spends them locally, calling Redis 50× less often, at the cost of slight inaccuracy.

Memory code: "One script, Redis's clock, a TTL on every key."