Skip to content

remix/session-storage-redis

The remix/session-storage-redis package provides a session storage backend that stores session data in Redis. Redis is an in-memory data store that supports automatic key expiration, making it well-suited for session management in production applications with multiple servers.

Installation

Install the package along with a Redis client:

bash
npm install remix/session-storage-redis redis

createRedisSessionStorage(options)

Creates a SessionStorage backend that reads and writes session data to Redis.

ts
function createRedisSessionStorage(options: RedisSessionStorageOptions): SessionStorage

Options:

OptionTypeDefaultDescription
urlstring--Redis connection URL (e.g. 'redis://localhost:6379'). Required if client is not provided.
clientRedisClient--An existing Redis client instance. Use this if you want to share a client across your application. Required if url is not provided.
ttlnumber604800Session time-to-live in seconds. Sessions are automatically deleted from Redis after this duration. Defaults to 7 days.
prefixstring'session:'Key prefix for session entries in Redis. Useful for namespacing when sharing a Redis instance across multiple applications.

Redis Client Setup

Using a connection URL

The simplest setup passes a Redis URL. The storage backend creates and manages its own client:

ts
import { createRedisSessionStorage } from 'remix/session-storage-redis'

let sessionStorage = createRedisSessionStorage({
  url: process.env.REDIS_URL!,
})

Common Redis URL formats:

redis://localhost:6379
redis://:password@redis.example.com:6379
rediss://user:password@redis.example.com:6380  (TLS)

Using an existing client

If you already have a Redis client (e.g. for caching), you can share it:

ts
import { createClient } from 'redis'
import { createRedisSessionStorage } from 'remix/session-storage-redis'

let redisClient = createClient({ url: process.env.REDIS_URL! })
await redisClient.connect()

let sessionStorage = createRedisSessionStorage({
  client: redisClient,
})

Configuration

TTL (Time-to-Live)

The ttl option controls how long session data persists in Redis. After the TTL expires, Redis automatically deletes the key.

ts
let sessionStorage = createRedisSessionStorage({
  url: process.env.REDIS_URL!,
  ttl: 60 * 60 * 24 * 30, // 30 days
})

The TTL is refreshed every time the session is saved (i.e. on every request that uses the session), so active sessions do not expire.

Match TTL to cookie maxAge

Set the Redis TTL to match (or slightly exceed) the maxAge of your session cookie. This ensures that sessions are not kept in Redis longer than the cookie is valid.

Key Prefix

The prefix option adds a string before every session key in Redis. This is useful when multiple applications share the same Redis instance:

ts
let sessionStorage = createRedisSessionStorage({
  url: process.env.REDIS_URL!,
  prefix: 'myapp:session:',
})

// Sessions are stored as: myapp:session:a1b2c3d4e5f6

Complete Example

ts
// app/session.ts
import { createCookie } from 'remix/cookie'
import { createRedisSessionStorage } from 'remix/session-storage-redis'

export let sessionCookie = createCookie('__session', {
  httpOnly: true,
  secure: process.env.NODE_ENV === 'production',
  sameSite: 'Lax',
  secrets: [process.env.SESSION_SECRET!],
  maxAge: 60 * 60 * 24 * 7, // 1 week
})

export let sessionStorage = createRedisSessionStorage({
  url: process.env.REDIS_URL!,
  ttl: 60 * 60 * 24 * 7, // Match cookie maxAge
  prefix: 'session:',
})
ts
// app/server.ts
import { createRouter } from 'remix/fetch-router'
import { session } from 'remix/session-middleware'
import { sessionCookie, sessionStorage } from './session.ts'

let router = createRouter({
  middleware: [
    session(sessionCookie, sessionStorage),
  ],
})

Released under the MIT License.