Cloud concept · Updated

Session Affinity - Session affinity is a load-balancing behavior that routes a client's related requests to the same backend target while the affinity remains valid and the target is available.

In short

Session affinity helps applications that keep session data or useful cached context on individual backend servers. It can simplify support for stateful applications, but it can also create uneven load and does not preserve session data when a backend fails.

Cloud Concepts Hub →

6 key aspects of Session Affinity

Session affinity provides backend continuity, but its identity mechanism, lifetime, load distribution, and failure behavior must match the application's state-management requirements.

01
Repeatable backend selection

Related requests are preferentially routed to the same target instead of being independently balanced. The assignment is conditional on the affinity rules, expiration behavior, and target availability.

02
Client identification mechanism

HTTP cookies can identify a browser session, while source-IP affinity groups requests by network identity. Shared public IP addresses and changing client addresses can make IP-based affinity unsuitable for user-level session continuity.

03
Routing rather than state storage

Affinity directs traffic but does not replicate application memory or make session data durable. Applications must handle expired assignments, backend replacement, and lost local state.

04
Uneven utilization and scaling

Long-lived or unusually busy sessions can concentrate traffic on a subset of targets. New capacity may receive new assignments without relieving targets that retain existing sticky sessions.

05
Failure-aware operation

Unhealthy or removed targets cannot be relied on to retain an assignment. Test failover and deployment behavior, including what users experience when requests move to another backend.

06
Shared-state alternative

Externalizing session data allows healthy targets to serve requests interchangeably. This usually improves scaling flexibility, although the shared store introduces its own latency, capacity, and availability requirements.

Cloud services for Session Affinity

An application-layer load balancer such as AWS Application Load Balancer or Azure Application Gateway can use cookies to maintain backend continuity for web requests. A Network Load Balancer instead provides source-IP stickiness in supported configurations, making client network identity important to traffic distribution. These load balancers are alternatives selected according to protocol and routing requirements, not components that must be chained together. A shared session store such as Amazon ElastiCache can reduce dependence on affinity by letting multiple application targets retrieve the same session data. Health checks, redundant targets, and resilient state storage remain necessary regardless of the routing choice.

Amazon ElastiCacheAmazon ElastiCache

Can provide a shared session store for application instances, reducing their dependence on load-balancer affinity. Session expiration, eviction behavior, replication, and failover must be designed so that the application's required continuity is preserved.

Session Affinity exam questions

Real practice exam questions on Session Affinity, with the correct answer and its explanation. Answer them with every option in the exam simulator.

DVA-C02

A company runs a legacy web application on Amazon EC2 instances behind an Application Load Balancer (ALB). Each instance keeps a user's temporary session context only in memory. Requests from the same browser can use different TCP connections, and many users access the application through a corporate proxy with one public IP address. Browsers accept cookies.

The developer must keep each browser's requests on its initially selected instance while that instance remains healthy, without treating all browsers behind the proxy as one client. Application code cannot be changed, and losing session context if an instance fails is acceptable.

Which configuration best meets these requirements?

Show answerCORRECT ANSWER

Enable duration-based stickiness on the ALB target group, with a load-balancer-generated cookie duration that covers the application's session lifetime.

ALB duration-based stickiness uses a load-balancer-generated browser cookie to maintain affinity to a healthy target across requests and TCP connections. It requires no application code changes and can distinguish browsers sharing a public IP address. It does not replicate in-memory session data, which is consistent with the acceptable loss on instance failure.

ANS-C01

