Skip to main content
The Redis state adapter provides persistent subscriptions and distributed locking using the official redis package. Recommended for production deployments.

Installation

Basic Setup

Configuration

createRedisState(options)

Creates a Redis state adapter instance.

url

Required. Redis connection URL in format:

keyPrefix

Optional prefix for all Redis keys. Useful for:
  • Sharing Redis instance across multiple bots
  • Separating staging/production environments
  • Namespacing in multi-tenant scenarios

logger

Optional logger for error reporting. Defaults to console logger.

Environment Variables

REDIS_URL

The createRedisState() helper automatically reads REDIS_URL if not provided:
If REDIS_URL is not set and no url is provided, createRedisState() throws an error.

Redis Key Structure

The adapter uses the following key patterns:

Example Redis Commands

Connection Management

The adapter automatically connects when the Chat instance initializes. You typically don’t need to manage connections manually.

Manual Connection

Connection Errors

Connection errors are logged but don’t crash your application:
The redis package automatically reconnects on network failures. Your bot will resume normal operation once Redis is reachable.

Thread Subscriptions

Subscriptions are stored in a Redis Set for efficient membership checks.

Checking Subscription Status

Distributed Locking

Locks prevent concurrent message processing across serverless instances or webhook retries.

How Locks Work

Lock Implementation

Locks use Redis SET NX PX for atomic acquisition:
Properties:
  • NX - Only set if key doesn’t exist (atomic check-and-set)
  • PX - Expire after TTL milliseconds (auto-cleanup)
  • Token - Unique token ensures only lock holder can release

Extending Locks

For long-running operations:
Locks auto-expire after TTL. Always set a TTL longer than your expected processing time, and use extendLock() for operations that might exceed it.

Caching

The cache API stores JSON-serialized values with optional TTL.

Serialization

Values are automatically JSON-serialized:
Non-JSON values (functions, symbols) will be lost during serialization. Use plain objects and primitives.

Advanced Usage

Accessing the Redis Client

For advanced Redis operations:

Custom Key Prefix

Separate environments using different prefixes:

Deployment Examples

Monitoring

Key Metrics to Track

Health Check

Troubleshooting

The REDIS_URL environment variable is not set.Solution:
Or provide it explicitly:
You’re calling state methods before connect() is called.Solution: The Chat SDK calls connect() automatically. If using the adapter directly:
Your message handlers are taking longer than the default 30s lock TTL.Solution: Use extendLock() to refresh the TTL during long operations, or increase the initial TTL in your custom locking logic.
This is normal with MemoryStateAdapter. With RedisStateAdapter, subscriptions persist.Solution: Verify you’re using @chat-adapter/state-redis (not @chat-adapter/state-memory):

Migration from Memory Adapter

1

Install Redis adapter

2

Set up Redis

Use a hosted Redis provider (Vercel KV, Upstash, Railway) or run locally:
3

Update configuration

4

Set environment variable

5

Re-subscribe threads

Existing subscriptions from memory are lost. Users will need to @-mention your bot again to re-subscribe.

Next Steps

ioredis Adapter

Use ioredis for Cluster or Sentinel

State Overview

Learn about subscriptions and locking

Thread API

Explore thread.subscribe() and state methods

Deployment

Production deployment guides