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
ThecreateRedisState() helper automatically reads REDIS_URL if not provided:
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 RedisSET NX PX for atomic acquisition:
- 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: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
Error: Redis url is required
Error: Redis url is required
The Or provide it explicitly:
REDIS_URL environment variable is not set.Solution:Error: RedisStateAdapter is not connected
Error: RedisStateAdapter is not connected
You’re calling state methods before
connect() is called.Solution:
The Chat SDK calls connect() automatically. If using the adapter directly:Locks are expiring too quickly
Locks are expiring too quickly
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.Subscriptions lost after restart
Subscriptions lost after restart
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