Have you ever observed that some internet sites seem to remember you when you click around or navigate away? This is what is referred to as sticky sessions, or session persistence. Let’s keep the explanation simple.
• Cookie-based: The load balancer uses a small cookie in your browser to remember which server your session is associated with. This is sufficient for most use cases.
• IP Hashing: The load balancer hashes your IP address to choose a server for your session. This can be easy, but with shared IP addresses, your sticky session is not necessarily consistent among users.
• Session Tokens: Some applications will use tokens to indirectly decide which server to send your request.
• Database storage: Will store all the session data in a single location that any server can access.
• Memory storage: Will supply fast session store for instance, Redis or Memcached, provides a session to be cached in memory for higher retrieval speed
• Cloud load balancers (e.g. AWS ELB or Azure LB)
• Kubernetes with session affinity
This essentially tells the system to “send this user to the same server every time.”
You should try this on your website. You'll find that your website is more reliable and users will have a better experience.
What are Sticky Sessions?
Sticky sessions simply mean that when a user makes a request, the request always goes to the same web server. For example, if you are shopping on-line, you want your shopping cart to remain the same. If requests are sent to different servers, your shopping cart could suddenly disappear. Sticky sessions will prevent that.Why Sticky Sessions are Helpful
Sticky sessions help to make web sites more user-friendly by:- Helping to maintain user data
- Preventing unexpected sign-outs
- Improving user experience when navigating the site
How to Configure Sticky Sessions
You can do it in a few different ways, depending on your infrastructure:1) Using a Load Balancer
Most sticky sessions are implemented at the load balancer:• Cookie-based: The load balancer uses a small cookie in your browser to remember which server your session is associated with. This is sufficient for most use cases.
• IP Hashing: The load balancer hashes your IP address to choose a server for your session. This can be easy, but with shared IP addresses, your sticky session is not necessarily consistent among users.
• Session Tokens: Some applications will use tokens to indirectly decide which server to send your request.
2) Within the Application
Some applications implement session data for requests:• Database storage: Will store all the session data in a single location that any server can access.
• Memory storage: Will supply fast session store for instance, Redis or Memcached, provides a session to be cached in memory for higher retrieval speed
3) Sticky sessions in clouds or containers.
Sticky sessions in cloud environments can be implemented using:• Cloud load balancers (e.g. AWS ELB or Azure LB)
• Kubernetes with session affinity
This essentially tells the system to “send this user to the same server every time.”
Highlights
Sticky sessions serve to improve the user experience when consuming a website. You can leverage cookies, IP based methods, and/or storing session information in the database to maintain session state. The idea is simple: keep users connected to the same server so data can persist across requests.You should try this on your website. You'll find that your website is more reliable and users will have a better experience.