Skip to main content
The ioredis state adapter provides persistent subscriptions and distributed locking using the ioredis package. Use this adapter when you need Redis Cluster, Sentinel, or want to reuse an existing ioredis client.

Installation

Basic Setup

Configuration

createIoRedisState(options)

Creates an ioredis state adapter instance.

url

Redis connection URL in format:

client

Pass an existing ioredis client for advanced configurations:
When providing your own client, the adapter will NOT call quit() on disconnect. You’re responsible for client lifecycle management.

keyPrefix

Optional prefix for all Redis keys:

logger

Optional logger for error reporting:

Redis Cluster

For high-availability Redis Cluster deployments:

Redis Sentinel

For Redis Sentinel (automatic failover):

Redis Key Structure

Identical to the Redis adapter:

Inspecting Keys

Connection Management

ioredis automatically connects on instantiation. The adapter tracks connection state internally.

Connection Events

The adapter listens for ioredis events:
ioredis automatically reconnects on network failures. Your bot will resume operation once Redis is reachable.

Thread Subscriptions

Subscriptions are stored in a Redis Set:

Distributed Locking

Locks use atomic SET NX PX operations:

Lock Token Format

Extending Locks

For operations exceeding the initial TTL:

Caching

Store JSON-serialized values with optional TTL:

Advanced Usage

Accessing the ioredis Client

Custom Retry Strategy

Connection Pooling

Deployment Examples

Monitoring

Connection Health

Metrics

Troubleshooting

ioredis is failing to connect to Redis.Solution:
  • Check REDIS_URL is correct
  • Verify network access (security groups, firewalls)
  • Ensure Redis is running: redis-cli ping
  • Check logs for connection errors
You’re writing to a read replica in Redis Cluster.Solution: Configure cluster with scaleReads:
When providing your own client, the adapter doesn’t call quit() on disconnect.Solution: Manually disconnect:
Locks expire automatically via TTL, but graceful release is better.Solution: Always use try/finally:

redis vs ioredis

Both adapters implement the same StateAdapter interface and can be swapped without code changes.

Migration from redis

1

Install ioredis adapter

2

Update imports

3

Update configuration

4

Test

Data is compatible. Keys use the same format, so existing subscriptions and cache persist.

Next Steps

Redis Adapter

Simple Redis setup for single instance

State Overview

Learn about subscriptions and locking

Deployment

Production deployment guides

ioredis Docs

Official ioredis documentation