In this lesson, you will learn how to offload user session data from web servers to a centralized cache to enable true horizontal scalability.
The Problem: Server Affinity
When a user logs into a website, their "session" (the data tracking who they are and what they are doing) is often stored in the local memory of the web server they first connected to. If you have only one server, this works fine. However, as you scale horizontally by adding more web servers, you face a problem: if a user’s next request hits a different server, that server won't recognize them because it doesn't have the session data stored in its local memory.
You could use "sticky sessions" (forcing a user to stay on the same server), but this prevents load balancers from distributing traffic effectively.
The Solution: Externalized Session Stores
To solve this, we remove session data from the individual application servers entirely. Instead, we treat the web server as "stateless" and store the session object in an external, in-memory key-value engine like Redis.
When a user logs in, the application server creates a session ID, stores the session data (such as user permissions or shopping cart contents) as a serialized object in Redis, and sends the ID to the user as a cookie. On every subsequent request, the application retrieves the session data from the shared cache:
# Pseudo-code for handling a request
def handle_request(request):
session_id = request.cookies.get("session_id")
# Fetch session from shared Redis cache instead of local RAM
session_data = redis.get(f"session:{session_id}")
if session_data:
process_user_request(session_data)
else:
redirect_to_login()
Benefits for Scalability
By moving this data to a shared cache:
- Load Balancing: Your load balancer can route requests to any available server, as all servers pull the same session state from the same source.
- Resilience: If one web server crashes or is taken down for deployment, the user’s session is not lost, as it persists in the shared cache.
- Performance: Because we use an in-memory engine, the latency of fetching session state across the network is negligible compared to the overhead of querying a traditional relational database for every single user request.
Key Takeaway: Externalizing session state to a shared in-memory cache transforms web servers into stateless nodes, enabling seamless horizontal scaling across your entire infrastructure.