Sticky Sessions, Session affinity & Session persistence
First of all, all these three terms in title are same and used interchangeably.
Sticky sessions or session affinity is technique used in load balancers. Its used by load balancers to ensure that all requests for a given session from a particular user always go to the same server.
Once a client establishes a connection with a particular server, the load balancer remembers this mapping and directs all subsequent requests from that client to the same server.
Here's a quick illustration -

Why connect a client to same server ?
In distributed systems, we try to connect a client to same server for three reasons.
- Performance
We dont need to remember and transfer the sessions. This saves time and bandwidth.
- Caching efficieny
Allows servers to maintain local caches of user data, improving response times.
- Session state preservation
Without sticky sessions, the server may not have all the necessary information to handle the user's session correctly. This is critical for applications that rely on session state, such as shopping carts or user authentication. E.g Without sticky sessions: User adds items to cart on Server A, but next request goes to Server B where cart appears empty.
How sticky sessions are implemented ?
There are different ways in which load balancers implement sticky sessions. Lets discuss them one by one.
- Cookie based stickiness
In this approach, the load balancer add a unique set-cookie header to the response sent to the client for the first request. This cookie contains information about the server to which the client should be directed for subsequent requests.
The cookie added can be either load balancer specific or application specific.
Application specific cookie
Load balancer specific cookie
The client's browser then sends the cookie in subsequent requests, allowing the load balancer to direct the request to the appropriate server.
or
- IP based stickiness
In this approach, the load balancer uses clients IP address and keep a mapping table to direct the request to the same server. This approach does not work for clients using dynamic IP addresses or clients behind NAT.
- URL based stickiness
In this approach, the application embedds the session id in the url. The load balancer uses a parsing logic to extract this session id and direct the request to the same server.
Configuring sticky sessions
In this section we will discuss how to configure sticky sessions in different load balancers.
- NGINX
The sticky; directive within the upstream block enables the default cookie-based sticky sessions using the ngx_http_upstream_module. By default, it uses the route method, which inserts a cookie named ROUTEID.
For open-source Nginx, you can achieve IP-based stickiness using the ip_hash directive:
- AWS Elastic Load Balancer
You can configure sticky sessions for a Classic Load Balancer through the AWS Management Console or the AWS CLI.
Using the load balancer generated cookie
Using the application generated cookie
- HAProxy
Drawbacks of sticky sessions
- Uneven load distribution
Sticky sessions can lead to uneven load distribution across servers. This can be a result of user hotspots where we have a small number of users generating large number of requests.
Hotspot issue essentially negates the benefits of load balancing.
- Single point of Failures
If a backend server handling sticky sessions fails, all sessions tied to that server will be lost, leading to a poor user experience (e.g., loss of shopping cart data, being logged out).
This reduces the overall fault tolerance of the system.
We need to employ session replication to mitigate this issue. This is done by maintaining a copy of the session data in distributed caches or database.
- Scaling issues
Scaling out the backend tier can be complicated with sticky sessions. Adding new servers won't automatically balance existing sessions. Newly added servers will only handle new sessions.
- Client side dependencies
Cookie-based stickiness relies on the client's browser accepting and sending cookies. If a user disables cookies, stickiness will be lost.
This makes the system vulnerable to client-side configuration changes.