- Thread subscriptions - Track which threads your bot is monitoring
- Distributed locks - Prevent concurrent message processing across instances
- Caching - Store temporary data with TTL support
StateAdapter interface, making it easy to swap implementations.
Available Adapters
Memory
Development & testing
Redis
Production (recommended)
ioredis
Production with Cluster/Sentinel
StateAdapter Interface
All state adapters implement this interface fromchat/src/types.ts:467-499:
Thread Subscriptions
Subscriptions persist which threads your bot is actively monitoring. When subscribed, all future messages in that thread triggeronSubscribedMessage handlers.
How It Works
- User @-mentions your bot → triggers
onNewMention - Handler calls
thread.subscribe()→ adds threadId to state - Future messages check
state.isSubscribed(threadId) - Subscribed messages trigger
onSubscribedMessagehandlers
Subscriptions persist across restarts (except with MemoryStateAdapter). Your bot automatically resumes monitoring threads after deployment.
Distributed Locking
Locks prevent concurrent processing of the same thread across multiple server instances or requests.Why Locking Matters
Without locks:- Webhook retries could trigger duplicate processing
- Multiple serverless instances could race on the same message
- Concurrent edits to thread state could corrupt data
Lock Properties
- TTL-based - Locks auto-expire to prevent deadlocks from crashed instances
- Token-based - Only the lock holder can release or extend it
- Atomic - Acquisition uses
SET NX EXfor race-free locking
Caching
The cache API provides generic key-value storage with optional TTL:Use Cases
- Rate limiting - Track API call counts per user
- User preferences - Cache settings between messages
- Temporary state - Store wizard/form progress
- Deduplication - Track processed message IDs
Chat SDK uses the cache internally for message deduplication (5-minute TTL by default, configurable via
dedupeTtlMs).Choosing an Adapter
Memory - Development & Testing
Memory - Development & Testing
Pros:
- Zero configuration
- No external dependencies
- Fast for local development
- State lost on restart
- Not shared across instances
- Console warning in production
Redis - Production (Recommended)
Redis - Production (Recommended)
Pros:
- Official redis package
- Simple connection string
- Wide hosting support (Vercel, Railway, Render)
- Automatic reconnection
- Requires Redis server
- No built-in clustering
ioredis - Advanced Production
ioredis - Advanced Production
Pros:
- Redis Cluster support
- Sentinel support for HA
- More configuration options
- Can reuse existing client
- Requires Redis server
- More complex setup
Connection Management
All adapters require explicit connection before use:Disconnection
For graceful shutdown:Error Handling
State adapters throw errors for:- Connection failures
- Operations before
connect()is called - Network timeouts (Redis adapters)
Redis adapters log connection errors but don’t throw during background operations. Monitor your logs for
"Redis client error" messages.Next Steps
Memory Adapter
Quick setup for development
Redis Adapter
Production deployment guide
ioredis Adapter
Cluster and Sentinel setup
Core Concepts
Learn about threads and subscriptions