Why do in-memory sessions break when you scale a monolith horizontally?

Most web frameworks default to keeping session data in the application process. When a user logs in, the server generates a session ID, stores it in a cookie, and keeps the matching data (user ID, CSRF token, cart contents) in a hash map in memory. Each request carries the cookie, and the process looks up the ID in its own memory. This is fast and needs no extra infrastructure. It works as long as exactly one process serves every request.

That stops being true when you put a second instance behind a load balancer. A round-robin balancer sends a user's login to instance A and their next request to instance B. Instance B has never seen that session ID, so it treats the user as logged out. Restarts cause the same failure even with a single instance. Every deploy, crash, or out-of-memory kill wipes the session map, and every active user has to log in again.

Sticky sessions (session affinity) are the usual first fix. The load balancer pins each client to one instance, typically with a cookie or a hash of the source IP. This hides the problem without solving it:

  • Uneven load. Long-lived or heavy users pile up on whichever instance they first landed on.
  • Autoscaling lags. New instances only receive new sessions, so adding capacity does little for existing traffic.
  • Deploys and failures still log users out. Draining or losing an instance destroys every session pinned to it.

This article is for teams running a monolith behind a load balancer who have reached this point.

Who should use a shared session store or JWTs?

Move sessions out of process memory if any of these apply:

  • You run, or plan to run, more than one instance of the application. This includes blue-green or rolling deploys, where old and new instances serve traffic at the same time.
  • Users complain about being logged out after deploys or restarts. That is the in-memory store losing its state.
  • You want to autoscale. New instances need to serve existing users as well as new ones.
  • You rely on sticky sessions and are hitting their limits: uneven load, slow drains, or logouts when an instance is replaced.

Redis sessions fit most teams in this position. They suit a single application that already uses framework-managed sessions, needs immediate logout and revocation, and can run, or already runs, a Redis instance.

JWTs fit a different situation. They make sense when several independently deployed services, or third-party APIs, must verify a caller's identity without sharing a session store. A shared store means the services have to share access to it, agree on a session format, and depend on its availability. JWTs remove that coupling.

When should you avoid Redis sessions or JWTs?

Avoid adding Redis for sessions when:

  • You run a single instance and can tolerate logouts on restart. In-memory sessions are simpler, and an extra dependency buys you little.
  • You already run a relational database with spare capacity and moderate traffic. Database-backed sessions give you the same shared, revocable state without a new system to operate.
  • Nobody on the team can own Redis availability, persistence and memory limits. An unmanaged Redis becomes a single point of failure for every login.

Avoid JWTs as the primary browser session mechanism when:

  • Everything is served by a single application. You take on key rotation, revocation workarounds and token-size limits to remove one lookup.
  • You need immediate logout, account lockout or permission changes. These are requirements in many regulated or high-risk applications. A JWT stays valid until it expires unless you add a denylist, and a denylist brings back the shared lookup.
  • You plan to put large or sensitive data in the token. The payload is readable by anyone who holds it, and it travels with every request.
  • You lack a secure place to store signing keys and a rehearsed rotation procedure. A leaked key lets an attacker mint tokens for any user.

How does it work, step by step?

Both approaches take session state out of a single process, so any instance can serve any request. They differ in where that state lives.

With a Redis session store:

  1. On login, the application generates a random session ID and writes the session data to Redis under that ID with a TTL.
  2. The application sends the ID to the browser in an HttpOnly, Secure cookie.
  3. On each request, whichever instance receives it reads the cookie, fetches the session from Redis, and optionally extends the TTL.
  4. On logout or revocation, the application deletes the key. The next request finds nothing, on every instance.

With JWTs:

  1. On login, an issuer signs a token containing claims such as the user ID (sub), expiry (exp), issuer and audience.
  2. The client sends the token with each request, in a cookie or an Authorization header.
  3. Each instance verifies the signature with the issuer's public key (or a shared secret), checks the expiry, issuer and audience, and only then trusts the claims.
  4. The token stays valid until it expires. Revocation relies on short lifetimes, refresh tokens held on the server, or a denylist.

The sections below go through each step in detail, with code.

How does a Redis session store work compared to JWT stateless sessions?

Redis: opaque ID, server-side state

