Users logged out at random after adding a second server
Short answer: If customers get logged out at random, lose their basket or see "session expired" right after you add a second app server, your sessions are almost certainly stored in each server's memory. The load balancer sends one request to server A and the next to server B, which has never heard of that session. You can fix it quickly with sticky sessions, which pin each visitor to one server, but the durable fix is a shared session store that every server reads from.
Why it starts after you scale out
With one app server, keeping sessions in process memory works fine. Every request lands on the same machine, so the session data is always there. The trouble begins the day you add a second server or a second container behind a load balancer, often to handle a busy season or to deploy without downtime.
A load balancer spreads requests across servers. Unless you tell it otherwise, it doesn't care that request two belongs to the same visitor as request one. So the login happens on server A, the session lives in A's memory, and the next page view goes to server B. B finds no session for that cookie and treats the visitor as a stranger. To the customer, it looks random: sometimes it works, sometimes they're bounced to the login page.
The default session store in many frameworks is in-memory, because that's simplest for development. The README for the popular Express session middleware is blunt about this: its default MemoryStore is purposely not designed for a production environment, will leak memory under most conditions and does not scale past a single process.
Symptoms that point to this problem
- Users are logged out a few clicks after signing in, but not every time.
- Basket contents or form progress vanish between pages.
- CSRF or "invalid token" errors appear on form submits, because the token was stored in a session on another server.
- The problem disappears when you scale back to one server.
- Restarting or deploying logs everyone out, because the memory holding sessions is wiped.
That last one is worth noticing even with a single server. In-memory sessions die with the process.
Option 1: sticky sessions
Sticky sessions (also called session affinity) tell the load balancer to keep sending a visitor to the same backend. There are two common ways to do it.
IP-based affinity. In nginx, the ip_hash directive in an upstream block distributes requests based on the client's IP address. The nginx documentation explains that the first three octets of an IPv4 address, or the whole IPv6 address, are used as the hashing key, so requests from the same client go to the same server unless it's unavailable. Many visitors share addresses, though, so the spread can be uneven.
Cookie-based affinity. The load balancer sets its own cookie naming the backend, then routes on it. nginx has a sticky cookie directive for this; its documentation notes it was once available only in the commercial subscription, so check the docs for your version. Managed cloud load balancers usually offer the same feature as a "stickiness" setting with a duration.
Sticky sessions are a fair short-term fix. Their weaknesses:
- When a server is removed, restarted or crashes, everyone pinned to it loses their session.
- Load can bunch up on one server, because long-lived visitors stay put.
- Rolling deploys get awkward: you need to drain servers gently or people get logged out.
- They hide the real issue, which is that state lives in a place only one machine can see.
Option 2: a shared session store
The better pattern is to make app servers stateless and keep session data somewhere they can all reach. Common choices:
- A key-value store such as Redis or Valkey, which is fast and supports expiry times on keys.
- Your main database, using a sessions table with an expiry column. It's slower, but you already run and back it up.
- Your framework's built-in drivers. Most web frameworks ship session drivers for Redis, the database or memcached; switching is often a config change plus a store package.
Once sessions live in a shared store, any server can handle any request, deploys stop logging people out, and you can add or remove servers freely.
A few things to get right:
- Expiry. Set a time-to-live on stored sessions that matches your idle timeout, so the store doesn't fill with stale data.
- Security. Keep the store on a private network, require authentication, and don't expose it to the internet.
- Availability. If the session store goes down, everyone is logged out. A small managed instance with persistence and monitoring is usually enough for a small business app.
- Size. Store an ID and a few small values, not whole objects or files. Large sessions make every request slower.
Option 3: signed cookie sessions
Some frameworks can keep the whole session in a signed (and optionally encrypted) cookie, so there's nothing to share between servers at all. This works well for small amounts of data, like a user ID and a few preferences. The trade-offs are cookie size limits, the data travelling with every request, and the fact that you can't easily revoke one session on the server without extra work, such as a server-side list of revoked sessions or a per-user version number.
A practical order of work
- Confirm the cause. Scale to one server for a test, or check logs for requests from the same session ID landing on different backends.
- If customers are suffering now, enable sticky sessions on the load balancer as a stopgap.
- Move sessions to a shared store, test login, basket and checkout across several servers, then deploy.
- Remove stickiness once the shared store is proven, so load spreads evenly again.
- Test a rolling deploy and a server restart while logged in, to confirm nobody is kicked out.
A worked example
Picture a hypothetical Denver online gift shop that added a second app container before the holiday rush. Within a day, support emails mentioned empty baskets. The team saw that sessions were held in memory and that the load balancer used round-robin. They turned on cookie stickiness that afternoon, then moved sessions to a managed Redis instance with a two-hour expiry the following week. After that, they switched stickiness off and checked that deploys no longer emptied anyone's basket.
FAQ
Are sticky sessions bad?
Not bad, but limited. They're a reasonable stopgap or a fit for apps that genuinely hold per-server state, such as some real-time connections. For ordinary logins and baskets, a shared store is more resilient.
Will a shared session store slow my site down?
A key-value store on the same private network adds very little time per request. A database-backed store is slower but often fine for small sites.
Do I need this if I only have one server?
Not for routing, but in-memory sessions still vanish on every restart or deploy. Moving them to a store fixes that too.
Does this affect APIs that use tokens?
APIs that authenticate each request with a token don't rely on server sessions, so they usually don't have this problem.
If customers are being logged out since you added servers, send us a note about your setup and we'll help you pick the right session approach.