Back to results for “Commands”
Redis › Lua Scripting

Why would you use a Lua script with Redis instead of just sending several separate commands from your application?

Beginner

Answer

A Lua script runs entirely on the Redis server as a single atomic operation, meaning no other client's commands can run in between the individual steps of your script, which both guarantees consistency for multi step logic and reduces network round trips compared to sending several separate commands back and forth between your application and Redis.
EVAL "return redis.call('set', KEYS[1], ARGV[1])" 1 mykey myvalue
Real-world example A ticket booking system uses a Lua script to atomically check available seats and reserve one in a single operation, preventing a race condition where two customers might otherwise both believe they successfully booked the very last seat.

Common follow-ups: What does the atomicity guarantee of a Lua script actually protect against?;What is the difference between EVAL and EVALSHA?