🔴 Redis
Intermediate
What is Lua scripting in Redis and when should you use it?
Answer
Redis supports executing Lua scripts atomically on the server via EVAL script numkeys key [key ...] arg [arg ...]. The entire script runs as a single atomic unit — no other command can execute between Lua operations. This is stronger than MULTI/EXEC because Lua allows conditional logic: you can read a value inside the script and branch based on it before writing, which is impossible in a MULTI block (where all commands are queued before any result is known). Common uses: complex atomic operations like "check inventory and decrement if available," rate limiting with fine-grained logic, and distributed lock implementation. Use EVALSHA to execute a pre-loaded script by its SHA1 hash, avoiding repeated network transfer of the script body.
More Redis Questions
View all →- Intermediate What is Redis Cluster and how does it shard data?
- Intermediate What is the difference between Redis Sentinel and Redis Cluster?
- Intermediate How do MULTI and EXEC work in Redis transactions?
- Intermediate What is WATCH and optimistic locking in Redis?
- Intermediate What are Redis eviction policies?