Why does in-process session storage break when you scale out?
A session store holds the server-side state tied to a session ID: who the user is, CSRF tokens, cart contents, half-finished form steps. Most frameworks default to keeping this in process memory. That works on one instance because every request reaches the same heap.
Add a second instance behind a round-robin load balancer and this breaks. A user logs in on instance A, and their next request lands on instance B, which has never seen that session ID. B treats them as anonymous. Users get logged out at random, carts empty, and multi-step forms reset partway through. Deploys and autoscaling make it worse. Every restarted or terminated process takes its sessions with it, so a rolling deploy logs out a share of users on each instance it replaces, and scaling in has the same effect.
Load balancer
/ | \
inst 1 inst 2 inst N
(a) local memory: each instance has its own map; sessions don't cross
(b) shared Redis: inst 1..N ---> one Redis store (ID -> data)
(c) signed cookie: client holds the data; any instance verifies it
This article compares three fixes:
- Sticky sessions pin each client to one instance. This keeps the in-memory store working, but it does not remove the coupling between a user and a single process.
- A shared Redis store moves session data out of the process, so any instance can serve any request.
- Signed cookies put the session data in the client. The server needs no store and only verifies a signature on each request.
(a) local memory (b) shared Redis (c) signed cookie
+------------------+ +------------------+ +------------------+
| client | | client | | client |
| cookie: sid | | cookie: sid | | cookie: data+sig |
+---------+--------+ +---------+--------+ +---------+--------+
| | |
v v v
+------------------+ +------------------+ +------------------+
| Load balancer | | Load balancer | | Load balancer |
+--+------+------+-+ +--+------+------+-+ +--+------+------+-+
| | | | | | | | |
v v v v v v v v v
+----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+
|in 1| |in 2| |in N| |in 1| |in 2| |in N| |in 1| |in 2| |in N|
|map | |map | |map | | | | | | | |key | |key | |key |
+----+ +----+ +----+ +--+-+ +--+-+ +--+-+ +----+ +----+ +----+
| | |
no shared state: +------+------+ no server store:
session lives on one | each instance checks
instance; lost when v the signature with
it restarts +------------------+ the shared key
| Redis |
| sid -> data |
+------------------+
Three ways to hold session state behind a load balancer: (a) a separate in-memory map per instance, (b) one shared Redis store keyed by session ID, and (c) a signed cookie that carries the data and that any instance can verify with a shared key.
How does it work? The request cycle all three approaches share
Every session mechanism handles each request in the same three steps. The approaches differ only in where the state lives.
- Identify. The browser sends a session cookie. With in-memory or Redis sessions, the cookie holds an opaque ID. With signed cookies, it holds the data and a signature.
- Resolve. The instance that receives the request turns the cookie into session data. With an in-memory store, it looks the ID up in its own map, which only works if the request reached the instance that created the session. With Redis, it runs a
GETagainst the shared store. With a signed cookie, it recomputes the HMAC with the shared key and decodes the payload. - Persist. After the handler runs, any changes are written back: to the local map, to Redis with
SET, or to the client with a newSet-Cookieheader.
Scaling out works when step 2 succeeds on any instance. Sticky sessions do not change step 2. They make the load balancer send each client to the instance whose map holds its session. Redis and signed cookies make step 2 independent of which instance answers. Once that is true, instances are interchangeable. The load balancer can use any routing algorithm, deploys can replace instances in any order, and the autoscaler can remove instances without logging anyone out.
The sections below cover each approach in detail.
How do sticky sessions work, and why are they only a migration step?
Sticky sessions, also called session affinity, leave the session in process memory and make the load balancer send each client back to the same instance. The balancer pins a client in one of two ways. With cookie affinity, it sets its own cookie that names the backend. With source-IP affinity, it hashes the client's address to pick one. Either way the application code stays the same, which is why this is the usual first fix. The state is still local, though. Only the symptom is hidden.
Users see the problem whenever their instance goes away:
- Drain: connection draining lets in-flight requests finish, but the next request goes to a new instance with no session. The user is logged out or loses a cart or a half-finished form.
- Crash: every session on that instance is lost at once.
- Scale-in: the autoscaler removes instances regardless of how many sessions they hold.
Affinity also distorts load. Long-lived sessions keep traffic on the instances that were up when users logged in. A new instance only receives new sessions, so adding capacity does little for the existing hot spots. Rolling deploys get harder for the same reason. Each replaced instance logs out its users unless you wait for its sessions to expire, and that can take hours.
Source-IP affinity has its own failure modes. Behind corporate or carrier-grade NAT, many users share one address and all land on one instance. On mobile networks a user's address can change mid-session, which moves them to a different instance with no session.
Sticky sessions are a reasonable way to stabilise a first scale-out. They should not be the final design.
How do Redis-backed sessions work?
The cookie holds nothing but an opaque session ID: a random value of at least 128 bits from a cryptographically secure generator. The session data lives in Redis under a key such as session:<id>. Every instance can resolve that key, so the load balancer can route each request anywhere.
On login, the server generates the ID and writes the data with SET session:<id> <payload> EX 1800. The key then expires on its own, with no cleanup job. The request middleware does four things:
- Reads the cookie and runs
GET session:<id>. - If the key is missing, treats the request as anonymous.
- Runs the handler.
- Writes back only if the session changed, then calls
EXPIRE session:<id> 1800to get sliding expiration.
Logout is DEL session:<id>. If you skip writes for unchanged sessions, you avoid needless traffic and reduce, but do not remove, the overwrite races covered later.
Because the server owns the state, revocation takes effect immediately. Deleting the key ends that session on the next request. To log a user out everywhere, for example after a password reset, keep a per-user set (SADD user_sessions:<uid> <id>) and delete every key in it. Large or sensitive data, such as permissions or OAuth tokens, also stays server-side and never reaches the browser.
The cost is one network round trip on every authenticated request. That puts Redis on the hot path, so if Redis is unavailable, logins fail. Development gets harder too. Each developer and CI job needs its own Redis instance or a key prefix, because a shared store lets test runs read and delete each other's sessions.
import json, secrets
import redis # redis-py client
r = redis.Redis(host="localhost", port=6379, decode_responses=True)
TTL = 1800 # 30-minute idle timeout, in seconds
def create_session(uid, data):
sid = secrets.token_urlsafe(32) # 256 random bits from a CSPRNG
r.set(f"session:{sid}", json.dumps({"uid": uid, **data}), ex=TTL) # SET ... EX 1800
r.sadd(f"user_sessions:{uid}", sid) # per-user index for logout-everywhere
return sid # only this opaque ID goes into the cookie
def session_middleware(request, handler):
sid = request.cookies.get("sid")
raw = r.get(f"session:{sid}") if sid else None # one round trip per request
session = json.loads(raw) if raw else {} # missing or expired key means anonymous
response = handler(request, session)
if raw:
updated = json.dumps(session)
if updated != raw:
r.set(f"session:{sid}", updated, ex=TTL) # write only on change; EX also resets the TTL
else:
r.expire(f"session:{sid}", TTL) # sliding expiration without rewriting data
return response
def logout(sid, uid):
r.delete(f"session:{sid}") # takes effect on the very next request
r.srem(f"user_sessions:{uid}", sid)
def logout_everywhere(uid): # e.g. after a password reset
sids = r.smembers(f"user_sessions:{uid}")
# The index may list IDs whose keys already expired; DEL ignores missing keys.
r.delete(*[f"session:{s}" for s in sids], f"user_sessions:{uid}")
A minimal redis-py session layer that stores only an opaque ID in the cookie. It uses SET with EX on login, GET on every request, a write-back only when the session changed, EXPIRE for sliding expiration, and DEL for single-session and logout-everywhere revocation through a per-user set.
What does it take to run Redis sessions in production?
Store two lifetimes. A sliding expiration resets the key's TTL on each authenticated request, so idle sessions expire. An absolute lifetime caps how long any session can live. Redis keeps only one TTL per key, so record the creation time inside the session and reject sessions past the cap in application code. Without the cap, a stolen session ID stays valid for as long as someone keeps using it.
Set maxmemory and maxmemory-policy explicitly; don't rely on defaults. Avoid allkeys-lru. Under memory pressure it evicts the least recently used keys, and those are often valid sessions of users who stepped away. They return to a login screen, and nothing logs an error. A dedicated instance with maxmemory-policy noeviction is safer because a full store rejects writes and the failure shows up in your error rates. A volatile-* policy such as volatile-ttl is also reasonable if eviction is a last resort. In both cases, size the instance so eviction never triggers.
Illustrative estimate: assume 500,000 live sessions with a 1 KB payload and about 150 bytes of per-key overhead. That is roughly 575 MB. Set maxmemory near 1.2 GB to leave headroom for traffic spikes and replication buffers.
Decide in advance what happens when Redis is unreachable. Logins should fail closed. Authenticated requests should either fail, or deliberately degrade to read-only pages with mutations blocked.
Replication is asynchronous. Sessions written just before a failover may not have reached the replica, and those users will have to log in again.
maxmemory 1200mb
maxmemory-policy noeviction
appendonly yes
appendfsync everysec
min-replicas-to-write 1
min-replicas-max-lag 10
A redis.conf for a dedicated session instance that sets an explicit memory cap, a non-evicting policy, persistence and a replica-lag write guard, with comments noting the sliding TTL and absolute cap that the application enforces.
How do signed cookie sessions work, and when should you avoid them?
With signed cookie sessions there is no store. The server serializes the whole session into the cookie and appends an HMAC computed with a secret key. On each request, any instance that holds the same key recomputes the HMAC and compares it in constant time. If they match, the payload is trusted. Horizontal scaling becomes trivial because the client carries the state.
Signing is not encryption. Anyone holding the cookie can decode the payload and read user IDs, roles, or anything else in it. If the contents are sensitive, encrypt them with authenticated encryption, or keep only an opaque identifier in the cookie.
Constraints
- Size: Browsers cap a cookie at about 4 KB, and the signature and encoding use part of that.
- Bandwidth: The cookie is sent on every request to the domain, including static assets. Serve those from a separate cookieless domain.
- Staleness: Roles and permissions stay frozen until the cookie is rewritten. A demoted admin keeps admin rights until then.
- Revocation: You cannot invalidate a single session from the server. Fixing that needs a denylist or a per-user version counter checked on each request, which brings back the shared state you were trying to remove.
Key management
Every instance needs the same key. To rotate it, keep an ordered list of keys: sign with the newest and verify against all of them, then retire old keys after the cookie lifetime has passed. This avoids logging everyone out at once. A leaked key lets an attacker forge a valid session for any user, so store it in a secrets manager, not in the repository.
import base64, binascii, hashlib, hmac, json, time
KEYS = [b"placeholder-key-2026-11-xxxxxxxx", b"placeholder-key-2026-08-xxxxxxxx"]
MAX_AGE = 7 * 24 * 3600 # seconds
def _b64(data): return base64.urlsafe_b64encode(data).rstrip(b"=")
def _unb64(data): return base64.urlsafe_b64decode(data + b"=" * (-len(data) % 4))
def sign(session: dict) -> str:
# The payload is only encoded, not encrypted. Anyone with the cookie can read it.
body = dict(session, iat=int(time.time()))
payload = _b64(json.dumps(body, separators=(",", ":")).encode())
mac = hmac.new(KEYS[0], payload, hashlib.sha256).digest() # always sign with newest key
return (payload + b"." + _b64(mac)).decode()
def verify(cookie: str):
try:
payload, mac = cookie.encode().split(b".")
mac = _unb64(mac)
except (ValueError, binascii.Error):
return None
# Try every key so cookies signed before a rotation remain valid.
for key in KEYS:
expected = hmac.new(key, payload, hashlib.sha256).digest()
if hmac.compare_digest(expected, mac): # constant-time comparison
try:
body = json.loads(_unb64(payload))
except (ValueError, binascii.Error):
return None
if time.time() - body.get("iat", 0) > MAX_AGE:
return None # expired; the server still cannot revoke it earlier
return body
return None
cookie = sign({"user_id": 42, "role": "editor"})
print(verify(cookie))
Signing a session cookie with the newest HMAC key and verifying it against every key in the rotation list, using a constant-time comparison and an issued-at age check.
What goes wrong with concurrent requests to the same session?
Most session code follows the same cycle: load the whole session at the start of a request, mutate it in memory, and save it at the end. That works for one request at a time. It breaks when a user has two tabs open or a page fires several XHR calls in parallel, because each request starts from the same snapshot and the last one to finish discards the others' changes.
With signed cookies, the race happens in the browser. Request A adds an item to the cart, and request B updates a preference. Each response carries a full Set-Cookie built from its own snapshot. Whichever response arrives last overwrites the cookie, so one change disappears. The server never sees a conflict.
With Redis, the race moves to the server. If the store serializes the session as one blob and writes it back with SET, the second write replaces the first. Redis is atomic per command, but that does not protect a read-modify-write cycle that spans an entire request.
The fixes are about how the session is written, not which store you pick:
- Update fields, not blobs. Store the session as a Redis hash and use HSET for only the keys a request changed. Concurrent edits to different fields then stop clobbering each other.
- Skip unchanged writes. Track a dirty flag and do not save on read-only requests. This removes most collisions.
- Move real state out. Carts, multi-step wizards and drafts belong in application tables with proper transactions or row versioning.
Swapping cookies for Redis does not fix the race by itself. Keep sessions small and write-light, and use them mainly for identity.
Who should use each approach?
This comparison applies to teams running a stateful web application, typically a server-rendered monolith that uses its framework's default in-memory sessions, that need to move from one instance to several. It also applies if you already run several instances and see random logouts, lost carts or reset forms after deploys or scale-in events. If you run a single instance with no plans to add more, or your API is already stateless and authenticates every request with a token, none of this changes much for you.
- Sticky sessions suit teams that need more capacity now and cannot change application code yet. Examples include a legacy codebase, a framework whose session layer is hard to swap, or a release freeze. Use them as a bridge to one of the other two approaches.
- Redis sessions suit applications whose sessions hold more than identity, such as carts, wizard state or OAuth tokens. They also suit applications that need immediate revocation, for example logout-everywhere after a password reset or when an administrator disables an account. They fit best if your team already operates Redis or can take on a replicated instance.
- Signed cookies suit applications whose session is only a user ID and a few small claims, built by teams that want no extra infrastructure on the request path. You must be able to accept revocation that is delayed until expiry or enforced through a per-user version check. They are a poor fit if the session must hold sensitive data, unless you encrypt it, or anything close to the 4 KB cookie limit.
How can moving session state off the instance save engineering time?
Most of the time saved comes from removing work that local or sticky sessions create on every deploy, scaling event and incident.
- Less debugging of intermittent logouts. "Users get logged out at random" reports are hard to reproduce because they depend on which instance served each request. With shared or client-held state, the whole class of bug disappears, and any remaining logout has a deterministic cause such as expiry, revocation or a Redis outage.
- Unattended deploys. With sticky sessions, a careful rolling deploy means draining each instance and waiting for its sessions to expire, or accepting a wave of logouts. When any instance can serve any session, deploys can replace instances in any order without someone watching for user impact.
- Simpler autoscaling. The autoscaler can add and remove instances on load alone. You do not need session-aware scale-in rules or long cooldowns to protect pinned users.
- Fewer load-balancer special cases. You drop affinity configuration and the NAT and mobile-network edge cases of source-IP affinity.
Illustrative estimate: assume a team deploys 10 times a week. Assume each sticky-session deploy needs an engineer to monitor instance drains and logout complaints for 30 minutes. That is about 5 engineer-hours a week spent attending deploys, which shared session state removes. These inputs are assumed, not measured. Substitute your own deploy frequency and attended time.
The savings are not free. Redis adds ongoing operational work: memory sizing, replication, failover testing, and per-developer or per-CI-job isolation. Signed cookies add key management and rotation. Pick the approach whose ongoing cost your team already knows how to carry.
Which session store should you choose, and how do you migrate without a mass logout?
Default to Redis-backed sessions when sessions hold more than identity or when you need server-side revocation. Use signed cookies when the session is only a user ID plus a few claims and you can tolerate revocation through a short expiry or a per-user version counter. Treat sticky sessions as temporary.
| Sticky sessions | Redis sessions | Signed cookies | |
|---|---|---|---|
| Can store | Anything, on one node | Identity, carts, wizard state | Identity and small claims |
| Revocation | Delete on owning node | Delete the key | Expiry or version check |
| Latency | In-process | One network round trip | None server-side, larger requests |
| Failure mode | Node loss logs users out | Redis outage affects all logins | Key leak allows forgery |
| Infrastructure | LB affinity | Redis with replication | Key management |
| Cost | Low now, rises with scale | Moderate, ongoing ops | Low ops, careful design |
| Suits | Short migration window | Most stateful monoliths | Stateless, identity-only apps |
Staged migration
Move from local sessions to sticky sessions first. This lets you add nodes immediately without changing application code. Next, write new sessions to the shared store or new cookie format, and read from both the old and new locations for one session lifetime. After that window, remove the old path. Users whose sessions are still active migrate as they make requests, so no one is logged out at once. Each stage can also ship and roll back on its own.
Hygiene for all three
- Set the
Secure,HttpOnlyandSameSiteflags on the session cookie. - Regenerate the session ID at login to prevent session fixation.
- Never place secrets or authorization decisions in client-side state that is signed but not encrypted.
Alternatives to consider
Sessions in your existing relational database
If your monolith already depends on a relational database, a sessions table keyed by session ID avoids running a new piece of infrastructure. The cost is load: every authenticated request becomes a primary-key read, and any write-back on access (such as a "last seen" timestamp) adds write traffic to the database that also handles your business data. This works at moderate traffic if you skip updates that don't change the session and purge expired rows with a scheduled job instead of on every request. It is a reasonable step before Redis, not a replacement for it under heavy load.
Stateless tokens (JWT-style access tokens)
Signed tokens carrying identity claims remove the server-side lookup, much like signed cookies. They share the same weakness: you cannot revoke a token before it expires without adding back a server-side check. The usual compromise is short-lived access tokens paired with a server-stored refresh token. That design still needs a session store. It just reads that store less often. Tokens help most when several services or a separate API client must verify identity. For a single monolith serving browsers, they add complexity without removing state.
Delegating to an authentication proxy
An identity-aware reverse proxy in front of the monolith can handle login and pass verified identity in a trusted header. The application then stores only application state, not authentication. This only works if the proxy is the sole network path to the app. Otherwise a client can forge the header and impersonate any user.
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 horizontal scaling, 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.
- Changing a shared variable is one edit and one review instead of editing every service's
.envby hand.
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.
- A wrong or stale value is caught at review time by the services it affects, not by a failed deploy.
- 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.