Back to results for “Commands”
Redis › Lua Scripting

How do you call other Redis commands from within a Lua script, and what restrictions apply to this?

Intermediate

Answer

You call other Redis commands from within a Lua script using the redis.call or redis.pcall functions, passing the command name and its arguments, and while you have access to most standard Redis commands this way, certain commands that could introduce non determinism or block the server for a long time are restricted or behave differently within a script's context.
local current = redis.call('get', KEYS[1])
if current == false then
  redis.call('set', KEYS[1], ARGV[1])
end
Real-world example An inventory management script checks whether a product's stock key already exists using redis.call, only initializing it with a default value if it does not already exist, all within a single atomic script execution.

Common follow-ups: What is the difference between redis.call and redis.pcall for error handling?;What Redis commands are restricted from being used inside a Lua script?