session-storage-redis Overview
The remix/session-storage-redis package stores session data in Redis, an in-memory data store. Redis is the most popular choice for production session storage because it is fast, supports automatic key expiration, and works across multiple servers.
Key Concepts
- Redis -- An in-memory key-value store that persists data with optional time-to-live (TTL). It runs as a separate process (or managed service) and is accessed over the network.
- TTL (Time-to-Live) -- How long a session stays in Redis before it is automatically deleted. Active sessions have their TTL refreshed on every request.
- Key prefix -- A string prepended to every session key in Redis (e.g.
session:abc123). Useful for namespacing when multiple applications share a Redis instance.
Why Redis for Sessions?
| Concern | Redis |
|---|---|
| Multi-server | Sessions are stored centrally, so any server can read any session |
| Speed | In-memory reads, typically sub-millisecond |
| Expiration | Built-in TTL support -- expired sessions are cleaned up automatically |
| Persistence | Optional disk persistence if the Redis process restarts |
| Scalability | Supports clustering for large-scale deployments |
Quick Example
ts
// app/session.ts
import { createCookie } from 'remix/cookie'
import { createRedisSessionStorage } from 'remix/session-storage-redis'
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
})
let sessionStorage = createRedisSessionStorage({
url: process.env.REDIS_URL!, // e.g. 'redis://localhost:6379'
ttl: 60 * 60 * 24 * 7, // Match cookie maxAge
prefix: 'session:', // Key prefix in Redis
})ts
// app/server.ts
import { createRouter } from 'remix/fetch-router'
import { session } from 'remix/session-middleware'
let router = createRouter({
middleware: [
session(sessionCookie, sessionStorage),
],
})No changes to your handlers are needed. context.get(Session) works exactly the same regardless of the storage backend.
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
url | string | -- | Redis connection URL. Required if client is not provided. |
client | RedisClient | -- | An existing Redis client. Required if url is not provided. |
ttl | number | 604800 (7 days) | Session lifetime in seconds. Refreshed on every request. |
prefix | string | 'session:' | Key prefix for session entries in Redis. |
Match TTL to cookie maxAge
Set the Redis ttl to match (or slightly exceed) the maxAge of your session cookie. This ensures sessions are cleaned up from Redis when the cookie expires.
When to Use This Package
Use Redis session storage when:
- You deploy to multiple servers or containers (load balancers distribute requests across servers, so sessions must be shared)
- You use serverless or container platforms where the filesystem is ephemeral
- You need sessions to survive server restarts
- You want automatic expiration without manual cleanup
For development or testing, cookie storage or memory storage is simpler. See the session overview for a comparison of all storage backends.
Next Steps
- Tutorial: Set Up Redis Session Storage -- Step-by-step setup and testing.
- API Reference -- Full documentation of
createRedisSessionStorage.