Redis Lua Script Generator

Pick an operation and get a Lua script that runs it atomically. Two rules decide whether a script survives contact with production: every key goes through KEYS, and the script stays short, because it stops the whole server while it runs.

The operation

Every key is passed through KEYS and never written into the script body, because Redis Cluster routes a script by its declared keys. A key built inside the script is invisible to that routing: it works on a single instance and breaks the day the data is sharded.

script.lua

updates as you type

    Examples

    Worked setups you can load into the form above. Each one is a decision the generator makes differently, and the reason it makes it.

    A counter with a ceiling

    The TTL is set on the first write only, so the window does not slide

    operation
    atomic-counter

    Delete only if unchanged

    The comparison and the delete happen with nothing in between

    operation
    compare-and-delete

    Common mistakes

    These are the ones that fail silently. The config is accepted, nothing raises an error, and the consequence arrives later.

    1. Building a key name inside the script

      Redis Cluster routes by the keys in KEYS. A key constructed in the body is invisible to routing, so the script works on one instance and breaks when the data is sharded.

      Instead:Compute key names in the client and pass them in KEYS.

    2. Expecting a failed script to roll back

      Atomic means uninterrupted, not transactional. Commands that already ran stay applied when a later one raises.

      Instead:Validate everything before the first write.

    3. Iterating a large collection inside a script

      The whole server is stopped for the duration. Once it passes busy-reply-threshold, SCRIPT KILL will not work if the script has written.

      Instead:Bound the work, or do it in the client across several calls.

    4. Sending only EVALSHA and never the body

      The script cache is in memory and is not replicated. After a restart or a failover every call fails with NOSCRIPT.

      Instead:Handle NOSCRIPT by resending the script once.

    5. Getting the key count wrong in EVALSHA

      The number after the sha is the key count. Too low and the extra names land in ARGV, so the script reads empty values and does nothing, silently.

      Instead:Count the keys, and let the client library do it.

    What atomic means here, and what it does not

    Lua in Redis gives you one strong guarantee and one that people assume but do not get. The gap between them causes real data problems.

    Nothing else runs during a script

    Lua executes on the same single thread that serves every client, and it runs to completion with nothing interleaved. That is exactly why a check followed by a write is safe here and is a race when done as two commands from a client.

    A script that fails partway is NOT rolled back

    Atomic here means uninterrupted, not transactional. If the third command raises an error, the first two have already applied and they stay applied. Redis has no rollback, for scripts or for MULTI. Do every validation before the first write.

    Every key must be declared in KEYS

    Redis Cluster routes a script by the keys passed in KEYS. A key built inside the script body, or concatenated from ARGV, is invisible to that routing. The script works perfectly on a single instance and breaks the day the data is sharded, which is the worst possible time to discover it.

    A long script is an outage

    Because nothing else runs, a script that loops over a large collection stalls every other connection. Past busy-reply-threshold the only ways out are SCRIPT KILL, which fails once the script has written, and SHUTDOWN NOSAVE, which loses whatever was not persisted.

    EVALSHA fails after a restart or a failover

    The script cache is in memory and is not replicated to a promoted replica. A client that only ever sends EVALSHA breaks at exactly the moment of a failover. Every mature client library handles the NOSCRIPT error by resending the body; confirm yours is one of them.

    More redis tools

    Redis Hash Slot Calculator Which of the 16,384 slots does this key land in? Redis Hash Tag Tester Will these keys survive a multi-key command? Redis RESP Protocol Decoder Read what the server actually sent Redis RESP2 vs RESP3 Reply Decoder What the same reply looks like on each protocol Redis Command to RESP Exactly what your client puts on the socket Redis Glob Pattern Tester Redis globs are not shell globs Redis Connection URL Parser The path is the database number Redis Memory Unit Converter 1g and 1gb are not the same number Redis TTL Converter -1 and -2 are not durations Redis Stream ID Parser The first half is a millisecond timestamp redis.conf Validator Will Redis start with this file? Redis Production Config Linter The settings that cause incidents redis.conf to CONFIG SET Which of these can you change without a restart? Redis ACL Rule Decoder What does this user actually get? Redis ACL Validator Find the rule that does nothing Redis INFO Analyzer The numbers INFO does not print Redis SLOWLOG Analyzer What blocked everyone else Redis CLUSTER NODES Parser Read the topology, and find the gap Redis Cluster Slot Distribution Who owns how much, and what is missing Redis Keyspace Notification Flags Why your events never arrive Redis Memory Calculator The encoding decides, not the data Redis Key Count to Memory The fixed cost per key Redis Encoding Threshold Calculator One field more, several times the memory Redis Bitmap Memory Calculator Sized by the highest bit, not the set ones Redis HyperLogLog Calculator 12 KB whether you count a thousand or a billion Redis Cluster Sizing Only 60% of each node is usable Redis RDB and AOF Size Calculator The fork needs memory, not disk Redis Replication Bandwidth Calculator How long the backlog actually covers Redis Connection Pool Calculator More connections is not more throughput Redis Pipeline Calculator It removes round trips, not work Redis Cache Hit Rate Calculator 99% to 90% is ten times the backend load Redis Eviction Policy Simulator volatile- with no TTLs is noeviction Redis Cost Estimator Your rates, so nothing goes stale Redis Config File Generator A redis.conf with the reasons in it Redis ACL Generator A user that can do exactly one job Redis Maxmemory Config Generator The limit, and the headroom it needs Redis Persistence Config Generator How much you can afford to lose Redis TLS Config Generator Encrypted, and the old port actually closed Redis Sentinel Config Generator Failover that can actually be authorised Redis Cluster Config Generator Three primaries, and the bus port open Redis Docker Compose Generator Local Redis that is not on the internet Redis Client Config Generator Timeouts on both sides, and a sane pool Redis MEMORY STATS Analyzer Which number actually matters Redis Bigkeys Output Analyzer Elements are not bytes Redis CLIENT LIST Analyzer Find the connection hurting you Redis LATENCY Report Analyzer An empty report may mean nothing was recorded Redis Keyspace Prefix Analyzer Which key family is growing Redis SET Command Builder A plain SET clears the TTL Redis ZRANGE Query Builder REV reverses the argument order Redis SCAN Iteration Planner COUNT is a hint, not a page size Redis Key Name Validator Legal is not the same as workable Redis Cluster Compatibility Checker Works now, breaks when you shard

    Elsewhere on the site