The cookie holds only a random session ID with no meaning of its own. The session data (user ID, roles, CSRF token, cart) lives in Redis under a key derived from that ID, with a TTL so abandoned sessions expire without a cleanup job. Every request follows the same path:

Browser → cookie sid=abc123 → any app instance → GET session:abc123 in Redis → handler

Because any instance can read any session, the load balancer can route requests freely and you no longer need sticky sessions. The cost is a network round trip on every authenticated request, and Redis becomes a dependency your login path cannot run without. Writing the session sets the TTL in the same command, and you can extend it on activity to get sliding expiry.

JWT: signed claims, no lookup

The token carries the claims, such as subject, expiry and scopes, together with a signature. The path skips the store:

Browser → Authorization: Bearer <jwt> → any app instance → verify signature, exp, issuer and audience → handler

Verification must succeed before your code reads any claim. A decoded but unverified payload is attacker-controlled input. You also need to pin the accepted algorithm instead of trusting the token's own header.

"Stateless" applies only to the per-request check. The server still holds signing keys and has to rotate them. Most real deployments also keep refresh tokens in a database so that long-lived access can be cut off. That leftover state is the main subject of the next section.

import json
import secrets

import jwt    # PyJWT
import redis  # redis-py

r = redis.Redis(host="localhost", port=6379, decode_responses=True)
SESSION_TTL = 1800  # 30 minutes; idle sessions expire on their own

def create_session(user_id: str, roles: list[str]) -> str:
    sid = secrets.token_urlsafe(32)  # random, carries no meaning
    data = {"user_id": user_id, "roles": roles, "csrf": secrets.token_urlsafe(16)}
    # Value and TTL are set in one command, so the key never exists without an expiry
    r.set(f"session:{sid}", json.dumps(data), ex=SESSION_TTL)
    return sid  # send as: Set-Cookie: sid=<sid>; HttpOnly; Secure; SameSite=Lax

def load_session(sid: str) -> dict | None:
    raw = r.get(f"session:{sid}")  # one round trip on every request
    if raw is None:
        return None  # expired, unknown or deleted at logout
    r.expire(f"session:{sid}", SESSION_TTL)  # sliding expiry on activity
    return json.loads(raw)

PUBLIC_KEY = open("/etc/app/jwt-signing.pub").read()  # placeholder path

def verify_access_token(token: str) -> dict:
    # Raises jwt.InvalidTokenError on a bad signature, expiry, issuer or audience
    return jwt.decode(
        token,
        PUBLIC_KEY,
        algorithms=["RS256"],  # pinned; the token's own 'alg' header is not trusted
        issuer="",
        audience="orders-api",
        options={"require": ["exp", "iss", "aud", "sub"]},
    )
    # Only the dict returned here is safe to use. Never act on an unverified decode.

A session written to Redis with a TTL that slides on each read, next to a JWT verified with a pinned algorithm, issuer and audience before any of its claims are used.

How do revocation and logout differ between Redis sessions and JWTs?

With a Redis store, the session is the authority. Logout deletes the key, and the next request carrying that session ID finds nothing and is treated as anonymous. Forced revocation works the same way: an admin action or security job deletes the user's keys, and the effect is immediate on every instance. This requires a per-user index, such as a set of session IDs per user, so you can find all of a user's sessions without scanning the keyspace.

A JWT is the authority until it expires. The server checks the signature and the exp claim and does not ask anyone else. Clearing the cookie on logout only removes the client's copy. A stolen token keeps working. There are two ways to narrow that window. One is short-lived access tokens with server-side refresh tokens, which caps exposure at the access-token lifetime. The other is a denylist of token IDs checked on every request, which brings back the shared lookup that JWTs were meant to remove.

Embedded claims also go stale. If a role is revoked, a token that says role: admin keeps granting admin access until it is refreshed. During an incident, that means you cannot tell responders the attacker is locked out. You can only say the attacker will be locked out within N minutes.

Scenario Redis sessions JWTs
Logout Delete key; immediate Token valid until expiry unless denylisted
Admin revocation Delete all user keys Revoke refresh token; access token lingers
Role change Update session or re-read per request Stale until next refresh
Password reset Delete all sessions Revoke refresh tokens or bump a per-user version claim
Compromised account Immediate lockout Denylist, or rotate signing keys (logs out everyone)

