Overview
The Redis persistence adapter provides distributed locks, circuit breaker state management, caching, and checkpoint support using Redis as the backend. It leverages Redis’s native expiration features and atomic operations for high-performance distributed coordination.Installation
Features
- Distributed Locks: TTL-based locks with automatic expiration
- Circuit Breaker State: Persistent failure tracking across instances
- Caching: High-performance cache with TTL support
- Checkpoints: Save and restore task state for fault tolerance
- Idempotency: Prevent duplicate operations (via
RedisIdempotencyAdapter) - Key Prefixing: Namespace isolation for multi-tenant applications
Basic Usage
Configuration
ioredis Redis client instance
Configuration options
Lock Provider Methods
acquire(key, ttl, owner?)
Acquires a distributed lock with automatic TTL-based expiration.Lock identifier
Time-to-live in milliseconds
Optional lock owner identifier (auto-generated if not provided)
Promise<LockHandle | null> - Lock handle or null if already locked
extend(key, ttl, owner)
Extends the TTL of an existing lock.Lock identifier
New time-to-live in milliseconds
Lock owner identifier
Promise<boolean> - True if extended successfully
forceRelease(key)
Forces release of a lock regardless of owner.Lock identifier
Cache Provider Methods
get(key)
Retrieves a cached value.Cache key
Promise<T | null> - Cached value or null if not found
set(key, value, ttl?)
Stores a value in the cache.Cache key
Value to cache (must be JSON-serializable)
Time-to-live in milliseconds (optional, no expiration if omitted)
delete(key)
Removes a cached value.Cache key
clear()
Clears all cached values (respects key prefix if set).keys(pattern?)
Lists all cache keys matching an optional pattern.Optional glob pattern (e.g.,
'user:*')Promise<string[]> - Array of matching keys
Checkpoint Provider Methods
saveCheckpoint(checkpoint)
Saves a checkpoint for task recovery.Checkpoint data including taskId, sequence, progress, and data
loadLatestCheckpoint(taskId)
Loads the most recent checkpoint for a task.Task identifier
Promise<Checkpoint<T> | undefined> - Latest checkpoint or undefined
Connection Example
Key Storage Patterns
The adapter uses the following Redis key patterns:- Locks:
{prefix}lock:{key} - Circuit Breaker:
{prefix}circuit:{key} - Cache:
{prefix}cache:{key} - Checkpoints:
{prefix}checkpoint:{taskId}:{sequence}
Best Practices
The Redis adapter uses native Redis expiration (SETEX, PEXPIRE) for automatic cleanup. No manual cleanup required.
Related
PostgreSQL Adapter
PostgreSQL-based persistence
MongoDB Adapter
MongoDB-based persistence