Home

sessionaffinity

SessionAffinity is a technique used by load balancers and service proxies to route all requests from a given client to a single backend server for the duration of a session. The goal is to preserve session state and avoid repeated initialization or authentication on each request.

Common approaches to implement session affinity include: Client IP affinity, which uses the client's IP address

Benefits include improved user experience and reduced server-side session retrieval costs for apps that store state

Platform patterns vary. In Kubernetes, a Service can specify sessionAffinity: ClientIP with an optional timeoutSeconds to

See also: load balancing, session persistence, sticky sessions, and stateful service design.

to
select
and
consistently
route
requests
to
the
same
backend;
Cookie-based
affinity,
in
which
the
load
balancer
issues
a
persistent
cookie
that
binds
the
client
to
a
specific
backend;
Session
token
affinity,
where
the
application-provided
token
determines
the
backend;
and
hash-based
affinity,
which
applies
a
deterministic
hash
of
client
identity
to
choose
a
backend
while
aiming
to
balance
load.
on
the
backend.
Drawbacks
include
skewed
load
distribution,
reduced
resilience
to
instance
failures,
and
operational
complexity
during
scaling
or
rolling
upgrades.
Client
IP
can
be
unreliable
when
clients
are
behind
NAT
or
proxies;
cookie-based
methods
can
be
affected
by
client
settings
or
privacy
constraints;
and
backend
migrations
can
disrupt
sessions
unless
designed
for
migration
or
session
transfer.
control
affinity
duration.
Cloud
load
balancers
offer
stickiness
through
duration-based
cookies
or
load
balancer
generated
cookies.
NGINX
and
HAProxy
support
sticky
sessions
via
cookies
or
tokens,
configurable
per
backend
or
route.