QA
Intermediate LevelREST APIs & Web Services

Compare Token Bucket vs Leaky Bucket vs Fixed Window Rate Limiting algorithms.

❓ Interview Prompt: Compare Rate Limiting algorithms: Fixed Window Counter (burst boundary problem), Sliding Window Log, Token Bucket (burst allowance), and Leaky Bucket (smooth constant rate).

Selecting rate limiting algorithms for API Gateways:

1
Fixed Window Counter: Counts requests per fixed minute window (e.g. 12:00-12:01). Flaw: A client can send 100 requests at 12:00:59 and 100 at 12:01:01, causing 200 requests in 2 seconds!
2
Sliding Window Log: Tracks request timestamps in Redis ZSET. Extremely accurate but high memory consumption.
3
Token Bucket (Standard): Tokens added to bucket at steady rate. Requests consume tokens. Allows initial burst traffic up to bucket capacity.
4
Leaky Bucket: Requests fill queue; processed at constant output rate. Smooths bursty traffic completely.

Code Implementation Example

typescript
import { redis } from './redis';

// Token Bucket Implementation in Redis Lua Script
const tokenBucketLua = '
  local key = KEYS[1]
  local limit = tonumber(ARGV[1])
  local refillRate = tonumber(ARGV[2])
  local now = tonumber(ARGV[3])
  -- Calculate refilled tokens and evaluate quota...
';
💡Redis Lua script token bucket rate limiting architecture.
🎯 Interview Key Takeaway

Token Bucket allows traffic bursts up to capacity; Leaky Bucket smooths requests to a constant output processing rate.

#Rate Limiting#Token Bucket#Redis#Intermediate
Back to all REST APIs & Web Services Questions→

More REST APIs & Web Services Interview Questions

Beginner LevelREST APIs & Web Services

What is HTTP Method Idempotency and how do you implement Idempotency Keys for REST APIs?

Beginner LevelREST APIs & Web Services

What are standard HTTP Status Codes (200, 201, 204, 400, 401, 403, 404, 409, 429, 500, 503)?

Beginner LevelREST APIs & Web Services

What are HTTP Verbs (GET, POST, PUT, PATCH, DELETE) and when to use PUT vs PATCH?