MODULE 3/LESSON 4
🔴 Redis Architecture

🛡 Rate Limiter Simulator

Test Token Bucket rate limiting — see HTTP 200 vs 429 in real time

Interactive⚡ Interactive Tool
The Token Bucket algorithm allows controlled traffic bursts while enforcing an average rate limit. Tokens refill at a constant rate. Requests consume tokens. When empty, requests are dropped with HTTP 429. Use the simulator below to experience how a Token Bucket limiter handles bursty traffic compared to steady traffic.

Key Concepts

Bucket Capacity

The maximum number of requests (tokens) that can be burst at once. If capacity is 10, a user can make 10 requests instantaneously before hitting the limit.

Refill Rate

How fast tokens are added back to the bucket (e.g., 2 tokens per second). This defines the sustained long-term average request rate.

HTTP 200 (Success)

When a request arrives, the API Gateway checks Redis. If a token exists, it decrements the token count by 1 and forwards the request to the backend.

HTTP 429 (Too Many Requests)

If the bucket is empty (0 tokens), the API Gateway immediately drops the request, returning a 429 status code without touching the backend.

⚡ Interactive Architecture Simulator

Interactive Rate Limiter Simulator
Token Reservoir
10
/ 10 max capacity
HTTP 200 Allowed0
HTTP 429 Blocked0
Block Rate0%
Current Algorithmtoken bucket
Live Traffic Stream & Rate Limit Enforcement (Req / sec)
HTTP 200 (Allowed): 0 r/s
HTTP 429 (Blocked): 0 r/s
Refill Rate Capacity: 2 r/s
0 r/s4 r/s8 r/s12 r/s
No requests yet. Send manual requests or enable Auto Traffic above!

The Math Behind Token Bucket

token-bucket.jsjavascript
1// Pseudocode for Token Bucket check in Redis
2function allowRequest(userId, maxCapacity, refillRatePerSec) {
3  const key = `rate_limit:${userId}`;
4  const now = Math.floor(Date.now() / 1000);
5  
6  // 1. Get current state from Redis
7  let { tokens, lastRefillTime } = redis.get(key) || { tokens: maxCapacity, lastRefillTime: now };
8  
9  // 2. Calculate how many tokens to add based on elapsed time
10  const timePassed = now - lastRefillTime;
11  const tokensToAdd = timePassed * refillRatePerSec;
12  
13  // 3. Refill bucket, but don't exceed max capacity
14  tokens = Math.min(maxCapacity, tokens + tokensToAdd);
15  
16  if (tokens >= 1) {
17    // 4a. Allow request and consume 1 token
18    redis.set(key, { tokens: tokens - 1, lastRefillTime: now });
19    return true; // HTTP 200
20  } else {
21    // 4b. Bucket empty
22    redis.set(key, { tokens, lastRefillTime: now }); // update time anyway
23    return false; // HTTP 429
24  }
25}
💡
Senior Architect Insight: Why is Token Bucket better than Fixed Window? In a Fixed Window (e.g. max 100 reqs/min), a user could send 100 requests at 1:59:59 and another 100 at 2:00:01, effectively hitting your server with 200 requests in 2 seconds. Token Bucket prevents this edge-case burst by enforcing a smooth refill rate.