Why do sticky sessions block horizontal scaling?
Sticky sessions (session affinity) tell the load balancer to keep sending a client to the same app server, usually by setting a cookie or hashing the client IP. They exist because the monolith keeps session state in process memory, so only the node that created a session can serve it. This works with two or three servers. It starts to fail once you scale out.
Load stops being even. The balancer pins clients by session, not by current load. A node that picked up a few heavy users, or a large group of clients behind one corporate NAT under IP hashing, stays overloaded while its neighbors sit idle. A new node gets only new sessions, so it takes a long time to absorb any of the existing traffic.
Sessions are tied to the life of a process. If a node crashes, every session on it is gone, and those users are logged out or lose carts and half-finished forms. Draining a node in a controlled way is not much better. You either wait for sessions to expire or cut them off.
Deploys and scale-in become negotiations. A rolling deploy has to drain each node, so it runs as slowly as your longest session timeout. Autoscaling has the same problem in reverse: removing an instance destroys user state, so teams tend to over-provision rather than scale in.
The fix is stateless app servers, where any node can handle any request because session state is stored somewhere else. This guide is for backend and platform engineers who run a session-dependent monolith behind a load balancer and want to move that state into a Redis session store without a big-bang rewrite.
Who should use a Redis session store?
A Redis session store fits teams that match most of these conditions:
- You run a monolith or service that keeps session state in process memory and relies on load balancer affinity to make that work.
- Your fleet changes size. You autoscale, or you add and remove instances often enough that pinned sessions get in the way.
- You deploy frequently. Rolling deploys that must drain each node for a full session timeout slow down every release.
- You need server-side control over sessions. You want to revoke a single session, force "log out everywhere", or invalidate sessions after a password change, which signed cookies cannot do on their own.
- Your session payload is moderate but not tiny. It is too large or too sensitive to put in a cookie, but small enough to fetch on every request without strain.
- You can operate Redis reliably, or already do, including monitoring, persistence choices and failover.
It is a poor fit for a small, fixed fleet with rare deploys, where sticky sessions cost little, and for applications whose sessions are a few small, non-secret fields that never need server-side revocation, where signed cookie sessions are simpler. The last section covers those alternatives in more detail.
What is a Redis session store and how does it make app servers stateless?
A Redis session store separates the session identifier from the session data. The browser holds only an opaque ID in a cookie. Everything the application knows about that session, such as user ID, CSRF token, cart contents and flash messages, lives in Redis under a key like sess:3f9a…. The sess: prefix keeps session keys apart from cache or queue keys in the same instance and makes them easy to scan, count or flush as a group.
The ID must be unguessable, so generate it from a cryptographically secure random source with at least 128 bits of entropy. Anyone who can predict an ID can take over that session. Sign the cookie so tampered values are rejected before they reach Redis. Mark it HttpOnly so page scripts cannot read it, and Secure so it never travels over plain HTTP.
The request path
- The load balancer routes the request to any healthy app server, with no affinity.
- The server verifies the cookie signature and extracts the ID.
- It fetches
sess:<id>from Redis, handles the request, writes back any changes and refreshes the TTL.
No app server keeps anything between requests, so you can add, drain or kill instances freely.
What it costs
- Latency on every request. Each request makes at least one network round trip to Redis.
- A hard dependency. If Redis is down, nobody stays logged in.
- Connection pooling. Without a pool, each request pays connection setup and can exhaust Redis client limits.
- Session size. Every byte is serialized and moved on each request, so keep sessions small.
import hashlib, hmac, json, secrets
import redis
SECRET = b"placeholder-load-from-env-not-source" # cookie signing key
TTL = 1800 # idle timeout in seconds
pool = redis.ConnectionPool(host="redis.internal", port=6379, max_connections=50)
r = redis.Redis(connection_pool=pool)
def _mac(sid):
return hmac.new(SECRET, sid.encode(), hashlib.sha256).hexdigest()
def create_session(data):
sid = secrets.token_urlsafe(32) # 256 bits from a CSPRNG
r.set(f"sess:{sid}", json.dumps(data), ex=TTL)
# Send as: Set-Cookie: sid=<value>; HttpOnly; Secure; SameSite=Lax; Path=/
return f"{sid}.{_mac(sid)}"
def load_session(cookie):
# Reject tampered cookies before touching Redis.
sid, _, mac = (cookie or "").rpartition(".")
if not sid or not hmac.compare_digest(mac, _mac(sid)):
return None, None
raw = r.get(f"sess:{sid}") # one network round trip per request
return sid, (json.loads(raw) if raw else None)
def save_session(sid, data):
# Write back changes and refresh the TTL. Keep data small:
# it is serialized and sent over the network on every request.
r.set(f"sess:{sid}", json.dumps(data), ex=TTL)
A minimal Python session store using redis-py that issues a 256-bit random ID in an HMAC-signed cookie, keeps all state under a namespaced sess: key with a TTL, and uses one shared connection pool per process so any app server can handle any request.
How does a Redis session store work?
A session goes through the same lifecycle in every implementation, whether you write the adapter yourself or use your framework's Redis session backend.
- Creation. When a user logs in or first needs a session, the app generates a random ID, writes the session data to Redis under
sess:<id>with an expiry (SET ... EX), and sends the signed ID to the browser in a cookie. - Lookup. On each later request, whichever app server receives it verifies the cookie signature, reads
sess:<id>from Redis, and deserializes the data. A missing key means the session expired or was deleted, so the user is treated as logged out. - Update. If the request changes the session, the app writes the new data back. It does this either as a whole blob with
SETor field by field withHSETif the session is stored as a hash. - Expiry. Redis removes keys automatically once their TTL runs out, so idle sessions clean themselves up without a sweeper job. Refreshing the TTL on activity turns this into a sliding idle timeout.
- Termination. On logout the app deletes the key with
DEL. The ID in any copied cookie then points at nothing.
Redis runs each command atomically, so a single GET, SET or HSET never observes a half-written session. Multi-step changes, such as reading, editing and writing back, are not atomic by default. That is why the concurrency section below covers WATCH/MULTI/EXEC and per-field writes.
Because every server reads and writes the same keys, the load balancer can route each request anywhere. The app servers hold only code and a connection pool. Session state lives in Redis.
How should you design session data, expiry and concurrency?
Before moving anything, audit what the monolith actually puts in session. Long-lived apps accumulate cached query results, full user objects, and derived permission sets. Keep identifiers and small flags, and recompute or cache everything else elsewhere. A user ID plus a roles version is usually enough.
Serialization
Use a plain, versioned format such as JSON. Language-native serializers like Java serialization, Python pickle, or PHP serialize couple stored bytes to class definitions. A renamed field or moved class then breaks deserialization during a rolling deploy, while old and new code read the same keys. These formats are also an attack path, because deserializing them can execute code if anyone can write to Redis.
Expiry
Treat the key TTL as the idle timeout. Refreshing it on every request adds a write per request. Instead, refresh only when the remaining TTL drops below a threshold, for example TTL sess:abc followed by EXPIRE sess:abc 1800. Store a created_at field and reject sessions older than the absolute lifetime in application code, because sliding TTLs never enforce that limit.
Concurrency
Parallel requests (tabs, XHR bursts) each read the whole session, change one thing, and write the whole session back. The last write wins and silently discards the others' changes. There are three fixes:
- Write only on change, so read-only requests never overwrite state.
- Per-field hashes, so
HSET sess:abc cart_id 42touches one field rather than the whole blob. - Optimistic checks, using
WATCH sess:abc, thenGET, thenMULTI/SET/EXEC, and retrying ifEXECreturns nil.
> TTL sess:abc
(integer) 412
> EXPIRE sess:abc 1800
(integer) 1
> WATCH sess:abc
OK
> GET sess:abc
"{\"v\":1,\"uid\":1017,\"roles_v\":3,\"created_at\":1762675200}"
> MULTI
OK
> SET sess:abc "{\"v\":1,\"uid\":1017,\"roles_v\":3,\"created_at\":1762675200,\"cart_id\":42}" EX 1800
QUEUED
> EXEC
1) OK
> EXEC
(nil)
> HSET sess:abc cart_id 42
(integer) 1
A redis-cli session showing the threshold-based TTL refresh, a WATCH/MULTI/EXEC optimistic write that keeps the TTL and retries on a nil EXEC, and a single-field HSET alternative.
How do you migrate from sticky sessions to Redis incrementally?
Treat the migration as a series of reversible steps, each with its own exit criterion, rather than one cutover.
- Add read-through with dual-write. On each request, check Redis first. On a miss, read the session from the local in-memory store and copy it into Redis. Every session write goes to both stores. Users never get logged out, because the old store remains the fallback.
- Keep sticky sessions on. Affinity keeps each user on the node that holds their in-memory copy. If Redis has a gap or a serialization bug, it costs nothing yet.
- Measure the Redis hit rate. Count requests where the session came from Redis versus from the fallback. The fallback rate should fall toward zero as old sessions get copied over or expire. Wait at least one full session TTL so no session exists only in memory.
- Remove affinity at the load balancer. Do this for a fraction of traffic first if your balancer supports it. Any request still falling back now points to a real bug.
- Delete the dual-write and fallback code once the fallback counter stays flat.
During the transition, the two stores can briefly disagree, for example when one of the two writes fails. Resolve this by making Redis the source of truth whenever both stores have a copy.
The extra code is temporary. It lives in one session adapter, so you can scope the work to that adapter plus metrics and a removal ticket.
The payoff is diagnosis. A big-bang cutover that logs everyone out leaves you sorting through serialization, TTL and network faults under pressure. Each step here isolates one of those risks and can be rolled back with a config change.
import json
import redis
from prometheus_client import Counter
SESSION_READS = Counter("session_reads_total", "Session reads by source", ["source"])
SESSION_TTL_SECONDS = 1800 # match the existing in-memory session TTL
class MigratingSessionStore:
"""Temporary adapter: Redis first, in-memory fallback, dual-write."""
def __init__(self, local_store, fallback_enabled=True):
self.redis = redis.Redis(host="sessions.internal.example", port=6379)
self.local = local_store # the old per-node dict-like store
self.fallback_enabled = fallback_enabled # config flag for rollback/removal
def get(self, session_id):
raw = self.redis.get(f"session:{session_id}")
if raw is not None:
SESSION_READS.labels(source="redis").inc()
return json.loads(raw) # Redis wins whenever it has a copy
if not self.fallback_enabled:
SESSION_READS.labels(source="miss").inc()
return None
data = self.local.get(session_id)
if data is not None:
SESSION_READS.labels(source="fallback").inc()
self.set(session_id, data) # read-through: copy into Redis
return data
def set(self, session_id, data):
# Write Redis first so the source of truth is updated before the old store.
self.redis.set(f"session:{session_id}", json.dumps(data), ex=SESSION_TTL_SECONDS)
if self.fallback_enabled:
self.local[session_id] = data # dual-write; delete in step 5
A temporary Python session adapter using redis-py that reads Redis first, falls back to and copies from the in-memory store, dual-writes, and counts reads by source so the fallback rate can confirm coverage before affinity is removed.
How should you configure Redis for sessions?
Start by keeping sessions away from cache traffic. If sessions share an instance with a cache running allkeys-lru or allkeys-lfu, a burst of cache writes can evict session keys. Users get logged out and nothing logs an error. A dedicated instance is the safest choice. A separate logical database is not enough, because eviction applies across the whole instance.
On the session instance, set maxmemory-policy noeviction. When memory fills, writes fail loudly instead of sessions disappearing. If you choose volatile-lru or volatile-ttl to shed the oldest sessions under pressure, alert on the evicted_keys counter in INFO stats so evictions don't go unnoticed.
Choose persistence and replication based on how much session loss you can tolerate:
| Setting | Session loss on failure | Cost |
|---|---|---|
| No persistence, no replica | All sessions on restart | Lowest |
appendonly yes, appendfsync everysec |
Up to about one second of writes | Moderate disk I/O |
| AOF plus replica with automatic failover | Writes not yet replicated | Extra node, failover tooling |
Replication is asynchronous. A primary can acknowledge a login, then fail before the replica receives it, and the promoted replica won't have that session. min-replicas-to-write and min-replicas-max-lag limit how much the replicas can fall behind, but they don't guarantee zero loss.
Decide in advance what happens when Redis is down or slow. Set short connect and read timeouts. Then choose one of two behaviors:
- Fail closed: return errors on authenticated routes.
- Degrade: treat the user as logged out and keep public pages working.
Don't fall back to in-process sessions. That brings back sticky state without the load balancer routing that made it work.
port 6379
bind 10.0.2.15
protected-mode yes
requirepass change-me-session-store-password
maxmemory 2gb
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 rejects writes instead of evicting sessions, uses AOF with fsync every second, and limits replica lag, with comments on how much session loss each setting still allows.
How do you secure and monitor a Redis session store?
Security operations
Regenerate the session ID on login and on any privilege change, such as sudo mode or a role grant. This blocks session fixation: an ID an attacker planted before login becomes worthless once it's issued. On logout, delete the Redis key. Clearing the cookie alone leaves a stolen copy of the ID valid until its TTL expires.
To support "log out everywhere", keep a per-user set of active session IDs. Add the ID to the set when you create a session, and delete every member when the user asks to sign out of all devices. Set members don't expire individually, so prune IDs whose session keys have already expired whenever you read the set.
Require TLS and authentication on the Redis connection. Session data is a bearer credential. Where your Redis supports ACLs, give the application a user that can only touch the session key prefix.
Monitoring
| Signal | What it reveals | Alert when |
|---|---|---|
| Command latency | Slow commands, network or CPU saturation | p99 exceeds your request budget |
Memory vs maxmemory |
Headroom before eviction | Sustained above ~80% |
| Key count | Session growth, TTL bugs | Trend diverges from active users |
| Evictions | Live sessions being dropped | Any non-zero rate |
| Pool saturation | App waiting for connections | Wait time rises |
| Session-miss rate (app-side) | Users unexpectedly logged out | Deviates from baseline |
Alerting on these signals together cuts triage time because the combination tells you where the fault is. A miss-rate spike alongside evictions points to memory. A miss-rate spike while Redis looks healthy points to the application, such as a cookie-domain or serialization change in the latest deploy.
How can a Redis session store save engineering time?
Most of the time savings come from removing work that sticky sessions force on every release and every incident.
Faster rolling deploys. With sticky sessions, each batch of nodes has to drain for up to the session timeout before it can restart without logging users out. With shared sessions, a node can stop as soon as its in-flight requests finish. Here is an illustrative estimate with assumed inputs: a 10-node fleet deployed in batches of 2 (5 batches), with an assumed 30-minute drain per batch. The drain alone accounts for about 150 minutes of every deploy. Without affinity, the drain shrinks to the time needed to finish in-flight requests. Your own numbers depend on your timeout, batch size and request duration, so measure them before and after.
Less over-provisioning work. When scale-in no longer destroys user state, autoscaling policies can remove instances. Engineers stop maintaining manual capacity buffers and exceptions to keep sessions alive.
Fewer "users got logged out" incidents. A crashed or replaced node no longer takes sessions with it. That removes a whole class of support tickets and the investigation that follows each one.
Simpler debugging. Session state is inspectable in one place. An engineer can run GET sess:<id>, HGETALL sess:<id> or TTL sess:<id> in redis-cli to see exactly what a user's session contains and when it expires. The alternative is guessing which node holds it and attaching to that process.
Less load balancer special-casing. Affinity configuration, cookie-based routing rules and per-node health workarounds go away. The balancer becomes plain round-robin or least-connections, which is easier to reason about and change.
Clean revocation features. "Log out everywhere", forced logout after a password reset, and admin session kills become a few key deletions instead of a fan-out to every app node.
These savings come with ongoing costs: operating Redis, keeping serialization compatible across deploys, and monitoring the signals above. The trade pays off when you deploy often or scale dynamically. For a small, static fleet it may not, which the next section covers.
When should you avoid Redis sessions, and what are the alternatives?
Redis is not free. It adds a network hop to every request, a service that must stay up, and serialization code that has to stay compatible across deploys. Two alternatives are worth weighing first.
Stay with sticky sessions when the fleet is small and fixed, deploys are rare, and losing a session on node failure is tolerable. A handful of servers behind a load balancer that you restart monthly gains little from shared state, and you avoid a new dependency entirely.
Use signed or encrypted cookie sessions when the payload is small, such as a user ID, a role and a CSRF token, and you never need to kill a session from the server. Every app server can validate the cookie with a shared key, so servers stay stateless without any shared store. The catch is revocation. A stolen cookie stays valid until it expires, unless you add a server-side denylist, which brings back the shared store. Rotating the signing key works only as a blunt fallback because it logs out every user at once.
| Concern | Sticky sessions | Redis store | Cookie sessions |
|---|---|---|---|
| Size limit | Server memory | Redis memory | Roughly 4 KB per cookie, sent on every request |
| Data exposure | Server-side | Server-side | Readable by the client unless encrypted |
| Revocation | Delete on node | Delete key | Not possible without a denylist |
| Operational dependency | Load balancer affinity | Redis availability | Key management |
| Development cost | Lowest | Moderate | Low to moderate |
Decision checklist
- Do you need server-side logout or forced revocation? If yes, choose Redis.
- Does the session fit comfortably under a few kilobytes, with nothing secret in it? If yes, choose cookies.
- Is the fleet small and static, with rare deploys? If yes, sticky sessions are acceptable.
- Are you autoscaling or deploying often? If yes, rule out sticky sessions.
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 redis session store, 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.