A company connects its data center to a VPC through AWS Direct Connect. Employees access a private HTTPS application through a corporate proxy that presents the same source IP address for all employees. Requests for /legacy/* must reach several on-premises servers, and requests for /api/* must reach Amazon EC2 instances. All backend addresses are private and reachable from the VPC.

The legacy application stores session state in each server's memory and does not issue application cookies. While a server remains healthy, each browser's subsequent legacy requests must return to that server. Different browsers must be distributed across the legacy servers rather than all being assigned to one server because they share a proxy. Browsers accept load-balancer cookies. TLS can terminate at the load balancer, but the company cannot modify the application or proxy.

Which configuration meets these requirements?

Show answerCORRECT ANSWER

Deploy an internal Application Load Balancer with path-based listener rules. Register the on-premises addresses in an IP target group for /legacy/* and use a separate target group for /api/*. Enable duration-based, load-balancer-generated cookie stickiness on the legacy target group.

An Application Load Balancer supports path-based routing and can forward requests to reachable on-premises private IP targets over Direct Connect. Duration-based target-group stickiness uses a load-balancer-generated cookie to keep a browser assigned to a healthy target. It distinguishes browsers behind the shared proxy without requiring the application to generate cookies or maintain shared session state.

SAA-C03

A manufacturer runs a device-management service on Amazon EC2 instances behind a Network Load Balancer (NLB) with a TCP listener. Devices use a proprietary binary protocol, not HTTP. Each device opens separate TCP connections for commands and telemetry. The instance that accepts a device's initial connection stores temporary device context in memory, and subsequent connections must reach that same instance while it remains healthy.

Each device has its own unique, stable source IPv4 address as observed by the NLB. Target registration and target health remain unchanged during an active device session. The manufacturer cannot modify the devices or the service software.

Which configuration change will ensure that each device's connections reach the same instance?

Show answerCORRECT ANSWER

Enable stickiness on the NLB target group using the source_ip stickiness type.

NLB source-IP stickiness directs connections from the same source IP address to the same target. The devices have unique, stable source addresses, so this preserves each device's backend assignment across separate TCP connections while the target pool and health remain unchanged.

Practice Session Affinity in a full exam

Questions on Session Affinity in each practice exam, with every answer option and an explanation for each:

Start a free practice exam

What is Session Affinity

2 min read

Session affinity changes how a load balancer distributes successive requests. Without affinity, each request can be assigned independently using the configured routing algorithm. With affinity, the load balancer identifies a returning client or session and attempts to reuse its previous backend assignment. This is useful when an application stores information such as a shopping cart, login workflow, or temporary processing context in a server's memory.

Consider a legacy web application running on three virtual machines. A user logs in through the first machine, which stores the user's session locally. If the next request reaches the second machine, the application might treat the user as unauthenticated. Cookie-based affinity can keep that browser's requests on the first machine. However, the routing cookie normally identifies an assignment; it does not copy the application's session data to other machines.

Common implementations use cookies or the client's source IP address. Cookie-based affinity works at the HTTP application layer and can distinguish browsers that share a public IP address. Source-IP affinity can support traffic where HTTP cookies are unavailable, but many users behind a corporate proxy or network address translation gateway may appear to be one client. Changing client IP addresses can also change the backend assignment. Choose the load-balancing layer and affinity mechanism based on the application's protocol and client behavior.

In AWS, an Application Load Balancer supports target-group stickiness using load-balancer-generated cookies or application-based cookies. A Network Load Balancer offers source-IP stickiness for supported configurations rather than HTTP-cookie stickiness. These are different mechanisms, not interchangeable settings. Maintaining one TCP connection on one target is also different from maintaining affinity across multiple connections. Similarly, an established WebSocket connection naturally remains with its selected target; reconnecting is a separate routing event.

Affinity introduces a trade-off between continuity and balanced resource utilization. A few busy sessions can overload particular targets while others have spare capacity. Adding servers does not immediately redistribute existing sticky sessions, so scaling out may provide less immediate relief than expected. Affinity duration should reflect the actual need, and engineers should monitor per-target request counts, latency, resource utilization, and errors rather than relying only on fleet-wide averages.

Affinity is not a high-availability mechanism. When a target fails or is removed, subsequent requests may be assigned elsewhere, and locally stored session state may be lost. Health checks, multi-zone deployment, graceful deregistration, and application recovery behavior remain necessary. Connection draining can help active requests finish during planned removal, but it does not transfer in-memory sessions.

For scalable, loosely coupled designs, prefer interchangeable application targets with session data in a shared, appropriately resilient store, or use securely designed client-held session state. For example, storing a cart in a database allows any healthy application instance to serve the next request. In architecture questions, choose affinity when requests explicitly need the same backend, but do not mistake it for session replication, durable storage, or automatic fault tolerance.

Learn Session Affinity in lessons

2 short lessons on Session Affinity in 1 certification course.

Open the lessons