How to use Session Affinity on requests to Kubernetes service?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In modern cloud infrastructure, Kubernetes has established itself as a crucial tool for container orchestration, ensuring scalability and robustness of applications. One of its compelling features is the ability to manage network traffic via services. However, when dealing with stateful applications or services that require sticky sessions, session affinity becomes paramount. This article delves into the implementation and nuances of session affinity for Kubernetes services.
Session Affinity Explained
Session affinity, also known as "sticky sessions," refers to the practice of directing client requests to the same backend pod. This is vital for applications that store user session data locally, as it ensures that a user's requests are consistently handled by the same instance, maintaining session state effectively.
In Kubernetes, session affinity can be controlled on services, which manage the routing of requests to pods. This feature is especially important when you need to ensure continuity and application consistency for business logic such as shopping carts, user profiles, or interactive sessions.
Configuring Session Affinity in Kubernetes
Kubernetes achieves session affinity using the service's sessionAffinity attribute. By default, this is set to "None," meaning there is no sticky session, and requests are distributed based on round-robin or another balanced approach. You can change this setting to "ClientIP" to enable session affinity.
Step-by-Step Guide
- Create a Kubernetes ServiceBelow is a basic template of a Kubernetes Service manifest using session affinity:
- protocol: TCP
- Impact on Load Balancing: Enabling session affinity affects load distribution. Although it ensures session consistency, it might lead to uneven distribution of requests.
- Session Expiration: Kubernetes session affinity by ClientIP includes a timeout parameter (default: 3 hours) that can be adjusted according to requirement. This manages how long a session remains sticky.
- Shopping Cart Applications: Maintain persistent cart state by directing requests from the same user to one pod.
- User Profiles: Session affinity ensures that profile edits and customizations remain consistent.
- Interactive Sessions: In multiplayer gaming or chat applications, consistency is crucial.
- Scaling Issues: Sudden spikes directed to a single pod can stress that instance beyond its capacity.
- Pod Failures: StatefulSets or external data stores might provide better resilience against pod failures.
Related reading
- How to use skaffold with volumes
- How to use the kubernetes go-client to get the same Pod status info that kubectl gives
- How to use variables with forward slash in kubernetes chart?
- How to verify cluster network policy configuration/support
- How to use SSH to run a local shell script on a remote machine?
- How to use Tensorflow addons' metrics correctly in functional API?
- How to verify Kubernetes service account token JWT
- How to view logs of failed jobs with kubectl?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.