Usage
Parameters
The Lua script to load into the server cache.
Response
The SHA-1 hash of the loaded script. This hash can be used with EVALSHA to execute the script.
Examples
Load a Simple Script
Load a Counter Script
Load Multiple Scripts
Atomic Multi-Key Operations
Complex Data Processing
When to Use SCRIPT LOAD
Good Use Cases
- Frequently executed scripts: Load once, execute many times
- Application startup: Pre-load all scripts your application needs
- Performance optimization: Reduce bandwidth and parsing overhead
- Complex atomic operations: Ensure scripts are available before use
Example: Application Initialization
Script Caching Behavior
-
Scripts are cached on the Redis server until:
- The server restarts
- The script cache is explicitly flushed
- Memory pressure causes eviction (rare)
- Scripts are not automatically replicated to read replicas. If using read replicas, you must load scripts on each instance.
Using the Script Class
Instead of manually managing SHA-1 hashes, use theScript class:
Script class:
- Automatically computes the SHA-1 hash
- Optimistically tries EVALSHA first
- Falls back to EVAL if the script isn’t loaded
- Caches the script on the server after first use