Skip to content

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:

bash
npm install remix/session-storage-memcache memcached

createMemcacheSessionStorage(options)

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

ts
function createMemcacheSessionStorage(options: MemcacheSessionStorageOptions): SessionStorage

Options:

OptionTypeDefaultDescription
serversstring[]--Array of Memcache server addresses (e.g. ['localhost:11211']). Required if client is not provided.
clientMemcacheClient--An existing Memcache client instance. Use this if you want to share a client across your application. Required if servers is not provided.
ttlnumber604800Session time-to-live in seconds. Sessions are automatically evicted after this duration. Defaults to 7 days.
prefixstring'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:

ts
import { createMemcacheSessionStorage } from 'remix/session-storage-memcache'

let sessionStorage = createMemcacheSessionStorage({
  servers: [process.env.MEMCACHE_URL!],
})

For multiple servers (consistent hashing distributes keys across them):

ts
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:

ts
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.

ts
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:

ts
let sessionStorage = createMemcacheSessionStorage({
  servers: [process.env.MEMCACHE_URL!],
  prefix: 'myapp:sess:',
})

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

Complete Example

ts
// 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:',
})
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.