Skip to main content

createMemorySessionStorage

Creates a simple in-memory SessionStorage object. This is primarily useful for testing and as a reference implementation. Not suitable for production use beyond a single process.

Signature

MemorySessionStorageOptions
Configuration options for memory session storage.
The cookie used to store the session ID on the client, or options to create one automatically.
string
default:"__session"
The name of the session ID cookie.
string[]
Array of secrets for signing the session ID cookie.
number
Maximum age of the session in seconds.
boolean
default:"true"
Makes cookie inaccessible to JavaScript.
boolean
Only send cookie over HTTPS.
'lax' | 'strict' | 'none'
default:"lax"
Controls cross-site request behavior.

Returns

object
A session storage object with methods to manage sessions.
function
Parses the session ID from the Cookie header and returns the session.
function
Stores session data in memory and returns the Set-Cookie header with the session ID.
function
Deletes session data from memory and returns a Set-Cookie header that clears the cookie.

Basic Example

filename=app/sessions.server.ts

Testing Example

Use in tests to avoid external dependencies:
filename=app/routes/__tests__/auth.test.ts

Development Example

Simplify local development without setting up a database:
filename=app/sessions.server.ts

Integration Testing

filename=tests/integration/auth.test.ts

Session Expiration

Memory sessions support expiration:

TypeScript Support

Advantages

  • Zero dependencies - No database or external services required
  • Fast - In-memory operations are extremely fast
  • Simple setup - No configuration needed
  • Perfect for testing - Isolated sessions per test
  • Development friendly - Quick iteration without infrastructure

Limitations

Not Suitable for Production

Memory storage has several critical limitations for production use: Single Process Only
No Load Balancing Support Sessions are stored in a single process’s memory and won’t work across multiple servers:
Lost on Restart All sessions are lost when the process restarts:

Memory Leaks

Expired sessions remain in memory until explicitly cleaned:

When to Use

Testing

Perfect for unit and integration tests:

Local Development

Simplify development environment:

Prototyping

Quickly prototype features without infrastructure:

Single-User Applications

Acceptable for truly single-user scenarios:

Migration to Production Storage

When ready for production, migrate to persistent storage:
filename=app/sessions.server.ts