Skip to main content

Usage

Loads a Lua script into the Redis server’s script cache without executing it. Returns the SHA-1 hash of the script, which can be used with EVALSHA to execute the script later.

Parameters

script
string
required
The Lua script to load into the server cache.

Response

sha1
string
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

  1. Frequently executed scripts: Load once, execute many times
  2. Application startup: Pre-load all scripts your application needs
  3. Performance optimization: Reduce bandwidth and parsing overhead
  4. 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 the Script class:
The 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

See Also

  • EVAL - Execute a Lua script
  • EVALSHA - Execute a cached script by SHA-1 hash