Tuesday, 17 November 2026

Sticky Sessions in System Design

  

Sticky Session (also called Session Affinity or Session Persistence) is an important concept in system design, particularly when designing applications deployed across multiple servers behind a load balancer.

 

Let's understand it from the basics, with real-world examples, architecture diagrams, advantages, disadvantages, and interview scenarios.

 

1. First, understand the problem

Imagine you are building an e-commerce application. Your application runs on three backend servers:

-       Server 1

-       Server 2

-       Server 3

 

To handle incoming traffic, you put a Load Balancer in front of these servers.


Suppose a user logs in to your application.

-       User sends a login request.

-       Load Balancer forwards the request to Server 1.

-       Server 1 validates credentials.

-       Server 1 creates a user session and stores it in its local memory.

 

For example:

Map<String, UserSession> sessions = new HashMap<>();
sessions.put("SESSION123", userSession);

   

Now the user is logged in. But what happens when the user makes the next request?

The Load Balancer might forward it to Server 2. Server 2 doesn't have SESSION123 in its local memory. As a result, Server 2 considers the user unauthenticated.

 

The problem here is that, the user was successfully authenticated on Server 1, but the next request reached Server 2, which doesn't know about the session.

 

This is where Sticky Sessions come into the picture.

 

2. What is a Sticky Session?

Sticky Session ensures that requests from a particular user are consistently routed to the same backend server for a specified period.

 

For example:

-       User A → Server 1

-       User B → Server 2

-       User C → Server 3

 

Even if User A sends 100 requests, the Load Balancer tries to route all those requests to Server 1.

 

The Load Balancer maintains an association between the client and the backend server.

 

For example:

User

Assigned Server

User A

Server 1

User B

Server 2

User C

Server 3

 

This association is called Session Affinity.

 

3. How does Sticky Session work internally?

There are different ways to implement sticky sessions. Let's understand the most common approaches.

 

Approach 1: Cookie-based Sticky Sessions

This is one of the commonly used approaches.

 

Consider the following flow:

-       Client or browser sends the first request

-       Load Balancer selects Server 1

-       Load Balancer generates a cookie. For example, Set-Cookie: SERVER_ID=server1 and shared this to the browser

-       Browser stores the cookie, the browser automatically sends this cookie with subsequent requests.

-       Load Balancer reads the cookie, It identifies SERVER_ID=server1 and routes the request to Server 1.

 

Note: The cookie may be generated by the load balancer or infrastructure. Some implementations use an application cookie or an encoded server identifier.

 

Approach 2: IP-based Sticky Sessions

In this approach, the Load Balancer uses the client's source IP address to determine the backend server.

 

For example:

Client IP

Backend Server

192.168.1.10

Server 1

192.168.1.20

Server 2

192.168.1.30

Server 3

 

 

 

The Load Balancer maintains a mapping between IP addresses and backend servers.

 

Problem with IP-based affinity:

Imagine 1,000 employees accessing your application through the same corporate NAT gateway. All requests might appear to originate from the same public IP address. Consequently, they could all be routed to Server 1, creating an uneven load distribution. Cookie-based affinity is often more suitable in this scenario.

 

What happens behind a corporate NAT gateway?

Imagine 1,000 employees accessing your application.

 

Their private IP addresses might be:

Employee

Private IP

Employee A

10.0.0.11

Employee B

10.0.0.12

Employee C

10.0.0.13

Employee D

10.0.0.14

 

All these employees access the internet through the same NAT gateway.

 


From the application's perspective, their requests might all originate from the IP Address 203.10.20.30, this is because NAT translates their private source IP addresses into the same public IP address.

 

What happens with IP-based sticky sessions?

Suppose your Load Balancer uses the client IP to determine the backend server.

 

It sees:

Employee A (10.0.0.11) -> 203.10.20.30

Employee B (10.0.0.12) -> 203.10.20.30

Employee C (10.0.0.13) -> 203.10.20.30

Employee D (10.0.0.14) -> 203.10.20.30

 

The Load Balancer calculates the backend webserver based on this IP.

 

For example:

203.10.20.30 -> Server 1

 

Consequently, the sessions for all the employees are established on Server 1.

 

How does the application differentiate users?

Typically, through cookies containing unique session IDs.

 

For example:

Employee

Public IP

Session Cookie

A

203.10.20.30

SESSION_A

B

203.10.20.30

SESSION_B

C

203.10.20.30

SESSION_C

D

203.10.20.30

SESSION_D

 

The application uses the session ID to identify the individual user's session.

 

Why is cookie-based affinity better here?

With cookie-based affinity, the Load Balancer can assign different employees to different servers. This allows better load distribution.

 

3. Sticky Sessions vs Stateless Architecture

This is particularly important from a system design interview perspective.

 

Let's compare two approaches.

 

Approach A: Sticky Sessions with local session storage

Session is stored in Server 1's memory.

Load Balancer routes the user to Server 1.

Other servers don't need to know about that session.

 

Approach B: Shared session storage

Instead of storing sessions locally, store them in a centralized system such as Redis.

 

When the next request reaches Server 2:

·      Server 2 receives the request

·      It extracts the session ID

·      It retrieves session information from Redis

·      It validates the session

·      It processes the request

 

Now any backend server can handle the request.

 

4. What happens when a server goes down?

This is one of the most important interview questions.

 

·      Suppose User A is assigned to Server 1.

·      Server 1 suddenly crashes.

·      What happens?

 

The Load Balancer detects that Server 1 is unhealthy. It can no longer route requests to Server 1, so it selects another healthy server.

 

However, there is a problem:

·      Server 2 does not have User A's session stored in its local memory.

·      The user may be logged out or required to authenticate again.

·      This is one of the biggest disadvantages of sticky sessions when combined with local in-memory session storage. We can go with shared storage approach here.

·      Sticky sessions alone do not provide session replication or high availability.

 

5. How do technologies implement Sticky Sessions?

AWS Application Load Balancer: Duration-based and application-based cookie stickiness

NGINX: ip_hash and sticky-cookie options depending on configuration

Apache HTTP Server: mod_proxy_balancer with route-based stickiness

Kubernetes: Service session affinity using ClientIP

 

For example, an NGINX configuration using IP-based affinity:

 

upstream backend {
    ip_hash;

    server server1:8080;
    server server2:8080;
    server server3:8080;
}

server {
    listen 80;

    location / {
        proxy_pass http://backend;
    }
}

   

This makes NGINX consistently select a backend based on the client IP, subject to backend availability and configuration.

 

Sticky sessions solve a request-routing problem, not a session-storage problem. When designing a scalable distributed application, I would generally prefer:

 

-       Stateless application servers.

-       Shared session storage such as Redis, if server-side sessions are needed.

-       Load balancing without depending on a particular backend server.

-       Proper session expiration and failure handling.

 

This makes horizontal scaling, deployments, and server replacement much easier.

 


You may like

Interview Questions

FNV hash algorithm implementation in Java

Controlling Randomness in Java: Exploring the Role of Seeds in java.util.Random

Calculating initial capacity from expected size and load factor

A Beginner's Guide to UUID Types

Implementing LRU Cache in Java Using HashMap and Doubly Linked List

Private IP Addresses – A Complete Guide

No comments:

Post a Comment