0Pricing
Lua Academy · Lesson

Rate Limiting with Lua

Implement sliding window rate limiters atomically in Redis Lua.

Why Redis for Rate Limiting?

Redis provides atomic operations, in-memory speed, and shared state across multiple application servers — making it ideal for distributed rate limiting.

Simple Fixed Window Counter

Count requests per time window using INCR and EXPIRE.

EVAL "
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local count = redis.call('INCR', key)
if count == 1 then
  redis.call('EXPIRE', key, window)
end
if count > limit then
  return 0  -- rejected
end
return 1  -- allowed
" 1 rate:user:123 100 60

All lessons in this course

  1. EVAL and Redis Lua Environment
  2. Atomic Operations and Transactions
  3. Rate Limiting with Lua
  4. SCRIPT LOAD and EVALSHA
← Back to Lua Academy