CodePark/ questions / Scaling & statelessness
START PRACTICINGPRACTICE
Backend question · System design & architecture › Scaling & statelessness

You scale an Express API from 1 to 3 instances behind a round-robin load balancer. Users now get logged out at random. What is the most likely cause?

Sessions are stored in each process's memory, so other instances do not know the user

The cause is that "Sessions are stored in each process's memory, so other instances do not know the user". The user logs in on one instance. The next request may land on a different instance. That instance finds no session, so it treats the user as logged out.

The fix is one of these:

• a shared session store, such as Redis

• stateless tokens, which carry the user's data with each request


Sticky sessions send each user to the same instance every time. They would hide the symptom, but they bring two problems: uneven load, and lost sessions when an instance fails.

The other options don't fit. "Round robin drops requests when instances are busy" is false: round robin just sends requests to each instance in turn. "The JWT signing key is too short" is a security weakness, but it would not log users out at random. "The database connection pool is exhausted" would cause slow or failed requests, not random logouts.