Skip to main content

Usage

Executes a Lua script that has been previously loaded into Redis. Instead of sending the entire script, you only send the SHA-1 hash of the script, which is more efficient for frequently executed scripts.

Parameters

sha1
string
required
The SHA-1 hash of the script to execute. Obtain this by using SCRIPT LOAD or by computing the SHA-1 hash of your script.
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

Load and Execute a Script

Handling Missing Scripts

If the script isn’t loaded, EVALSHA will fail with a NOSCRIPT error:

Conditional Operations

EVALSHA vs EVALSHA_RO

EVALSHA

The evalsha command can execute scripts that modify data:

EVALSHA_RO (Read-Only)

Use evalshaRo for read-only scripts that don’t modify data:
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

The SDK provides a Script class that automatically handles SHA-1 computation and caching:
The exec method is recommended as it:
  1. First tries EVALSHA (fast, using cached script)
  2. If script not found, falls back to EVAL
  3. Subsequent calls will use EVALSHA
For read-only scripts:

Benefits of EVALSHA

  1. Reduced bandwidth: Only the SHA-1 hash is sent instead of the entire script
  2. Better performance: Script is pre-compiled and cached on the server
  3. Ideal for frequently executed scripts: Especially beneficial for scripts executed many times

See Also

  • EVAL - Execute a Lua script
  • SCRIPT LOAD - Load a script into the server cache