Session affinity is a load-balancing mode that pins a client to one backend instance, usually by setting a cookie or hashing the client's IP address. Every later request in that session goes to the same server.
Why it matters
Applications that keep login or cart state in server memory break when a request reaches a different server. Affinity is a quick workaround. Its costs are uneven load, since busy users stay on busy servers, and weaker failover, because when the pinned server dies its sessions are lost unless state is stored elsewhere. The sturdier fix is to keep session data in a shared store such as a database or cache so any server can handle any request.
Example
An nginx upstream using IP-hash affinity:
upstream app {
ip_hash;
server 10.0.0.11;
server 10.0.0.12;
}
Clients behind one corporate NAT share an IP and all land on the same server, skewing the load. Cookie-based affinity is more precise. Check what your load balancer sets with the cookie checker.
Common mistakes
- Using IP hash and overloading a server when many users share one NAT address.
- Forgetting that draining a node does not move pinned users until their sessions end.
- Treating affinity as a substitute for shared session storage.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.