Redis

Production state adapter for Chat SDK using the official `redis` client, with persistent subscriptions, distributed locking, and a key-value cache.

Create a new bot

Scaffold a Next.js bot that stores state in Redis with create-chat-sdk. Replace slack with your platform adapter:

pnpm create chat-sdk@latest my-bot --adapter slack redis -y

The CLI adds @chat-adapter/state-redis, wires createRedisState() into src/lib/bot.ts, and lists REDIS_URL in .env.example. To add the adapter to an existing app, start at Install.

Install

pnpm add @chat-adapter/state-redis

Quick start

With no options, createRedisState() connects to the URL in REDIS_URL.

lib/bot.ts
import { Chat } from "chat";
import { createRedisState } from "@chat-adapter/state-redis";

const bot = new Chat({
  userName: "mybot",
  adapters: { /* ... */ },
  state: createRedisState(),
});

If you already use ioredis or need Redis Sentinel, see when to choose ioredis vs redis.

Vercel Marketplace

Upstash Redis and Redis Cloud are both available on the Vercel Marketplace. From your project directory, link the project and add an integration with the Vercel CLI. vercel integration add creates the database, connects it to the linked project, adds its credentials as environment variables, and pulls them into your local env file.

vercel link
vercel integration add upstash/upstash-kv

Upstash Redis adds REDIS_URL to your project, and createRedisState() reads it with no extra configuration. If your provider stores the connection URL under another name, pass that variable as url.

Configuration

Prop

Type

Either url, the REDIS_URL env var, or client is required.

Environment variables

VariableRequiredDescription
REDIS_URLUnless url or client is setRedis connection URL, used when url is not passed.

Using an existing client

Pass a client you created with the redis package as client. The adapter doesn't connect a client it didn't create, so call client.connect() yourself before the bot starts. The adapter's disconnect() leaves the client open.

lib/state.ts
import { createClient } from "redis";
import { createRedisState } from "@chat-adapter/state-redis";

const client = createClient({ url: "redis://localhost:6379" });
await client.connect();

export const state = createRedisState({ client });

Data model

Every key starts with keyPrefix, which defaults to "chat-sdk". Set your own prefix to keep the bot's keys apart from other data in the same Redis database:

createRedisState({
  url: process.env.REDIS_URL!,
  keyPrefix: "my-bot",
});
KeyRedis typeContents
{keyPrefix}:subscriptionsSetSubscribed thread IDs
{keyPrefix}:lock:{threadId}StringLock token, with a TTL
{keyPrefix}:cache:{key}StringJSON-serialized cache value, with an optional TTL
{keyPrefix}:list:{key}ListJSON-serialized list entries, with an optional TTL
{keyPrefix}:queue:{threadId}ListQueued messages for a thread, with a TTL

Production notes

  • Run Redis 6.0+ for best performance.
  • Enable persistence (RDB or AOF).
  • Set explicit memory limits.
  • For Redis Sentinel, use @chat-adapter/state-ioredis. This adapter accepts a single redis client. Neither adapter supports Redis Cluster.

For serverless deployments such as Vercel or AWS Lambda, use a serverless-compatible Redis provider. On Vercel, you can provision Upstash Redis or Redis Cloud from the Vercel Marketplace.

Feature support

Capabilities

FeatureSupported
Persistence
Multi-instance
Subscriptions
Distributed locking
Key-value cachingTTL
Lists
Queues
Automatic reconnect
Cluster support
Sentinel support
Key prefix namespacing

More adapters

On this page