Why is express-session's default MemoryStore unsuitable for a production Express app, and what does its startup warning mean?
answer
- where the data lives, not the cookie
- one process, one heap
- removed only when read again
- NODE_ENV gates the warning
basics
~20 sWithout a store option, express-session keeps every session in a MemoryStore inside one Node process: abandoned sessions pile up in the heap, other processes cannot see them, and a restart wipes them, signing every user out.
solid answer
~50 sexpress-session puts only a signed session ID in the cookie; the data lives in whatever `store` you pass, and the default is a `MemoryStore`, a plain object in the process heap. It **leaks**: an expired record is deleted only when something reads it again, and with no `cookie.maxAge` a record never expires at all, so every visitor who never returns stays in memory. It is **single-process**: a second instance or cluster worker has its own empty map, so a request routed there is anonymous. And it is **volatile**: a restart or deploy empties it and every cookie points at nothing. The README says it is purposely not designed for production, and the package prints `MemoryStore is not designed for a production environment` only when `NODE_ENV` is `production`. The fix is a real store adapter such as connect-redis's `RedisStore`.
code
javascript · 11 linesconst express = require('express')
const session = require('express-session')
const app = express()
// No store option: express-session creates a MemoryStore in this process.
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}))go deeper
Recall that express-session defaults to an in-process MemoryStore, and name its three problems: unbounded growth, no sharing between processes, loss on restart.
Explain why it leaks: records expire only by maxAge, the default has none, and expired entries go only when read again, so abandoned sessions stay forever.
Connect it to incidents: logouts on every deploy, random logouts across instances, and a silent start when NODE_ENV is unset because the warning never prints.
Treat the default store as a configuration you must forbid in production, for example by failing start-up when no external store is configured.
## What express-session keeps, and where **express-session** is the session middleware most Express apps mount. It splits a session in two: - The **cookie** (named `connect.sid` unless you set `name`) carries only the **session ID**, signed with an HMAC of your `secret`. - The **session data** — whatever handlers put on `req.session` — lives server-side in a **store**, an object that implements `get`, `set` and `destroy` (and, ideally, `touch`). The `store` option is optional. Leave it out and the middleware creates `new MemoryStore()`. A **MemoryStore** is a plain JavaScript object in the Node process's heap: the key is the session ID, the value is the session serialised as a JSON string. Nothing is written anywhere else. The README is blunt about it: MemoryStore is *purposely* not designed for production, will leak memory under most conditions, does not scale past a single process, and is meant for debugging and developing. ## Why it leaks A record's life in a MemoryStore runs like this: 1. **Creation.** A session is saved when a handler modifies it — or, with `saveUninitialized: true`, whenever a request arrives without a valid cookie. That includes health checks, crawlers and scripts that never send a cookie back, so each of their requests adds a record. 2. **Expiry.** The record's expiry comes from `cookie.maxAge` (or `cookie.expires`). The default cookie block has `maxAge: null`, so by default a record carries no expiry at all and is never considered expired. 3. **Removal.** MemoryStore has **no timer** that sweeps expired entries. It deletes an expired record only when that record is read again (`get`, `touch`, or a listing through `all` or `length`), or when the app destroys the session or clears the store. A user who logs in once and never comes back is never read again, so the record stays until the process exits. Memory therefore grows with the number of distinct visitors, not with the number of active ones. That is the "leak" the warning means: not a bug in a loop, but a store with no eviction. ## Why it does not survive a deploy or a second process Because the data lives in one heap, two production facts break it: - **Restarts.** A deploy, a crash or an orchestrator rescheduling the container starts a new process with an empty map. Every browser still sends its valid, correctly signed cookie, express-session finds no record for that ID and starts a fresh, empty session — the user is signed out. This is the classic "everyone is logged out after every deploy". - **More than one process.** With several instances behind a load balancer, or Node's cluster workers, each process has its own MemoryStore. A user who logged in on one process and whose next request lands on another is anonymous there. Pinning users to one instance at the balancer is a separate topic and only hides the problem until that instance restarts. | Property | MemoryStore (default) | External store adapter (for example `RedisStore`) | |---|---|---| | Where data lives | the Node process's heap | a server other processes reach over the network | | Survives a restart or deploy | no | yes, while that server keeps the data | | Shared across instances and workers | no | yes | | Removes expired records | only when read again | by the store's own expiry, such as a key TTL | ## The warning, and why teams miss it When the middleware is created with a MemoryStore, express-session checks `NODE_ENV` (read once when the package is loaded). Only if it equals `production` does it call `console.warn` with: ``` Warning: connect.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process. ``` Three things follow from how that check is written: - It is **a warning, not an error**. The app starts and serves traffic normally. - It is printed **once**, at start-up, so it scrolls away in a busy log. - If `NODE_ENV` is unset — common in a container whose image never sets it — **nothing is printed at all**, and the app runs on MemoryStore with no hint. ## What to use instead Pass a store adapter in `store`. express-session's README lists dozens of compatible stores; for Redis, **connect-redis** exports a `RedisStore` class that takes a connected client. With a shared store, a restarted or second process reads the same records, and the store's own expiry removes old ones. Which kind of store to pick, and what happens when that store is down, are questions for the framework-neutral session-store material; on this leaf the point is that the default is a development convenience. MemoryStore stays perfectly reasonable for local development and for in-process tests, where losing sessions on restart is exactly what you want.
- Does setting cookie.maxAge stop the MemoryStore leak?Only partly. With `cookie.maxAge` set, each record carries an expiry and MemoryStore deletes an expired record when it is read again. It still has no background sweep, so a session that nobody ever requests again stays in the heap until the process exits. The growth slows for returning users but remains for one-off visitors, and restarts still sign everyone out.
- Is MemoryStore ever the right choice?Yes, for local development and in-process tests, which is what the README says it is for. There, a store that needs no server and is emptied on every restart is a feature: each test run starts clean. The mistake is shipping that same configuration to an environment with restarts, several instances or real traffic.
A MemoryStore is a cloakroom where the attendant keeps every coat in their own pockets: tickets nobody redeems pile up, a second attendant at another door has none of the coats, and when the attendant goes home every ticket points at nothing.
- MemoryStore is fine in production as long as only one instance runs.
- express-session stores the whole session inside the cookie.
- MemoryStore evicts expired sessions on a background timer.
- If NODE_ENV is unset, express-session refuses to start without a store.
- MemoryStore writes sessions to disk, so a restart keeps users signed in.