QA
Intermediate LevelSoftware Engineering Principles

Compare Rate Limiting Algorithms: Token Bucket, Leaky Bucket, Fixed Window, and Sliding Window Log.

❓ Interview Prompt: Compare Rate Limiting Algorithms: Token Bucket (burst support), Leaky Bucket (smooth queue output), Fixed Window Counter, and Sliding Window Log.

Evaluating Rate Limiting Algorithms:

•
Token Bucket: Tokens added at fixed rate. Requests consume tokens. Supports burst traffic up to bucket capacity. Best for general APIs.
•
Leaky Bucket: Requests enter queue and leak out at a smooth, constant rate. Eliminates traffic bursts. Best for smoothing background processing queues.
•
Fixed Window Counter: Counts requests per fixed time window (e.g. 12:00-12:01). Vulnerable to 2x burst traffic at window boundaries!
•
Sliding Window Log: Stores timestamps in Redis ZSET. Precise sliding window calculation; higher memory usage.

Code Implementation Example

typescript
// Redis Token Bucket Algorithm concept
async function consumeToken(userId: string): Promise<boolean> {
  const tokens = await redis.get('tokens:\${userId}');
  if (parseInt(tokens) > 0) {
    await redis.decr('tokens:\${userId}');
    return true; // Allowed
  }
  return false; // Rate limited!
}
💡Executing Token Bucket rate limiting in Redis.
🎯 Interview Key Takeaway

Use Token Bucket for APIs that allow burst traffic, and Leaky Bucket when smooth processing queues are required.

#Rate Limiting#Token Bucket#Algorithms#Intermediate
Back to all Software Engineering Principles Questions→

More Software Engineering Principles Interview Questions

Beginner LevelSoftware Engineering Principles

Explain the 5 SOLID Principles of Object-Oriented Software Design with practical refactoring examples.

Beginner LevelSoftware Engineering Principles

What are DRY (Don't Repeat Yourself), KISS (Keep It Simple, Stupid), and YAGNI (You Aren't Gonna Need It)?

Beginner LevelSoftware Engineering Principles

Why is Composition Over Inheritance a fundamental object-oriented design principle?