Redis Lua Script Builder

Generate a Redis Lua script for atomic operations

Creates a Redis Lua script for common atomic patterns — sliding/fixed-window rate limiting, distributed lock acquire and release, and atomic counter increment with expiry — including KEYS and ARGV documentation and an EVAL example.

Why run logic in a Lua script instead of multiple commands?

Redis executes a Lua script atomically — no other command runs in the middle. That eliminates the race conditions you get when reading a value, deciding, and writing it back as separate round trips.

Generate race-free Redis Lua scripts

Whenever you read a Redis value, make a decision, and write it back as separate commands, you have a race condition: another client can change the value in between. A Lua script runs atomically on the server, so the whole read-decide-write happens as one indivisible step. This builder produces correct, documented Lua for the three patterns that need atomicity most.

How it works

Pick a pattern and the builder emits a complete script plus its KEYS/ARGV contract:

  • Rate limiterINCR the window counter; on the first hit set EXPIRE to the window length; return 1 if the count is within the limit, else 0. Because increment and check are atomic, bursts cannot slip past the limit.
  • Distributed lock — acquire with SET key token NX PX ttl (returns whether the lock was taken); release by GET-ing the key, comparing it to your token, and only DEL-ing on a match so you never release someone else’s lock.
  • Atomic counterINCRBY by a step and set a TTL on first creation, returning the new value, useful for quotas and metrics that must not lose updates.

Each script uses KEYS[1] for the key and ARGV[…] for limits, TTLs, and tokens, with a comment block documenting every parameter and an example EVAL call wired to your chosen defaults.

Tips and notes

  • Load the script once with SCRIPT LOAD and invoke it by hash with EVALSHA to avoid sending the body on every call.
  • Pass the current time from your application as an ARGV value rather than calling TIME inside the script — that keeps the script deterministic and replication-safe.
  • In Redis Cluster, every key a script touches must be in KEYS and hash to the same slot; do not construct key names from ARGV.
  • For the lock, set the TTL longer than your worst-case critical section, and treat a failed acquire as “retry with backoff”, not an error.