{
  "id": "rate-limiter",
  "title": "Token bucket rate limiter with Redis",
  "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/",
  "summary": "Implement a token bucket rate limiter using Redis and Lua scripts",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-04-16T13:29:55-07:00",
  "children": [
    {
      "id": "nodejs",
      "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in Node.js",
      "title": "Token bucket rate limiter with Redis and Node.js",
      "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/nodejs/"
    },
    {
      "id": "go",
      "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in Go",
      "title": "Token bucket rate limiter with Redis and Go",
      "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/go/"
    },
    {
      "id": "java-jedis",
      "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in Java",
      "title": "Token bucket rate limiter with Redis and Java",
      "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/java-jedis/"
    },
    {
      "id": "dotnet",
      "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in .NET",
      "title": "Token bucket rate limiter with Redis and .NET",
      "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/dotnet/"
    },
    {
      "id": "java-lettuce",
      "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in Java with Lettuce",
      "title": "Token bucket rate limiter with Redis and Java (Lettuce)",
      "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/java-lettuce/"
    },
    {
      "id": "php",
      "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in PHP",
      "title": "Token bucket rate limiter with Redis and PHP",
      "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/php/"
    },
    {
      "id": "ruby",
      "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in Ruby",
      "title": "Token bucket rate limiter with Redis and Ruby",
      "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/ruby/"
    },
    {
      "id": "rust",
      "summary": "Implement a token bucket rate limiter using Redis and Lua scripts in Rust",
      "title": "Token bucket rate limiter with Redis and Rust",
      "url": "https://redis.io/docs/latest/develop/use-cases/rate-limiter/rust/"
    }
  ],
  "page_type": "content",
  "content_hash": "619304f90a754aa55f92e7a1fae6071db96daee517402e9adbf2530fbae9b98e",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "This guide shows you how to implement a distributed token bucket rate limiter using Redis and Lua scripts for atomic operations."
    },
    {
      "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."
    },
    {
      "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": "using-the-python-module",
      "title": "Using the Python module",
      "role": "content",
      "text": "The `TokenBucket` class provides a simple interface for rate limiting\n([source](token_bucket.py)):\n\n[code example]"
    },
    {
      "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 10 requests 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": "running-the-demo",
      "title": "Running the demo",
      "role": "content",
      "text": "A demonstration web server is included to show the rate limiter in action\n([source](demo_server.py)):\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 easily change the host\nand port in the `demo_server.py` script. 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": "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* [Transactions](https://redis.io/docs/latest/develop/using-commands/transactions) - Alternative to Lua scripts for atomicity"
    }
  ],
  "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": "using-the-python-module-ex0",
      "language": "python",
      "code": "import redis\nfrom token_bucket import TokenBucket\n\n# Create a Redis connection\nr = redis.Redis(host='localhost', port=6379, decode_responses=True)\n\n# Create a rate limiter: 10 requests per second\nlimiter = TokenBucket(\n    redis_client=r,\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\nallowed, remaining = limiter.allow('user:123')\n\nif allowed:\n    print(f\"Request allowed. {remaining} tokens remaining.\")\n    # Process the request\nelse:\n    print(\"Request denied. Rate limit exceeded.\")\n    # Return 429 Too Many Requests",
      "section_id": "using-the-python-module"
    },
    {
      "id": "running-the-demo-ex0",
      "language": "bash",
      "code": "# Install dependencies\npip install redis\n\n# Run the demo server\npython demo_server.py",
      "section_id": "running-the-demo"
    },
    {
      "id": "response-headers-ex0",
      "language": "python",
      "code": "allowed, remaining = limiter.allow(f'user:{user_id}')\n\n# Add standard rate limit headers\nresponse.headers['X-RateLimit-Limit'] = str(limiter.capacity)\nresponse.headers['X-RateLimit-Remaining'] = str(int(remaining))\nresponse.headers['X-RateLimit-Reset'] = str(int(time.time() + limiter.refill_interval))\n\nif not allowed:\n    response.status_code = 429  # Too Many Requests\n    response.headers['Retry-After'] = str(int(limiter.refill_interval))",
      "section_id": "response-headers"
    }
  ]
}
