# Rate Limiting

> Implementation of Token Bucket and Leaky Bucket algorithms for load balancing.

- Skill: `j4flmao/rate-limiting` (Agent Skill)
- Install (CLI): `npx skillmds@latest add j4flmao/rate-limiting`
- Raw SKILL.md: https://api.skillmd.com/api/skills/j4flmao/rate-limiting/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: j4flmao (https://skillmd.com/u/j4flmao)
- Updated: 2026-09-21
- Page: https://skillmd.com/skills/j4flmao/rate-limiting

---


# Rate Limiting

## Algorithms

### 1. Token Bucket
Tokens are added to a bucket at a fixed rate. Each request consumes a token. If the bucket is empty, the request is dropped.

### 2. Leaky Bucket
Requests are added to a queue (bucket). The queue is processed at a fixed rate. If the queue is full, new requests are dropped.

## Redis Lua Script (Token Bucket)
```lua
-- KEYS[1]: rate limit key
-- ARGV[1]: capacity (max tokens)
-- ARGV[2]: rate (tokens per second)
-- ARGV[3]: current timestamp

local key = KEYS[1]
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])

local info = redis.call("HMGET", key, "tokens", "last_update")
local tokens = tonumber(info[1])
local last_update = tonumber(info[2])

if tokens == nil then
    tokens = capacity
    last_update = now
else
    local delta = math.max(0, now - last_update)
    tokens = math.min(capacity, tokens + delta * rate)
end

if tokens >= 1 then
    tokens = tokens - 1
    redis.call("HMSET", key, "tokens", tokens, "last_update", now)
    redis.call("EXPIRE", key, math.ceil(capacity / rate))
    return 1 -- Allowed
else
    return 0 -- Rate Limited
end
```

## Architecture
```mermaid
%%{init: {"theme": "default", "flowchart": {"useMaxWidth": true}}}%%
flowchart TD
    A[Client Request] --> B{Rate Limiter}
    B -- Allowed --> C[API Gateway]
    C --> D[Backend Service]
    B -- Denied --> E[429 Too Many Requests]
```

