Skip to main content
The Script and ScriptRO classes allow you to execute Lua scripts efficiently by using Redis’s EVALSHA command, which caches scripts on the server for faster execution.

Overview

Scripts provide an optimized way to execute Lua code in Redis:
  1. First execution attempts EVALSHA with the script’s SHA-1 hash
  2. If the script isn’t cached, falls back to EVAL to load and execute it
  3. Subsequent executions use the cached version via EVALSHA
This approach minimizes bandwidth and improves performance for frequently used scripts.

Creating Scripts

Script (Read-Write)

Create a script that can modify data:

ScriptRO (Read-Only)

Create a read-only script that uses EVAL_RO and EVALSHA_RO:
script
string
required
The Lua script to execute
options
{ readonly?: boolean }
Script configuration options
script
Script<TResult> | ScriptRO<TResult>
A Script or ScriptRO instance based on the readonly option

Script Class

Properties

script

The Lua script source code.
script
string
The Lua script source code

sha1

Deprecated: This property is initialized asynchronously and may be an empty string immediately after construction. It’s exposed for backwards compatibility only.
The SHA-1 hash of the script.
sha1
string
The SHA-1 hash of the script (may be empty if not yet initialized)

Methods

eval()

Execute the script using the EVAL command (sends full script to Redis).
keys
string[]
required
Array of Redis keys that the script will access
args
string[]
required
Array of additional arguments to pass to the script
result
TResult
The script execution result

evalsha()

Execute the script using the EVALSHA command (uses cached SHA-1 hash).
keys
string[]
required
Array of Redis keys that the script will access
args
string[]
required
Array of additional arguments to pass to the script
result
TResult
The script execution result
If the script is not cached on the server, this will throw a NOSCRIPT error. Use exec() for automatic fallback.

exec()

Optimistically execute the script with automatic fallback.
  1. First attempts EVALSHA (fast, uses cached script)
  2. If script not cached (NOSCRIPT error), falls back to EVAL
  3. Subsequent calls use the cached version
keys
string[]
required
Array of Redis keys that the script will access. In Lua, access via KEYS[1], KEYS[2], etc.
args
string[]
required
Array of additional arguments. In Lua, access via ARGV[1], ARGV[2], etc.
result
TResult
The script execution result

ScriptRO Class

The ScriptRO class is identical to Script but uses read-only commands (EVAL_RO and EVALSHA_RO). This is useful for scripts that only read data and can be executed on read replicas.

Methods

evalRo()

Execute the script using EVAL_RO.
keys
string[]
required
Array of Redis keys to read
args
string[]
required
Array of additional arguments
result
TResult
The script execution result

evalshaRo()

Execute the script using EVALSHA_RO.
keys
string[]
required
Array of Redis keys to read
args
string[]
required
Array of additional arguments
result
TResult
The script execution result

exec()

Execute with automatic fallback (tries EVALSHA_RO, falls back to EVAL_RO).
keys
string[]
required
Array of Redis keys to read
args
string[]
required
Array of additional arguments
result
TResult
The script execution result

Usage Examples

Basic Script Execution

Atomic Counter with TTL

Conditional Operations

Read-Only Script for Analytics

Batch Operations

Rate Limiting

Complex Data Structure Operations

TypeScript Support

Scripts support full TypeScript type inference:

Best Practices

1. Use TypeScript Generics

2. Prefer exec() Over eval()

3. Use Read-Only Scripts When Possible

4. Handle Errors Properly

5. Document Complex Scripts