remix/session-storage-memcache
The remix/session-storage-memcache package provides a session storage backend that stores session data in Memcache (Memcached). Memcache is a distributed in-memory caching system that works well for session storage in multi-server deployments.
Installation
Install the package along with a Memcache client:
npm install remix/session-storage-memcache memcachedcreateMemcacheSessionStorage(options)
Creates a SessionStorage backend that reads and writes session data to Memcache.
function createMemcacheSessionStorage(options: MemcacheSessionStorageOptions): SessionStorageOptions:
| Option | Type | Default | Description |
|---|---|---|---|
servers | string[] | -- | Array of Memcache server addresses (e.g. ['localhost:11211']). Required if client is not provided. |
client | MemcacheClient | -- | An existing Memcache client instance. Use this if you want to share a client across your application. Required if servers is not provided. |
ttl | number | 604800 | Session time-to-live in seconds. Sessions are automatically evicted after this duration. Defaults to 7 days. |
prefix | string | 'session:' | Key prefix for session entries. Useful for namespacing when sharing a Memcache instance across multiple applications. |
Memcache Client Setup
Using server addresses
The simplest setup passes an array of Memcache server addresses:
import { createMemcacheSessionStorage } from 'remix/session-storage-memcache'
let sessionStorage = createMemcacheSessionStorage({
servers: [process.env.MEMCACHE_URL!],
})For multiple servers (consistent hashing distributes keys across them):
let sessionStorage = createMemcacheSessionStorage({
servers: [
'memcache-1.example.com:11211',
'memcache-2.example.com:11211',
'memcache-3.example.com:11211',
],
})Using an existing client
If you already have a Memcache client, you can share it:
import Memcached from 'memcached'
import { createMemcacheSessionStorage } from 'remix/session-storage-memcache'
let memcacheClient = new Memcached('localhost:11211')
let sessionStorage = createMemcacheSessionStorage({
client: memcacheClient,
})Configuration
TTL (Time-to-Live)
The ttl option controls how long session data persists in Memcache. After the TTL expires, the entry is evicted.
let sessionStorage = createMemcacheSessionStorage({
servers: [process.env.MEMCACHE_URL!],
ttl: 60 * 60 * 24 * 30, // 30 days
})The TTL is refreshed on every save (i.e. on every request that uses the session), so active sessions do not expire.
Match TTL to cookie maxAge
Set the Memcache TTL to match (or slightly exceed) the maxAge of your session cookie. This ensures sessions are not kept in Memcache longer than the cookie is valid.
Key Prefix
The prefix option adds a string before every session key:
let sessionStorage = createMemcacheSessionStorage({
servers: [process.env.MEMCACHE_URL!],
prefix: 'myapp:sess:',
})
// Sessions are stored as: myapp:sess:a1b2c3d4e5f6Complete Example
// app/session.ts
import { createCookie } from 'remix/cookie'
import { createMemcacheSessionStorage } from 'remix/session-storage-memcache'
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 = createMemcacheSessionStorage({
servers: [process.env.MEMCACHE_URL!],
ttl: 60 * 60 * 24 * 7, // Match cookie maxAge
prefix: 'session:',
})// 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),
],
})Related
- remix/session -- Session class API and built-in storage backends
- remix/session-middleware -- Middleware that integrates sessions with the router
- remix/session-storage-redis -- Redis storage alternative
- Sessions & Cookies -- Conceptual guide