Skip to content

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:

ConcernMemcacheRedis
Data modelSimple key-value onlyRich data types (strings, hashes, lists, sets)
PersistenceNo disk persistenceOptional disk persistence
ClusteringBuilt-in via consistent hashingRequires Redis Cluster or Sentinel
Memory managementLRU eviction when memory is fullConfigurable eviction policies
Best forPure caching, simple session storageSessions + 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

OptionTypeDefaultDescription
serversstring[]--Memcache server addresses. Required if client is not provided.
clientMemcacheClient--An existing Memcache client. Required if servers is not provided.
ttlnumber604800 (7 days)Session lifetime in seconds. Refreshed on every request.
prefixstring'session:'Key prefix for session entries.

Next Steps

Released under the MIT License.