Redis Lua Script Generator

Choose a pattern such as a rate limiter, token bucket, lock release or compare-and-set and get an atomic Redis Lua script together with ready-to-run EVAL or client code.

Use the tool

Why this tool exists

Redis commands are atomic one at a time, but real tasks need several steps: read a counter, check a limit, set an expiry. Running those as separate calls leaves a gap in which another client can change the data, so rate limits overshoot and locks are released by the wrong owner. A Lua script runs as one unit and closes that gap.

The patterns are well known, yet the details are easy to get subtly wrong, such as releasing a lock only if the value still matches. This tool gives you tested-style templates with the right KEYS and ARGV layout. The scripts are not executed here. Run them on a development Redis first. The text is generated in your browser.

How to use it

  1. Choose a pattern: rate limiter, token bucket, lock release or extend, compare-and-set or capped increment.
  2. Pick the client language for the example call.
  3. Read the script and the way KEYS and ARGV are used.
  4. Copy it, then test it on a development Redis instance before using it in production.

Frequently asked questions

Why use Lua instead of MULTI and EXEC?

A transaction cannot make a decision based on a value it has just read. A script can read, compare and write in one atomic step, which is what limiters and safe lock release need.

Does the tool run the script?

No. It only produces the text. Test on a dev Redis, with your Redis version and settings, and check how the script behaves under cluster mode, where all keys must live in the same slot.

How do I pass keys and arguments?

Keys go in the KEYS array and everything else, such as limits and timeouts, in ARGV. The generated example shows the count of keys followed by the values in EVAL order.