Whichever model you choose, the credential usually travels in a cookie, so the cookie flags do most of the defensive work. HttpOnly keeps JavaScript from reading the value, which limits what an XSS bug can steal. Secure restricts the cookie to HTTPS. SameSite=Lax or Strict stops the browser from attaching it to most cross-site requests. A typical header looks like Set-Cookie: sid=8f3a...; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=3600.

The browser sends cookies automatically, so any cookie-based auth is exposed to CSRF. That includes a JWT stored in a cookie. SameSite reduces the risk but does not remove it. Older clients and same-site subdomains can still get through. Keep anti-CSRF tokens or origin checks on state-changing endpoints.

JWTs add risks that opaque session IDs don't have:

  • Key compromise. Anyone holding the signing key can mint valid tokens for any user until you rotate the key and reject tokens signed with the old one. Store keys in a secrets manager and identify them with a key ID (kid) so you can rotate them. Rehearse the rotation before you need it.
  • Algorithm confusion. Configure the verifier with the one algorithm you expect. Never accept whatever the token header declares, none included.
  • Unverified contents. Never read claims before the signature and expiry checks pass.

Keep both formats small. A JWT payload is base64-encoded, not encrypted, so anyone holding the token can read it. Bulky session records also inflate Redis memory and add latency to every request. Store identifiers in the token or session record, and load the sensitive data from your database when you need it.

import express from "express";
import jwt, { JwtHeader, SigningKeyCallback } from "jsonwebtoken";

const app = express();

// Public keys by key ID, loaded from your secrets manager at startup.
// To rotate: add the new kid, sign with it, then delete the old entry so tokens signed with it are rejected.
const publicKeys: Record<string, string> = {
  "2026-11-a": process.env.JWT_PUBLIC_KEY_2026_11_A ?? "",
};

// Choose the verification key from the header's kid. Only the signature check makes the token trusted.
function getKey(header: JwtHeader, cb: SigningKeyCallback) {
  const key = header.kid ? publicKeys[header.kid] : undefined;
  key ? cb(null, key) : cb(new Error("unknown or retired kid"));
}

app.post("/login", (_req, res) => {
  // ...after checking credentials, issue a small token that carries only identifiers.
  // Equivalent header: Set-Cookie: at=...; Path=/; Max-Age=3600; HttpOnly; Secure; SameSite=Lax
  res.cookie("at", "<signed-jwt>", {
    httpOnly: true,   // not readable from JavaScript, which limits what XSS can steal
    secure: true,     // sent over HTTPS only
    sameSite: "lax",  // not attached to most cross-site requests; still keep CSRF checks
    path: "/",
    maxAge: 3600 * 1000, // Express takes milliseconds
  });
  res.sendStatus(204);
});

export function verifyAccessToken(token: string): Promise<jwt.JwtPayload> {
  return new Promise((resolve, reject) => {
    // Pin the algorithm. A header declaring "none" or HS256 is rejected.
    jwt.verify(token, getKey, { algorithms: ["RS256"] }, (err, payload) => {
      // Read claims only after the signature and exp checks pass.
      if (err || !payload || typeof payload === "string") return reject(err ?? new Error("invalid token"));
      resolve(payload); // e.g. { sub: "user_123" }; load sensitive data from the database
    });
  });
}

An Express handler sets an HttpOnly, Secure, SameSite=Lax cookie, and a jsonwebtoken verifier looks up the signing key by kid so old keys can be retired, pins RS256, and exposes claims only after verification succeeds.

What does it take to run Redis as a production session store?

Treat Redis as tier-one infrastructure. Once sessions live there, a Redis failure becomes an authentication failure.

Availability. Run at least one replica with automated failover, using Sentinel or Redis Cluster. Replication is asynchronous, so a failover can lose the last few writes. In practice, a handful of users who just logged in get logged out again. That is usually acceptable, but decide it explicitly.

Persistence. Without persistence, every restart logs out every user. RDB snapshots lose everything written since the last snapshot. AOF with appendfsync everysec narrows the loss to roughly a second, at the cost of more disk I/O.

Memory and eviction. Set a hard limit and choose a policy deliberately, for example maxmemory 1gb and maxmemory-policy noeviction. Sessions carry TTLs, so volatile-lru and allkeys-lru will quietly delete live sessions under memory pressure. noeviction makes writes fail loudly instead, which you can alert on. Keep caches in a separate instance so they cannot crowd out sessions.

