Course contentsShow
← System Design
Lesson 961 of 1,91925. Rate Limiting and ThrottlingFree lesson

Time Windows for Rate Limits

Understand fixed windows, sliding windows, and how time granularity affects fairness and burst handling.

Time Windows for Rate Limits

What You'll Learn

You'll learn how different time-window strategies affect how fairly your rate limiter behaves and how well it handles traffic bursts.

The Time Window Problem

Rate limiting always operates within a time window—you allow N requests per time period. But how you define and slide that window dramatically changes behavior.

Fixed Windows

A fixed window resets at regular intervals (e.g., every minute at :00 seconds). If you allow 100 requests per minute:

  • User makes 100 requests at 10:00:59
  • Window resets at 10:01:00
  • User immediately makes another 100 requests at 10:01:01

Result: 200 requests in 2 seconds! This "boundary burst" problem makes fixed windows easy to implement but less fair.

Sliding Windows

A sliding window looks back from the current moment. At any point in time, you count requests in the last 60 seconds, not from the last fixed boundary.

Analogy: Fixed windows are like a librarian who resets your checkout limit at midnight. Sliding windows are like a librarian who asks, "How many books did you take in the last 24 hours?" whenever you arrive.

Sliding windows are fairer but require tracking individual request timestamps, increasing memory usage.

Time Granularity

Finer granularity (seconds vs minutes) gives smoother rate limiting but requires more frequent checks and counters. Coarser granularity is simpler but allows larger bursts within the window.

Many systems use sliding window with buckets: subdivide the window into smaller fixed buckets and weight them based on how much overlap they have with the current time.

Key Takeaway: Fixed windows are simple but allow boundary bursts; sliding windows provide fairer distribution at the cost of complexity, and time granularity determines how smooth your rate limiting feels to legitimate users.