Lua Scripting Interview Questions
Lua Scripting interview questions for Redis — fundamentals through advanced scenarios.
- 20Questions with answers
- 3Difficulty levels
Questions (20)
Browse beginner, intermediate, and advanced questions with answers — hide them when you want to self-test.
What atomicity does EVAL actually give you?
Lua EVAL runs atomically on the primary: no other command interleaves until the script returns. That is stronger than MULTI/EXEC with WATCH for read-modify-write on the listed keys. Long scripts still block the whole instance.
Why does EVALSHA return NOSCRIPT, and what should the client do?
EVALSHA sends the SHA1 of a loaded script so the body is not retransmitted. NOSCRIPT means the server does not have that SHA; the client must EVAL the body and retry. Pools that connect to new nodes must handle NOSCRIPT.
Why do key names belong in KEYS rather than ARGV?
KEYS array versus ARGV: Cluster hashing uses KEYS, not ARGV. Putting key names in ARGV to be flexible breaks Cluster and the atomicity contract. Always declare keys in KEYS.
What does lua-time-limit protect, and what does it not prevent?
lua-time-limit, default five seconds, kills a runaway script with an error but the instance was blocked until then. Busy scripts show up as everyone else timing out. Split work or do it in the app.
When do you SCRIPT LOAD versus sending EVAL every time?
SCRIPT LOAD plus EVALSHA is the deploy path; SCRIPT FLUSH on a node creates a NOSCRIPT storm. Do not FLUSH in production casually. SCRIPT EXISTS checks SHAs before a rollout.
What is the difference between redis.call and redis.pcall?
redis.call raises on Redis errors and aborts the script; redis.pcall returns an error value. Uncaught errors still leave the atomic block. Handle WRONGTYPE explicitly when the key might be the wrong structure.
When should you not use Lua at all?
When not to use Lua: multi-key work that spans slots, scripts that call Redis with huge payloads, or logic that belongs in the app for testability. Lua is not a general application server. If you need loops over SCAN of the whole keyspace, stop.
How can non-deterministic Lua desync replicas?
Non-deterministic Lua using TIME or math.random used to desync replicas when the script body was re-executed. redis.replicate_commands() or writing only deterministic Redis calls is the safe pattern. Prefer redis.call of INCR over computing random in Lua.
When is a short Lua script better than a WATCH/MULTI retry loop?
WATCH/MULTI versus Lua: WATCH retries on conflict; Lua does not yield, so it cannot deadlock with another Lua, but it can starve others by running long. For a short compare-and-set, Lua is simpler than WATCH loops.
How are Lua scripts replicated to replicas?
Replication of scripts: replicas either re-execute the script or apply the write commands the script produced, depending on version and replicate_commands. Non-deterministic scripts plus script replication equal split-brain data. Keep scripts as pure Redis writes.
Can you EVAL a write script against a replica?
EVAL on a replica: replicas reject writes unless you are in an unusual debug story. Read-only Lua that only redis.call GET can still be wrong if it assumes it can SET. Send writes to the master.
Why does EVALSHA work on one Cluster shard and fail on the next?
Script cache is per node: Cluster means every master you hit needs the SHA. A client that EVALSHA against a new shard gets NOSCRIPT. Frameworks should load on all nodes or fall back to EVAL.
How do you debug a Lua script without harming production?
Debugging Lua: redis.log, SCRIPT DEBUG, and isolating with redis-cli --eval. Never debug with MONITOR on production while a hot script runs. Unit-test scripts with a throwaway instance.
Does pipelining EVALSHA make scripts run concurrently?
Lua and pipelining: you can pipeline EVALSHA but each script still runs atomically and serially. Pipelining does not make two scripts concurrent. Giant pipelines of EVALSHA can still spike p99.
Why are global Lua variables a footgun across SCRIPT LOAD?
Global variables in Lua persist in the interpreter across scripts unless you take care—this is a footgun. Use local. Shared globals plus SCRIPT LOAD lead to cross-request contamination.
Why do Lua scripts that pass standalone tests fail on Cluster?
Breaking Cluster rules inside Lua with undocumented tricks will pass on standalone and fail in production Cluster. Test scripts against a real Cluster. Hash tags in KEYS are the supported way to touch two keys.
Why can Redis Lua not call HTTP APIs, and why is that good?
lua sandbox: Redis Lua cannot do OS or network; that is a feature. Needing a JSON HTTP call from Lua means the design is wrong. Keep side effects as Redis writes only.
What happens if a script uses a Redis 7 command on a Redis 6 node?
Version skew: a script that uses Redis 7 commands fails on 6 with a runtime error inside EVAL, rolling back the atomic block. Pin script compatibility. Deploy scripts in the same change as the server upgrade.
Can Lua remove contention on a single hot INCR key?
High contention INCR in Lua around a HASH is still one thread; Lua does not add parallelism. If the hot key is the bottleneck, Lua cannot save you. Shard the counter instead.
When does SCRIPT KILL refuse to stop a script?
SCRIPT KILL only stops a script that has not yet written; after the first write you wait. A stuck write script is why lua-time-limit and short bodies matter. Design for killability.
Practice with AI mock interviews
Run Redis mock interviews with AI follow-ups, instant feedback, and analytics on AiLx.
Free to start · No credit card required