Skip to main content

Usage

Executes a Lua script on the Redis server. The script can access keys and arguments passed to it.

Parameters

script
string
required
The Lua script to execute.
keys
string[]
required
Array of key names that the script will access. These are available in the script as KEYS[1], KEYS[2], etc.
args
unknown[]
Array of arguments to pass to the script. These are available in the script as ARGV[1], ARGV[2], etc.

Response

result
unknown
The value returned by the Lua script. The type depends on what the script returns.

Examples

Basic Script Execution

Incrementing a Counter

Conditional Set

Working with Multiple Keys

EVAL vs EVAL_RO

EVAL

The eval command can execute scripts that modify data:

EVAL_RO (Read-Only)

Use evalRo for read-only scripts that don’t modify data. This is more efficient for read replicas:
Read-only scripts:
  • Can be executed on read replicas
  • Are optimized for read-heavy workloads
  • Will error if they attempt to modify data

Using the Script Class

For better performance with frequently used scripts, use the Script class which automatically handles SHA-1 caching:
The Script class provides three methods:
  • eval(keys, args) - Always uses EVAL
  • evalsha(keys, args) - Always uses EVALSHA (requires script to be loaded)
  • exec(keys, args) - Optimistically tries EVALSHA, falls back to EVAL if needed
For read-only scripts, use createScript with the readOnly option:

See Also

  • EVALSHA - Execute a cached script by SHA-1 hash
  • SCRIPT LOAD - Load a script into the server cache
  • FCALL - Call a Redis function