𝗕𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗮 𝗦𝗹𝗶𝗱𝗶𝗻𝗴 𝗪𝗶𝗻𝗱𝗼𝘄 𝗥𝗮𝘁𝗲 𝗟𝗶𝗺𝗶𝘁𝗲𝗿 𝗶𝗻 𝗥𝗲𝗱𝗶𝘀

Our YouTube API quota hit zero before 9:00 AM twice last quarter. This left our trending feeds stale for half our regions.

The problem was not traffic growth. It was our rate limiter.

We used a fixed-window counter. This allowed two massive bursts of API calls to pass through if they hit right at the window boundary. For a global pipeline with 8 regions, this boundary overlap happened often. It turned our hard daily quota into a budget leak.

I replaced the fixed window with a sliding window log using Redis sorted sets.

Here is how it works:

  • Use ZADD to record a request with a timestamp.
  • Use ZREMRANGEBYSCORE to remove old entries outside the window.
  • Use ZCARD to count exactly how many requests remain in the window.
  • Use PEXPIRE to clean up idle keys automatically.

This method is exact. There is no boundary to straddle.

I implemented this using a Lua script in Redis. This ensures the entire check and record process is atomic. If you run these steps in application code, race conditions will allow extra requests through.

Key technical decisions for production:

  • Use Redis TIME: Do not use timestamps from your application servers. Clock skew between servers ruins window accuracy.
  • Weighted costs: Not all API calls are equal. One search call might cost 100 units while a video list costs 1. My script handles this by inserting multiple members per call.
  • Precise Retry-After: By looking at the oldest entry in the sorted set, the system calculates exactly when capacity frees up.
  • Fail-safe logic: I use EVALSHA with an EVAL fallback. If the Redis script cache clears during a restart, the application handles it gracefully.

The trade-off is memory. Each request takes about 100 bytes. For a 10,000 unit daily quota, that is only about 1 MB of memory. For most use cases, the precision is worth the cost.

Since this change, our quota has not been exhausted once. Our jobs stop cleanly instead of hitting 403 errors.

If your rate limiter resets on a round number of the clock, you do not have a limit. You have a loophole.

Source: https://dev.to/ahmet_gedik778845/building-a-sliding-window-rate-limiter-in-redis-for-a-multi-region-video-api-50ni