This is a Redis rate limiting demo for JS and Node using:
NOTE: Read the tutorial on rate limiters for a guide.
This demo implements five rate limiting algorithms, each backed by different Redis data structures:
| Algorithm | Redis Data Structure | Description |
|---|---|---|
| Fixed Window Counter | STRING (INCR + EXPIRE) |
Counts requests in fixed time windows. Simple but susceptible to boundary bursts. |
| Sliding Window Log | SORTED SET (ZADD + ZREMRANGEBYSCORE) |
Logs each request timestamp. Precise sliding window, but stores every request. |
| Sliding Window Counter | STRING x2 (weighted) |
Weighted average of current and previous window counts. Smooths the fixed-window boundary problem. |
| Token Bucket | HASH + Lua script |
Tokens refill at a steady rate; each request consumes one. Allows short bursts. |
| Leaky Bucket | HASH + Lua script |
Requests fill a bucket that leaks at a constant rate. Smooths traffic to steady output (shaping), or drops requests above the bucket size (policing). |
Copy and edit the .env file:
cp .env.example .envYour .env file should contain the connection string you copied from Redis Cloud.
Your .env.docker file will look similar to .env, but should use the appropriate docker internal URLs. Here is
an example:
REDIS_URL="redis://redis:6379"Next, spin up docker containers:
bun dockerYou should have a server running on http://localhost:<port> where the port is set in your .env file (default is 8080).
Open http://localhost:8080 in your browser to see the demo UI. Each rate limiting algorithm has a card with:
- Send Request -- sends a single request through the rate limiter
- Burst 10 -- sends 10 rapid requests to see the algorithm hit its limit
- Reset All Counters -- clears all rate-limit keys from Redis
POST /api/rate-limit/:algorithm-- Test a single request against the named algorithmPOST /api/rate-limit/:algorithm/burst-- Send 10 rapid requests against the named algorithmPOST /api/rate-limit/reset-- Reset all rate-limit counters
Where :algorithm is one of: fixed-window, sliding-window-log, sliding-window-counter, token-bucket, leaky-bucket.
There are some tests in the __tests__ folder that can be run with the following command:
bun testThese tests setup and teardown on their own. You can modify them if you want to leave data in Redis.
To run the development server outside of docker:
bun install
# then
bun devFormatting code:
bun formatUpdating dependencies:
bun updateIf you don't yet have a database setup in Redis Cloud get started here for free.
Then follow the Connect to a Redis Cloud database doc. You should end up with a connection string that looks like the string below:
REDIS_URL="redis://default:<password>@redis-#####.c###.us-west-2-#.ec2.redns.redis-cloud.com:#####"Run the tests to verify that you are connected properly.
To learn more about Redis, take a look at the following resources:
- Redis Documentation - learn about Redis products, features, and commands.
- Redis Tutorials - read tutorials, quick starts, and how-to guides for Redis.
- Redis Demo Center - watch short, technical videos about Redis products and features.