Illustrative estimate, not a measurement:

Assumed input Value
Active sessions 300,000
Average payload 800 bytes
Per-key overhead ~100 bytes
Raw total ~270 MB

Doubling that for fragmentation and replication buffers suggests a maxmemory near 600 MB.

Latency. Every authenticated request now makes a network round trip. Keep Redis in the same zone as the application, pool connections, and set tight client timeouts.

Outages. Fail fast so request threads don't pile up waiting on Redis. Keep serving public pages, and show a clear "login temporarily unavailable" message instead of a generic error.

maxmemory 600mb

maxmemory-policy noeviction

appendonly yes
appendfsync everysec

save 900 1

requirepass change-me-session-store-password

A minimal redis.conf for a dedicated session store that caps memory at 600 MB, rejects writes instead of evicting live sessions when full, and uses AOF with per-second fsync so a restart loses about a second of writes rather than logging everyone out.

How do you migrate sessions without downtime?

Run the migration as a period where both stores are live, rather than a single cutover. Keep sticky sessions enabled the whole time. They protect any user whose session still exists only in one instance's memory.

The middleware

Every instance runs the same logic on each request:

  1. Read the session ID from the existing cookie.
  2. Look it up in Redis. On a miss, check local memory.
  3. If memory has it, copy it into Redis with the remaining TTL, not a fresh one. A fresh TTL would quietly extend sessions.
  4. On any session write, update both Redis and memory.

Writing to both stores is what makes rollback safe. Turn the Redis path on with a flag, first for a small share of instances, then for all of them. Remove the memory path only after the longest session lifetime has passed with everything on Redis.

Failure modes behind intermittent logouts

  • Redis-only writes during a mixed rollout. A user logs in on a migrated instance, then gets routed to an unmigrated one, which finds no session. Dual writes and stickiness prevent this.
  • Changing the cookie name or ID format mid-rollout. Old instances can't read the new cookie. Keep the format unchanged until the migration is done, then rename in a separate step that accepts both names.
  • Serialization drift. Objects stored in memory may fail to deserialize from Redis. Test round-trips before rollout.

Track logout and login rates per instance. A step change on migrated instances points to one of these failure modes.

Moving to JWTs works differently. Old instances can't validate tokens, so issue JWTs only after every instance can verify them. For short-lived sessions, a one-time forced re-login during a low-traffic window is often simpler than dual-reading.

// Dual-read + write-through session middleware (pseudocode-style; stores are abstract).
interface Session { id: string; data: Record<string, unknown>; expiresAt: number }
interface Store {
  get(id: string): Promise<Session | null>;
  set(s: Session, ttlSeconds: number): Promise<void>;
}

const COOKIE = "sid"; // keep name/format unchanged until migration is finished
const redisOn = process.env.SESSION_REDIS === "on"; // per-instance rollout flag

// Carry the remaining lifetime over; a fresh TTL would silently extend sessions.
const remainingTtl = (s: Session) =>
  Math.max(0, Math.floor((s.expiresAt - Date.now()) / 1000));

export function sessionMiddleware(redis: Store, memory: Store) {
  return async (req: any, _res: any, next: () => void) => {
    const id = req.cookies?.[COOKIE];
    let session: Session | null = null;
    if (id) {
      // Redis first; a Redis error falls back to memory instead of logging the user out.
      if (redisOn) session = await redis.get(id).catch(() => null);
      if (!session) {
        session = await memory.get(id);
        // Backfill so the user can land on any migrated instance next time.
        if (session && redisOn && remainingTtl(session) > 0) {
          await redis.set(session, remainingTtl(session)).catch(() => {});
        }
      }
      if (session && remainingTtl(session) === 0) session = null; // expired
    }
    req.session = session;
    // Write-through: memory stays authoritative for rollback; Redis gets every write.
    req.saveSession = async (s: Session) => {
      const ttl = remainingTtl(s);
      if (ttl === 0) return;
      await memory.set(s, ttl);
      if (redisOn) await redis.set(s, ttl);
    };
    next();
  };
}

Middleware that reads sessions from Redis with a fallback to local memory, copies memory hits into Redis using their remaining TTL, and writes every session update to both stores behind a per-instance rollout flag.

How can it save engineering time?

