Overview
The Upstash Redis SDK provides multiple ways to execute Lua scripts:
- Direct execution:
eval, evalsha, evalRo, evalshaRo
- Script helpers:
Script and ScriptRO classes for automatic caching
- Functions:
fcall and fcallRo for Redis Functions (Redis 7.0+)
Direct Script Execution
EVAL Command
Execute a Lua script directly:
Type Parameters
Specify return types for better type safety:
EVALSHA Command
Execute a script by its SHA1 hash (must be loaded first):
Read-Only Variants
Use evalRo and evalshaRo for read-only scripts (available in cluster mode):
Script Helper Classes
Script Class
The Script class provides automatic script caching and optimistic execution:
How It Works
- First execution: Tries
EVALSHA (optimistic)
- If script not cached: Falls back to
EVAL and caches it
- Subsequent executions: Uses cached
EVALSHA
Script Methods
SHA1 Hash
The sha1 property is deprecated and initialized asynchronously. Avoid using it immediately after creating the script.
ScriptRO Class (Read-Only)
For read-only scripts, use the ScriptRO class:
Practical Script Examples
Atomic Increment with Limit
Conditional Set
Batch Operations
Rate Limiting
Redis Functions (7.0+)
FCALL Command
Execute a loaded Redis Function:
Read-Only Function Calls
Managing Functions
Best Practices
- Use Script helpers for frequently executed scripts - They provide automatic caching
- Keep scripts simple - Complex logic is better handled in application code
- Prefer read-only variants when possible - Better for clustering and replication
- Type your results - Use TypeScript generics for type safety
- Handle errors gracefully - Scripts can fail due to syntax or runtime errors
See Also