

- Alan sends 100 requests in the first 2 seconds of the minute. The minute counter hits 100 and Alan is blocked for the remaining ~58 seconds, then can immediately send 100 more requests at the next minute boundary.
- John sends 100 requests in the last second of a minute. Once the minute flips, his counter resets and he can immediately send another 100 requests. Over a two-second window he effectively sent 200 requests.

- Each user has a bucket with capacity = 100 tokens.
- Each request consumes 1 token.
- Tokens refill over time at a steady rate: 100 tokens per minute ≈ 1.6667 tokens/second (for examples you can approximate to 2 tokens/sec).
- If the bucket is empty, requests are rejected. If it’s full, the user can burst up to the capacity.

- Alan drains 100 tokens in 2 seconds. After that he is limited to the refill rate (≈1.67 tokens/sec) until the bucket recovers.
- John drains 100 tokens at the end of the minute and must wait for tokens to refill; he cannot exploit a clock boundary to immediately perform another large burst.

- Reads the stored token count and last refill timestamp,
- Computes tokens to add based on elapsed time and refill rate,
- Checks if enough tokens exist for the current request,
- If allowed, subtracts tokens and updates the timestamp atomically,
- Returns an allow/deny result.
Operational considerations and tips
- Use Redis server time via
redis.call('TIME')in the Lua script to avoid clock skew between application servers. See TIME: https://redis.io/commands/time/. - Running the token-bucket logic in a Redis Lua script ensures the read-modify-write is atomic and avoids race conditions across multiple app servers.
- Refill granularity: you can use fractional tokens (floating-point) and refill continuously using elapsed-time calculation. This provides smoother behavior than integer-only refills.
- Hot keys & scaling: for very high scale or very hot users, use Redis Cluster or sharded key namespaces so a single Redis node doesn’t become a bottleneck. See Redis Cluster: https://redis.io/docs/latest/cluster/.
- Geo-distribution and strict global limits: if you need strict global enforcement across geographic regions, expect additional latency. Consider hybrid designs: local leaky buckets for immediate throttling plus periodic global reconciliation for exact global quotas.
- Tune two parameters: bucket capacity (allowed burst) and refill rate (sustained throughput). These define how bursty or smooth the user experience is.
Atomic updates and low-latency lookups are critical. If you implement a token-bucket without atomicity, concurrent requests can overspend tokens and break the rate-limit guarantees. Always benchmark Redis latency and plan for hot-key mitigation and failover.
- Redis: https://redis.io/
- Redis EVAL / Lua scripting: https://redis.io/commands/eval
- Redis TIME command: https://redis.io/commands/time/
- Stripe rate limit design: https://stripe.com/docs/rate-limits
- GitHub rate limiting overview: https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
- Amazon API Gateway throttling: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-throttling.html