session-storage-memcache Overview
The remix/session-storage-memcache package stores session data in Memcache (Memcached), a distributed in-memory caching system. Like Redis, Memcache stores data in memory for fast access and supports automatic expiration, making it suitable for session storage in multi-server deployments.
Key Concepts
- Memcache (Memcached) -- A distributed in-memory key-value cache. It runs as one or more server processes and is accessed over the network. When you have multiple Memcache servers, keys are distributed across them using consistent hashing.
- TTL (Time-to-Live) -- How long a session entry stays in Memcache before it is evicted. Active sessions have their TTL refreshed on every request.
- Key prefix -- A string prepended to every session key (e.g.
session:abc123). Useful for namespacing when sharing a Memcache instance.
Memcache vs Redis
Both are valid choices for production session storage. Here is how they differ:
| Concern | Memcache | Redis |
|---|---|---|
| Data model | Simple key-value only | Rich data types (strings, hashes, lists, sets) |
| Persistence | No disk persistence | Optional disk persistence |
| Clustering | Built-in via consistent hashing | Requires Redis Cluster or Sentinel |
| Memory management | LRU eviction when memory is full | Configurable eviction policies |
| Best for | Pure caching, simple session storage | Sessions + other use cases (queues, pub/sub) |
Choose Memcache if you already use it in your infrastructure or prefer its simplicity. Choose Redis if you want disk persistence or plan to use the same store for other purposes.
Quick Example
ts
// app/session.ts
import { createCookie } from 'remix/cookie'
import { createMemcacheSessionStorage } from 'remix/session-storage-memcache'
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 = createMemcacheSessionStorage({
servers: [process.env.MEMCACHE_URL!], // e.g. 'localhost:11211'
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'
let router = createRouter({
middleware: [
session(sessionCookie, sessionStorage),
],
})Handlers use context.get(Session) exactly the same way as with any other storage backend.
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
servers | string[] | -- | Memcache server addresses. Required if client is not provided. |
client | MemcacheClient | -- | An existing Memcache client. Required if servers is not provided. |
ttl | number | 604800 (7 days) | Session lifetime in seconds. Refreshed on every request. |
prefix | string | 'session:' | Key prefix for session entries. |
Next Steps
- Tutorial: Set Up Memcache Session Storage -- Step-by-step setup.
- API Reference -- Full documentation of
createMemcacheSessionStorage.