Fixed Window algorithm: Redis key format and behavior
Technical reference for the Fixed Window rate-limiting algorithm in RLaaS. Covers Redis key format, Lua script behavior, config, and trade-offs.
The Fixed Window algorithm divides time into discrete buckets based on floor(epochSeconds / windowSize). All requests in the same bucket share a single Redis STRING key. This is the simpler of the two algorithms, offering low memory use at the cost of potential burst behavior at window boundaries.
The full fixedWindowSize.lua script executed atomically in Redis:
local windowSize = tonumber(ARGV[1]) -- e.g. 60 (seconds)local limit = tonumber(ARGV[2]) -- e.g. 5 (max requests)-- Get current server time from Redis (avoids client clock skew)local now = redis.call('TIME')local currentSeconds = tonumber(now[1])-- Compute which window bucket this request falls intolocal window = math.floor(currentSeconds / windowSize)-- Build the full Redis key including the window bucketlocal key = KEYS[1] .. ':' .. window-- Atomically increment the counter for this windowlocal current = redis.call('INCR', key)-- Set TTL only on the first request so the key expires at window endif current == 1 then redis.call('EXPIRE', key, windowSize)end-- Read remaining TTL for the responselocal ttl = redis.call('TTL', key)-- Return {allowed, currentCount, ttl}if current > limit then return {0, current, ttl} -- deniedelse return {1, current, ttl} -- allowedend
Because each window is an independent counter, a user can send the maximum number of requests at the very end of one window and again at the very start of the next — and all of them will be allowed.Example with max-requests: 5 and window-size: 60:
Time (s)
Window
Requests
Outcome
59
N
5
All 5 allowed (window N count = 5)
61
N+1
5
All 5 allowed (window N+1 count = 5)
Within a two-second span, 10 requests pass — double the configured limit. This is an inherent property of fixed-window counting.
Use sliding-window-algo if you need to prevent boundary bursts. The sliding window tracks every request individually and enforces the limit over a true rolling time range.