Moving sessions to a shared store removes a class of recurring work, not just a scaling limit:

  • Deploys stop being user-visible events. Without session loss on restart, you can run rolling deploys during working hours. Releases no longer have to wait for a low-traffic window, and there is no follow-up to handle "I got logged out" reports.
  • Less load-balancer tuning. Once any instance can serve any request, you stop debugging affinity settings, uneven instance load and slow connection drains.
  • Autoscaling works without special cases. New instances take existing traffic right away, so capacity problems need fewer manual interventions.
  • Fewer intermittent bugs to chase. "Logged out at random" problems caused by routing are hard to reproduce. A shared store removes the cause rather than requiring per-incident investigation.
  • Incident response is faster with Redis sessions. Revoking a compromised account means deleting its keys. You don't have to rotate signing keys or build a denylist under pressure.
  • Little application code changes. With Redis, the framework's session API stays the same and only the storage backend changes. Most of the work is configuration and the migration middleware, not a redesign of authentication.

JWTs save time in a different place. When several services verify identity, each one checks a signature locally. No team has to coordinate access to a shared store or a session format. In a single monolith that saving doesn't apply, and key management and revocation workarounds usually cost more time than they save.

Illustrative estimate, using assumed inputs only. Assume a team deploys 10 times a month. Assume each deploy that logs users out costs about 1 engineer-hour in off-hours scheduling, monitoring and support follow-up. Assume affinity-related issues take another 4 engineer-hours a month to investigate. That is about 14 engineer-hours a month. Weigh that against the one-time migration effort and the ongoing cost of operating Redis. Replace these assumptions with your own figures before deciding.

When should you choose Redis, JWTs, or a hybrid?

For most monoliths, Redis is the lower-risk default. The application keeps its existing session API: you swap the storage backend, and the cookie still carries an opaque ID. Logout, forced sign-out and permission changes take effect on the next request, because the server still owns the state. You pay for a network hop per request and for one more system to run. Engineers rarely have to redesign anything, which is usually the larger saving.

JWTs make sense when several independently deployed services must verify identity and none of them can share a session lookup. They are a poor fit for a single application, where they add key rotation, revocation workarounds and token-size concerns to replace one lookup that Redis already handles in under a millisecond on a local network (illustrative estimate).

Many teams use a hybrid. The browser holds an opaque session cookie. The edge or gateway resolves it once and issues a short-lived signed token for internal calls. Revocation stays central, and downstream services stay stateless.

Two simpler alternatives also work. Sticky sessions defer the problem, but you lose sessions when an instance restarts and load spreads unevenly. Database-backed sessions reuse infrastructure you already run, which suits moderate traffic.

Option Revocation Latency per request Operational burden Implementation effort Best fit
Sticky sessions Immediate, lost on restart None added Low, load balancer config Minimal Monolith, short term
Database sessions Immediate One DB query Low, existing DB Low Monolith, moderate traffic
Redis sessions Immediate One cache lookup Moderate, HA and memory Low Monolith
JWTs Delayed until expiry Signature check only Key management High Multiple services
Hybrid Immediate at edge Lookup at edge only Moderate to high Medium to high Monolith becoming services

How OneEnv can help

Disclosure: OneEnv is the product behind this blog.

OneEnv (oneenv.io) is a workspace-based governance platform for the things services share — environment variables, database structure, API routes and validation rules. A change to a shared value is staged on a branch, reviewed once, and then every affected service explicitly approves or rejects it before it goes live, so you can see who a change touches before it ships instead of finding out in production.

Applied to session storage redis vs jwt, this is where it helps:

Saves time

  • Every service that inherits a shared value is listed in one place, so nobody searches repos and chats to find out who is affected.
  • One review per branch replaces coordinating each team separately; each service still keeps its own approve/reject.
  • Reviewers see the exact diff and the list of affected services up front, so approval is a decision rather than an investigation.

Saves cost

  • Fewer production incidents and rollbacks caused by a shared value, route or column changing under a consumer unannounced.
  • Less duplicated configuration to maintain: one shared definition inherited by many services.
  • Per-service approve/reject means a bad shared value can be refused where it would break something, before it is live.
  • OneEnv has a free plan (1 workspace, unlimited services and members, full governance), so trying it costs nothing.

These are qualitative benefits, not measured results — how much you save depends on how many services share the value.

Want to try it? Start at oneenv.io.