{
  "schema_version": 2,
  "id": "develop/use-cases/rate-limiter/ruby",
  "title": "Token bucket rate limiter with Redis and Ruby",
  "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/ruby/",
  "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in Ruby",
  "aliases": [
    "/develop/use-cases/rate-limiter/redis-rb"
  ],
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-09-29T14:34:47+01:00",
  "children": [],
  "page_type": "content",
  "content_hash": "de6687b402fd51002d284911af9564063efe37ec3ea323f162210bea96010a69",
  "sections": [
    {
      "id": "overview-1",
      "title": "Overview",
      "role": "overview",
      "text": "This guide shows you how to implement a distributed token bucket rate limiter using Redis and Lua scripts in Ruby with the [`redis-rb`](https://redis.io/docs/latest/develop/clients/ruby) client library."
    },
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Rate limiting is a critical technique for controlling the rate at which operations are performed. Common use cases include:\n\n* Limiting API requests per user or IP address\n* Preventing abuse and protecting against denial-of-service attacks\n* Ensuring fair resource allocation across multiple clients\n* Throttling background jobs or batch operations\n\nThe **token bucket algorithm** is a popular rate limiting approach that allows bursts of traffic while maintaining an average rate limit over time. This guide covers the Ruby implementation using the [`redis-rb`](https://redis.io/docs/latest/develop/clients/ruby) gem."
    },
    {
      "id": "how-it-works",
      "title": "How it works",
      "role": "content",
      "text": "The token bucket algorithm works like a bucket that holds tokens:\n\n1. **Initialization**: The bucket starts with a maximum capacity of tokens\n2. **Refill**: Tokens are added to the bucket at a constant rate (for example, 1 token per second)\n3. **Consumption**: Each request consumes one token from the bucket\n4. **Decision**: If tokens are available, the request is allowed; otherwise, it's denied\n5. **Capacity limit**: The bucket never exceeds its maximum capacity\n\nThis approach allows for burst traffic (using accumulated tokens) while enforcing an average rate limit over time."
    },
    {
      "id": "why-use-redis",
      "title": "Why use Redis?",
      "role": "content",
      "text": "Redis is ideal for distributed rate limiting because:\n\n* **Atomic operations**: Lua scripts execute atomically, preventing race conditions\n* **Shared state**: Multiple application servers can share the same rate limit counters\n* **High performance**: In-memory operations provide microsecond latency\n* **Automatic expiration**: Keys can be set to expire automatically (though not used in this implementation)"
    },
    {
      "id": "the-lua-script",
      "title": "The Lua script",
      "role": "content",
      "text": "The core of this implementation is a Lua script that runs atomically on the Redis server. This ensures that checking and updating the token bucket happens in a single operation, preventing race conditions in distributed environments.\n\nHere's how the script works:\n\n[code example]"
    },
    {
      "id": "script-breakdown",
      "title": "Script breakdown",
      "role": "content",
      "text": "1. **State retrieval**: Uses [`HMGET`](https://redis.io/docs/latest/commands/hmget) to fetch the current token count and last refill time from a hash\n2. **Initialization**: On first use, sets tokens to full capacity\n3. **Token refill calculation**: Computes how many tokens should be added based on elapsed time\n4. **Capacity enforcement**: Uses `math.min()` to ensure tokens never exceed capacity\n5. **Token consumption**: Decrements the token count if available\n6. **State update**: Uses [`HMSET`](https://redis.io/docs/latest/commands/hmset) to save the new state\n7. **Return value**: Returns both the decision (allowed/denied) and remaining tokens"
    },
    {
      "id": "why-atomicity-matters",
      "title": "Why atomicity matters",
      "role": "content",
      "text": "Without atomic execution, race conditions could occur:\n\n* **Double spending**: Two requests could read the same token count and both succeed when only one should\n* **Lost updates**: Concurrent updates could overwrite each other's changes\n* **Inconsistent state**: Token count and refill time could become desynchronized\n\nUsing [`EVAL`](https://redis.io/docs/latest/commands/eval) or [`EVALSHA`](https://redis.io/docs/latest/commands/evalsha) ensures the entire operation executes atomically, making it safe for distributed systems."
    },
    {
      "id": "installation",
      "title": "Installation",
      "role": "setup",
      "text": "Install the `redis` gem:\n\n[code example]\n\nOr add it to your `Gemfile`:\n\n[code example]\n\nThen run:\n\n[code example]"
    },
    {
      "id": "using-the-ruby-module",
      "title": "Using the Ruby module",
      "role": "content",
      "text": "The `TokenBucket` class provides a simple interface for rate limiting\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/rate-limiter/ruby/token_bucket.rb)):\n\n[code example]\n\nRuby's keyword arguments make the constructor parameters self-documenting, and the `allow` method returns a Hash with `:allowed` and `:remaining` keys."
    },
    {
      "id": "configuration-parameters",
      "title": "Configuration parameters",
      "role": "configuration",
      "text": "* **capacity**: Maximum number of tokens in the bucket (controls burst size)\n* **refill_rate**: Number of tokens added per refill interval\n* **refill_interval**: Time in seconds between refills\n\nFor example:\n* `capacity: 10, refill_rate: 1, refill_interval: 1.0` allows 1 request per second with bursts up to 10\n* `capacity: 100, refill_rate: 10, refill_interval: 1.0` allows 10 requests per second with bursts up to 100\n* `capacity: 60, refill_rate: 1, refill_interval: 60.0` allows 1 request per minute with bursts up to 60"
    },
    {
      "id": "rate-limit-keys",
      "title": "Rate limit keys",
      "role": "content",
      "text": "The `key` parameter identifies what you're rate limiting. Common patterns:\n\n* **Per user**: `user:{user_id}` - Limit each user independently\n* **Per IP address**: `ip:{ip_address}` - Limit by client IP\n* **Per API endpoint**: `api:{endpoint}:{user_id}` - Different limits per endpoint\n* **Global**: `global:api` - Single limit shared across all requests"
    },
    {
      "id": "script-caching-with-evalsha",
      "title": "Script caching with EVALSHA",
      "role": "content",
      "text": "The Ruby implementation uses [`EVALSHA`](https://redis.io/docs/latest/commands/evalsha) for optimal performance. On first use, the Lua script is loaded into Redis with `SCRIPT LOAD`, and subsequent calls use the cached SHA1 hash. If the script is evicted from the cache, the module automatically falls back to [`EVAL`](https://redis.io/docs/latest/commands/eval) and reloads the script.\n\n[code example]"
    },
    {
      "id": "running-the-demo",
      "title": "Running the demo",
      "role": "content",
      "text": ""
    },
    {
      "id": "get-the-source-files",
      "title": "Get the source files",
      "role": "content",
      "text": "The demo consists of two Ruby files. Download them from the [`ruby` source folder](https://github.com/redis/docs/tree/main/content/develop/use-cases/rate-limiter/ruby) on GitHub, or grab them with `curl`:\n\n[code example]"
    },
    {
      "id": "start-the-demo-server",
      "title": "Start the demo server",
      "role": "content",
      "text": "A demonstration web server is included to show the rate limiter in action\n([source](https://github.com/redis/docs/blob/main/content/develop/use-cases/rate-limiter/ruby/demo_server.rb)):\n\n[code example]\n\nThe demo provides an interactive web interface where you can:\n\n* Submit requests and see them allowed or denied in real-time\n* View the current token count\n* Adjust rate limit parameters dynamically\n* Test different rate limiting scenarios\n\nThe demo assumes Redis is running on `localhost:6379` but you can specify a different host and port using the `--redis-host HOST` and `--redis-port PORT` command-line arguments. Visit `http://localhost:8080` in your browser to try it out."
    },
    {
      "id": "response-headers",
      "title": "Response headers",
      "role": "returns",
      "text": "It's common to include rate limit information in HTTP response headers:\n\n[code example]"
    },
    {
      "id": "customization",
      "title": "Customization",
      "role": "content",
      "text": ""
    },
    {
      "id": "using-with-rack-middleware",
      "title": "Using with Rack middleware",
      "role": "content",
      "text": "You can wrap the rate limiter as Rack middleware for easy integration with any Rack-based framework (Rails, Sinatra, Hanami):\n\n[code example]"
    },
    {
      "id": "error-handling",
      "title": "Error handling",
      "role": "errors",
      "text": "The `allow` method may raise an error if the Redis connection is lost. Wrap calls in a `begin/rescue` block for production use:\n\n[code example]"
    },
    {
      "id": "alternative-rate-limiting-algorithms",
      "title": "Alternative rate limiting algorithms",
      "role": "content",
      "text": "The token bucket algorithm above handles most use cases but Redis supports\nother rate limiter patterns that might fit your requirements better. The table\nbelow lists four other algorithms alongside token bucket and summarizes\ntheir features:\n\n| Algorithm | Memory | Accuracy | Burst behavior | Best for |\n|---|---|---|---|---|\n| [Token bucket](#how-it-works) | 1 key (hash) | Exact | Controlled bursts | APIs with bursty traffic |\n| [Fixed window counter](#fixed-window-counter) | 1 key (string) | Approximate | 2x burst at boundaries | Simple API limits |\n| [Sliding window log](#sliding-window-log) | O(n) entries | Exact | No bursts | High-value APIs, audit trails |\n| [Sliding window counter](#sliding-window-counter) | 2 keys (string) | Near-exact | Smoothed boundaries | General-purpose APIs |\n| [Leaky bucket (policing)](#leaky-bucket-policing) | 1 key (hash) | Exact | No bursts | Strict no-burst enforcement |\n\nThe sections below give example implementations of these other algorithms.\nThe three time-based algorithms call `redis.call('TIME')` inside the Lua\nscript to derive the current timestamp from the Redis server clock. This\neliminates clock drift when the limiter runs across multiple application\nservers. The fixed window counter reads no clock: the key's TTL defines\nthe window."
    },
    {
      "id": "fixed-window-counter",
      "title": "Fixed window counter",
      "role": "content",
      "text": "Counts requests within discrete, non-overlapping time intervals.\nSimplest algorithm — one key per window, one `EVAL` round trip.\n\n[code example]\n\n**Trade-off**: A client can make 2x requests by sending `limit` requests\nat the end of one window and `limit` requests at the start of the next."
    },
    {
      "id": "sliding-window-log",
      "title": "Sliding window log",
      "role": "content",
      "text": "Records the exact timestamp of every request in a sorted set.\nProvides a true rolling window with no boundary bursts.\n\n[code example]\n\n**Trade-off**: Memory grows O(n) with request volume. Not ideal for\nhigh-volume, high-cardinality rate limiting."
    },
    {
      "id": "sliding-window-counter",
      "title": "Sliding window counter",
      "role": "content",
      "text": "Blends two fixed-window counters using a weighted average to approximate\na true sliding window. Near-exact accuracy with the same low memory\nfootprint as a fixed window. The two keys use hash tags so they map\nto the same slot in Redis Cluster.\n\n[code example]\n\n**Trade-off**: The weighted estimate may let slightly more or fewer\nrequests through than the exact limit. Negligible for most apps."
    },
    {
      "id": "leaky-bucket-policing",
      "title": "Leaky bucket (policing)",
      "role": "content",
      "text": "A virtual bucket fills with incoming requests and drains at a fixed rate.\nIf the bucket is full, requests are rejected immediately. This is the\npolicing variant — requests are allowed or denied instantly with no delay.\n\n[code example]\n\n**Trade-off**: Overflow traffic is rejected immediately. Clients must\nhandle `429 Too Many Requests` and retry with backoff."
    },
    {
      "id": "learn-more",
      "title": "Learn more",
      "role": "related",
      "text": "* [EVAL command](https://redis.io/docs/latest/commands/eval) - Execute Lua scripts\n* [EVALSHA command](https://redis.io/docs/latest/commands/evalsha) - Execute cached Lua scripts\n* [Lua scripting](https://redis.io/docs/latest/develop/programmability/eval-intro) - Introduction to Redis Lua scripting\n* [HMGET command](https://redis.io/docs/latest/commands/hmget) - Get multiple hash fields\n* [HMSET command](https://redis.io/docs/latest/commands/hmset) - Set multiple hash fields\n* [Ruby client](https://redis.io/docs/latest/develop/clients/ruby) - Redis Ruby client documentation"
    }
  ],
  "examples": [
    {
      "id": "the-lua-script-ex0",
      "language": "lua",
      "code": "local key = KEYS[1]\nlocal capacity = tonumber(ARGV[1])\nlocal refill_rate = tonumber(ARGV[2])\nlocal refill_interval = tonumber(ARGV[3])\nlocal now = tonumber(ARGV[4])\n\n-- Get current state or initialize\nlocal bucket = redis.call('HMGET', key, 'tokens', 'last_refill')\nlocal tokens = tonumber(bucket[1])\nlocal last_refill = tonumber(bucket[2])\n\n-- Initialize if this is the first request\nif tokens == nil then\n    tokens = capacity\n    last_refill = now\nend\n\n-- Calculate token refill\nlocal time_passed = now - last_refill\nlocal refills = math.floor(time_passed / refill_interval)\n\nif refills > 0 then\n    tokens = math.min(capacity, tokens + (refills * refill_rate))\n    last_refill = last_refill + (refills * refill_interval)\nend\n\n-- Try to consume a token\nlocal allowed = 0\nif tokens >= 1 then\n    tokens = tokens - 1\n    allowed = 1\nend\n\n-- Update state\nredis.call('HMSET', key, 'tokens', tokens, 'last_refill', last_refill)\n\n-- Return result: allowed (1 or 0) and remaining tokens\nreturn {allowed, tokens}",
      "section_id": "the-lua-script"
    },
    {
      "id": "installation-ex0",
      "language": "bash",
      "code": "gem install redis",
      "section_id": "installation"
    },
    {
      "id": "installation-ex1",
      "language": "ruby",
      "code": "gem 'redis', '~> 5.0'",
      "section_id": "installation"
    },
    {
      "id": "installation-ex2",
      "language": "bash",
      "code": "bundle install",
      "section_id": "installation"
    },
    {
      "id": "using-the-ruby-module-ex0",
      "language": "ruby",
      "code": "require 'redis'\nrequire_relative 'token_bucket'\n\n# Create a Redis connection\nredis = Redis.new(host: 'localhost', port: 6379)\n\n# Create a rate limiter: 10 requests per second\nlimiter = TokenBucket.new(\n  redis: redis,\n  capacity: 10,          # Maximum burst size\n  refill_rate: 1,        # Add 1 token per interval\n  refill_interval: 1.0   # Every 1 second\n)\n\n# Check if a request should be allowed\nresult = limiter.allow('user:123')\n\nif result[:allowed]\n  puts \"Request allowed. #{result[:remaining]} tokens remaining.\"\n  # Process the request\nelse\n  puts 'Request denied. Rate limit exceeded.'\n  # Return 429 Too Many Requests\nend",
      "section_id": "using-the-ruby-module"
    },
    {
      "id": "script-caching-with-evalsha-ex0",
      "language": "ruby",
      "code": "# The module handles script caching automatically.\n# First call loads the script, subsequent calls use EVALSHA.\nresult1 = limiter.allow('user:123') # Uses EVAL + caches\nresult2 = limiter.allow('user:123') # Uses EVALSHA (faster)",
      "section_id": "script-caching-with-evalsha"
    },
    {
      "id": "get-the-source-files-ex0",
      "language": "bash",
      "code": "mkdir rate-limiter-demo && cd rate-limiter-demo\nBASE=https://raw.githubusercontent.com/redis/docs/main/content/develop/use-cases/rate-limiter/ruby\ncurl -O $BASE/token_bucket.rb\ncurl -O $BASE/demo_server.rb",
      "section_id": "get-the-source-files"
    },
    {
      "id": "start-the-demo-server-ex0",
      "language": "bash",
      "code": "# Install dependencies\ngem install redis webrick\n\n# Run the demo server\nruby demo_server.rb",
      "section_id": "start-the-demo-server"
    },
    {
      "id": "response-headers-ex0",
      "language": "ruby",
      "code": "result = limiter.allow(\"user:#{user_id}\")\n\n# Add standard rate limit headers\nresponse['X-RateLimit-Limit'] = limiter.capacity.to_s\nresponse['X-RateLimit-Remaining'] = result[:remaining].to_i.to_s\nresponse['X-RateLimit-Reset'] = (Time.now.to_i + limiter.refill_interval).to_s\n\nunless result[:allowed]\n  response.status = 429 # Too Many Requests\n  response['Retry-After'] = limiter.refill_interval.ceil.to_s\nend",
      "section_id": "response-headers"
    },
    {
      "id": "using-with-rack-middleware-ex0",
      "language": "ruby",
      "code": "class RateLimitMiddleware\n  def initialize(app, limiter:, key_proc:)\n    @app = app\n    @limiter = limiter\n    @key_proc = key_proc\n  end\n\n  def call(env)\n    key = @key_proc.call(env)\n    result = @limiter.allow(key)\n\n    if result[:allowed]\n      status, headers, body = @app.call(env)\n      headers['X-RateLimit-Remaining'] = result[:remaining].to_i.to_s\n      [status, headers, body]\n    else\n      [429, { 'Content-Type' => 'application/json', 'Retry-After' => @limiter.refill_interval.ceil.to_s },\n       ['{\"error\":\"Rate limit exceeded\"}']]\n    end\n  end\nend\n\n# Apply per-IP rate limiting\nuse RateLimitMiddleware, limiter: limiter, key_proc: ->(env) { \"ip:#{env['REMOTE_ADDR']}\" }",
      "section_id": "using-with-rack-middleware"
    },
    {
      "id": "error-handling-ex0",
      "language": "ruby",
      "code": "begin\n  result = limiter.allow('user:123')\n  # Handle result\nrescue Redis::BaseError => e\n  puts \"Rate limiter error: #{e.message}\"\n  # Fail open or closed depending on your policy\nend",
      "section_id": "error-handling"
    },
    {
      "id": "fixed-window-counter-ex0",
      "language": "ruby",
      "code": "require \"redis\"\n\nFIXED_WINDOW_SCRIPT = <<~LUA\n  local key    = KEYS[1]\n  local limit  = tonumber(ARGV[1])\n  local window = tonumber(ARGV[2])\n\n  local count = redis.call('INCR', key)\n  if count == 1 then\n      redis.call('EXPIRE', key, window)\n  end\n\n  local ttl = redis.call('PTTL', key)\n\n  if count > limit then\n      return {0, ttl}\n  end\n  return {1, ttl}\nLUA\n\n# Returns a hash with :allowed and :retry_after_ms keys.\ndef fixed_window_allow(redis, key, limit, window_seconds)\n  allowed, ttl = redis.eval(FIXED_WINDOW_SCRIPT,\n                            keys: [key], argv: [limit, window_seconds])\n\n  { allowed: allowed == 1, retry_after_ms: allowed == 1 ? 0 : ttl }\nend",
      "section_id": "fixed-window-counter"
    },
    {
      "id": "sliding-window-log-ex0",
      "language": "ruby",
      "code": "require \"redis\"\nrequire \"securerandom\"\n\nSLIDING_WINDOW_LOG_SCRIPT = <<~LUA\n  local key    = KEYS[1]\n  local limit  = tonumber(ARGV[1])\n  local window = tonumber(ARGV[2])\n  local member = ARGV[3]\n\n  local t      = redis.call('TIME')\n  local now    = tonumber(t[1]) + tonumber(t[2]) / 1e6\n  local cutoff = now - window\n\n  redis.call('ZREMRANGEBYSCORE', key, '-inf', cutoff)\n\n  local count = redis.call('ZCARD', key)\n\n  if count < limit then\n      redis.call('ZADD', key, now, member)\n      redis.call('EXPIRE', key, window * 2)\n      return {1, 0}\n  end\n\n  local oldest = redis.call('ZRANGE', key, 0, 0, 'WITHSCORES')\n  local retry_after_ms = 0\n  if oldest[2] then\n      retry_after_ms = math.floor((tonumber(oldest[2]) + window - now) * 1000)\n  end\n\n  return {0, retry_after_ms}\nLUA\n\ndef sliding_window_log_allow(redis, key, limit, window_seconds)\n  allowed, retry_after_ms = redis.eval(SLIDING_WINDOW_LOG_SCRIPT,\n                                       keys: [key],\n                                       argv: [limit, window_seconds, SecureRandom.uuid])\n\n  { allowed: allowed == 1, retry_after_ms: retry_after_ms }\nend",
      "section_id": "sliding-window-log"
    },
    {
      "id": "sliding-window-counter-ex0",
      "language": "ruby",
      "code": "require \"redis\"\n\nSLIDING_WINDOW_COUNTER_SCRIPT = <<~LUA\n  local base   = KEYS[1]\n  local limit  = tonumber(ARGV[1])\n  local window = tonumber(ARGV[2])\n\n  local t   = redis.call('TIME')\n  local now = tonumber(t[1]) + tonumber(t[2]) / 1e6\n\n  local window_num = math.floor(now / window)\n  local elapsed     = (now % window) / window\n\n  local curr_key = base .. ':' .. window_num\n  local prev_key = base .. ':' .. (window_num - 1)\n\n  local prev = tonumber(redis.call('GET', prev_key) or 0)\n  local curr = tonumber(redis.call('GET', curr_key) or 0)\n\n  local estimate = prev * (1 - elapsed) + curr\n\n  if estimate >= limit then\n      return {0, 0}\n  end\n\n  local new_count = redis.call('INCR', curr_key)\n  if new_count == 1 then\n      redis.call('EXPIRE', curr_key, window * 2)\n  end\n\n  return {1, 0}\nLUA\n\ndef sliding_window_counter_allow(redis, key, limit, window_seconds)\n  allowed, = redis.eval(SLIDING_WINDOW_COUNTER_SCRIPT,\n                        keys: [\"{#{key}}\"], argv: [limit, window_seconds])\n\n  { allowed: allowed == 1 }\nend",
      "section_id": "sliding-window-counter"
    },
    {
      "id": "leaky-bucket-policing-ex0",
      "language": "ruby",
      "code": "require \"redis\"\n\nLEAKY_BUCKET_SCRIPT = <<~LUA\n  local key       = KEYS[1]\n  local capacity  = tonumber(ARGV[1])\n  local leak_rate = tonumber(ARGV[2])\n\n  local t   = redis.call('TIME')\n  local now = tonumber(t[1]) + tonumber(t[2]) / 1e6\n\n  local data      = redis.call('HGETALL', key)\n  local level     = 0\n  local last_leak = now\n\n  if #data > 0 then\n      for i = 1, #data, 2 do\n          if data[i] == 'level' then\n              level = tonumber(data[i+1])\n          elseif data[i] == 'last_leak' then\n              last_leak = tonumber(data[i+1])\n          end\n      end\n  end\n\n  local elapsed = now - last_leak\n  level = math.max(0, level - elapsed * leak_rate)\n\n  if level + 1 > capacity then\n      return {0, math.floor((level + 1 - capacity) / leak_rate * 1000)}\n  end\n\n  level = level + 1\n  local ttl = math.ceil(capacity / leak_rate) + 1\n\n  redis.call('HSET', key, 'level', level, 'last_leak', now)\n  redis.call('EXPIRE', key, ttl)\n\n  return {1, 0}\nLUA\n\ndef leaky_bucket_allow(redis, key, capacity, leak_rate)\n  allowed, retry_after_ms = redis.eval(LEAKY_BUCKET_SCRIPT,\n                                       keys: [key], argv: [capacity, leak_rate])\n\n  { allowed: allowed == 1, retry_after_ms: retry_after_ms }\nend",
      "section_id": "leaky-bucket-policing"
    }
  ]
}
