The Desi Architect
Notes / Docker Sandboxes / Distributed Token Bucket Rate Limiter with Redis Lua
Docker Sandboxes · Tier 2: Data & Scale · Lab 33

Distributed Token Bucket Rate Limiter with Redis Lua

Run four rate limiting algorithms as Lua scripts inside Redis, under 25 concurrent clients. See why the limiter must be one atomic step, how a fixed window doubles the rate at its edge, and what sliding window, token bucket and leaky bucket each do instead. Then freeze Redis and decide: fail open or fail closed?

Time35 minutes
StackRedis 7 + LuaPorts 16390
MemoryAbout 40 MB
Before thisLab 07Lua in Redis
Verified runWindows, Mac, LinuxBash and PowerShell
  1. 1PrepareWhat you will learn
  2. 2PredictGuess before you run
  3. 3RunStep by step
  4. 4ObserveWhat you should see
  5. 5Break itChange one thing
  6. 6Clean upLeave nothing running
On this page
  1. 00 / What You Will Learn
  2. 01 / Why It Matters
  3. 02 / What We Are Building
  4. 03 / Words You Need
  5. 04 / Predict First
  6. 05 / Step by Step
  7. 06 / What You Should See
  8. 07 / Look Inside
  9. 08 / Break It, Fix It
  10. 09 / In Real Projects
  11. 10 / Interview Corner
  12. 11 / Clean Up

00 /What You Will Learn

By the end you will be able to
  • Explain why a limiter must be one atomic step and show the leak when it is two.
  • Show the fixed window boundary burst and how a sliding window counter reduces it.
  • Tell a token bucket (allows bursts) from a leaky bucket (smooths output).
  • Choose fail open or fail closed for an endpoint.

Before you start

Finish Lab 07 (Redis and Lua Scripting) and Lab 08 (Race Conditions) first. You should know what a Lua script does inside Redis and what a race condition is. Docker Desktop and Python 3 are needed.