In express-session, what do the resave and saveUninitialized options control, and what should a login-only Express app set them to?
answer
- two kinds of write-back
- unchanged session vs brand-new session
- defaults are deprecated
- store.touch makes one safe
basics
~20 sIn express-session, resave writes an unchanged session back to the store on every request and saveUninitialized stores a new, untouched session and sets its cookie. Both default to true with a deprecation; a login app sets both to false.
solid answer
~50 s`resave: true` forces express-session to save the session back to the store at the end of every request, even if nothing changed. That costs a write per request and opens a race: two parallel requests load the same session, one changes it, and the other's unchanged save can land last and overwrite it. With `resave: false` an unmodified session is not re-saved; if the store implements `touch` (connect-redis does), express-session calls it to refresh the record's expiry instead. `saveUninitialized: true` stores a session that is new but never modified and sets its cookie, so every cookieless request creates a record; `false` stores nothing and sends no cookie until a handler writes to `req.session`, such as at login. Both default to `true`, and leaving either undefined logs a deprecation. For login sessions on a store with `touch`, set both to `false`.
code
javascript · 6 linesapp.use(session({
store: redisStore, // implements touch()
secret: process.env.SESSION_SECRET,
resave: false, // no write-back of unchanged sessions
saveUninitialized: false // no record or cookie until login writes req.session
}))go deeper
Know what each option means in one sentence and that a login app sets both to false explicitly.
Explain the mechanics: resave covers loaded unchanged sessions, saveUninitialized covers new unchanged ones, and touch keeps expiry alive when resave is false.
Show the production cost of the defaults: a store write per request, lost updates between parallel requests, and a record for every cookieless health check.
Decide which store contract a team requires, touch included, so resave: false can be the enforced house default.
## Two different write-backs At the end of each request, **express-session** decides whether to write `req.session` back to its **store** (the server-side object holding session data, such as a `MemoryStore` or connect-redis's `RedisStore`). Two options steer that decision, and they cover different sessions: - **`resave`** — about a session that was **loaded** from the store (the browser sent a valid cookie) and was **not modified** during the request. - **`saveUninitialized`** — about a session that is **new** (no valid cookie came in) and was **not modified**. The README calls such a session *uninitialized*. A session that a handler did modify is saved either way. To tell the two apart, express-session hashes the session when it is created or loaded and compares that hash with the session at the end of the request: any change to its serialisable contents, or a new session ID after `regenerate`, counts as a modification, while merely reading values does not. ## resave: rewriting an unchanged session With `resave: true`, express-session saves every loaded session at the end of every request, changed or not. Two costs follow: 1. **A store write per request**, even for read-only page views. 2. **A lost-update race.** Two requests from the same browser run in parallel and both load the session. Request A adds an item to `req.session.cart`; request B changes nothing. If B finishes last, its unchanged copy is saved over A's write and the cart change is gone. The README warns about exactly this, noting that it also depends on the store. With `resave: false`, an unmodified loaded session is not saved. That raises a question: if nothing is written, what keeps the store from expiring an active session? The answer is the store's optional **`touch(sid, session, callback)`** method. When the store implements it, express-session calls `touch` for an unmodified loaded session, and the store refreshes the record's expiry without saving the request's copy of the data. connect-redis implements `touch` as a Redis `EXPIRE` on the key. The README's rule of thumb: - the store implements `touch` → `resave: false` is safe; - the store has no `touch` but expires records → you probably need `resave: true`. ## saveUninitialized: storing a session nobody used With `saveUninitialized: true`, a request without a cookie gets a new session that is saved to the store and sent as a `Set-Cookie` even if no handler touched it. Every health check, crawler and first page view therefore creates a record. With `saveUninitialized: false`, such a session is neither saved nor given a cookie. The first write to `req.session` — typically the login handler storing the user ID — is what creates both. The README lists three reasons to choose `false`: - **login sessions**: there is nothing worth keeping before sign-in; - **storage**: no records for anonymous traffic; - **consent**: no cookie until the app has a reason to set one, which helps where the law requires permission first. It also reduces races between parallel first requests from a browser that has no session yet. | Option | Applies to | `true` does | `false` does | |---|---|---|---| | `resave` | loaded, unmodified session | saves it again every request | skips the save; calls `store.touch` if present | | `saveUninitialized` | new, unmodified session | saves it and sets the cookie | saves nothing and sends no cookie | ## The defaults, and the deprecation Both options default to `true`, and both defaults are **deprecated**: the README says they will change and asks you to choose. Leaving either undefined makes express-session log, through its deprecation helper: - `express-session deprecated undefined resave option; provide resave option` - `express-session deprecated undefined saveUninitialized option; provide saveUninitialized option` The app still runs with `true` for both, so a team that ignores the message gets the write-per-request and record-per-visitor behaviour. ## What a login app sets For an Express app whose sessions exist to hold a signed-in user, on a store that implements `touch`: - `resave: false` — no rewrites of unchanged sessions, no lost updates from parallel requests, expiry kept alive by `touch`; - `saveUninitialized: false` — no record and no cookie until login writes to `req.session`. connect-redis's own setup example uses exactly these two values. How the framework-neutral decision to save a session works in general is covered with web-framework session objects; here the point is the option names, their defaults and the store contract that makes `resave: false` safe.
- With saveUninitialized: false, why does req.sessionID change on every request from an anonymous visitor?Because express-session sends no cookie for a session nobody modified, each request without a cookie generates a new session with a new ID. Code that keys anything on `req.sessionID` before login therefore sees a different value every time. Writing something to `req.session` first makes the session saved, sets the cookie and keeps the ID stable from then on.
- How do you tell whether a given store makes resave: false safe?Check whether it implements the store's `touch` method. express-session calls `touch` for a loaded, unmodified session when `resave` is `false`, so a store with `touch` keeps active sessions from expiring. A store without `touch` that sets expiry on records may drop active users whose sessions never change, and then `resave: true` is the README's suggestion.
- resave: false means express-session never saves the session, even after changes.
- saveUninitialized: false stops users from logging in at all.
- The defaults are false, so leaving both options out is the safe choice.
- resave: true is harmless because it only rewrites identical data.
- With resave: false, nothing can keep an active but unchanged session from expiring.