Sticky Session
ELI5 — The Vibe Check
Sticky sessions make sure a user always gets routed to the SAME server, like getting the same cashier every time you visit a store. It's needed when your app stores session data in memory on one server. The downside: if that server goes down, the user's session is gone.
Real Talk
Sticky sessions (session persistence or session affinity) configure a load balancer to route a client's requests to the same backend server for the duration of a session, typically using a cookie or IP hash. Required when session state is stored locally on servers rather than in a shared cache.
Show Me The Code
# Nginx sticky sessions with ip_hash
upstream backend {
ip_hash; # Same IP always goes to same server
server app1.example.com;
server app2.example.com;
}
# Better: use shared session storage instead
# Store sessions in Redis, not server memory
app.use(session({
store: new RedisStore({ client: redisClient }),
secret: 'your-secret'
}));
When You'll Hear This
"We need sticky sessions because the app stores state in memory." / "Replace sticky sessions with a Redis session store so any server can handle any user."
Related Terms
Load Balancer
A load balancer is like a traffic cop for servers.
Redis
Redis is an incredibly fast database that lives entirely in memory (RAM). It's used as a cache, a session store, and a message queue.
Round Robin
Round Robin is the simplest load balancing strategy: send request 1 to server A, request 2 to server B, request 3 to server C, then back to server A, and s...
Session
A session is the server's way of remembering who you are across multiple requests.