Skip to Content

Sticky sessions and cross-zone load balancing

Keeping a user on the same target with cookies, and how cross-zone load balancing spreads traffic evenly across AZs.

Sticky sessions (session affinity)

Normally a load balancer may send each request from a user to a different target. Sticky sessions make the same client always reach the same target, for a chosen period.

The ALB reads each client's cookie, so clients 1 and 2 always reach instance A and client 3 always reaches instance B The ALB reads each client's cookie, so clients 1 and 2 always reach instance A and client 3 always reaches instance B

Each client keeps going to the same instance.
  • Works with Application, Network and Classic Load Balancers. ALB and CLB use a cookie with a configurable expiry; NLB sticks by the client's source IP.
  • Use case: an app that keeps session data (a shopping cart, a login) in the server's memory, so the user doesn't lose it.
  • Downside: load can become uneven: a few heavy users may pin themselves to one instance, and scaling in can still cut sessions. Storing sessions outside the servers (ElastiCache, DynamoDB) is usually better, and allows stickiness to be turned off.
  • Duration-based cookie: generated by the load balancer, named AWSALB (AWSELB on a CLB). Its duration is configurable, from 1 second to 7 days.
  • Application-based cookie:
    • a custom cookie generated by the application (the target). It can carry any attributes the app needs. Its name is set per target group, and it mustn't use the reserved names AWSALB, AWSALBAPP or AWSALBTG;
    • plus an application cookie generated by the load balancer, named AWSALBAPP, which tracks the stickiness for the custom cookie.

Cross-zone load balancing

A load balancer has a node in each AZ, and DNS sends roughly the same share of clients to each node. The question is whether a node may send traffic to targets in other AZs.

With 2 instances in AZ 1 and 8 in AZ 2, each node gets 50 percent; with cross-zone on, each instance gets 10 percent; with it off, AZ 1 instances get 25 percent and AZ 2 instances 6.25 percent With 2 instances in AZ 1 and 8 in AZ 2, each node gets 50 percent; with cross-zone on, each instance gets 10 percent; with it off, AZ 1 instances get 25 percent and AZ 2 instances 6.25 percent

Same load balancer, same 10 instances, two results.

In this example AZ 1 has 2 instances and AZ 2 has 8. Each load balancer node gets 50% of the traffic.

  • With cross-zone load balancing, each node spreads its share across all 10 instances, so each gets 10%. The load is even.
  • Without it, each node uses only its own AZ. The 2 instances in AZ 1 share 50%, 25% each, while the 8 in AZ 2 get 6.25% each. The AZ 1 instances work four times harder.

Defaults per type

  • Application Load Balancer: always on at the load balancer; it can be turned off per target group. No charge for data between AZs.
  • Network and Gateway Load Balancers: off by default. It can be turned on, but data sent between AZs is charged.
  • Classic Load Balancer: off by default when created with the API or CLI (on when created in the console); no charge between AZs.

Keeping the same number of instances in every AZ (which Auto Scaling groups try to do) avoids the imbalance even without cross-zone.

Exam tips

  • "Users must keep their session on the same instance" → sticky sessions (ALB cookie, or source IP on an NLB).
  • "Load is uneven since enabling stickiness" → that's the expected side effect; store sessions in ElastiCache or DynamoDB instead.
  • "Uneven instance counts across AZs cause overloaded instances" → turn on cross-zone load balancing.
  • "Cross-zone on an NLB costs money"; on an ALB it's free and on by default.

Common traps

  • "Sticky sessions improve load distribution." They make it worse; they trade balance for session continuity.
  • "Cross-zone load balancing is off by default on ALB." It's on for ALB, off for NLB and GWLB.

Hands-on lab: turn on stickiness

Goal: see an ALB send every request from your browser to one instance once stickiness is on.

Costs money

Uses the ALB and two instances from the Elastic Load Balancing lab (rebuild them if you deleted them), under $0.10 an hour.

Check the default

Open the ALB's DNS name and refresh a few times: the hostname alternates.

Turn on stickiness

Open target group saa-tg → Attributes → Edit → turn on Stickiness, type Load balancer generated cookie, duration 1 minute. Save.

Test it

Refresh many times: the hostname stays the same. In your browser's developer tools (Application/Storage → Cookies), you'll see the AWSALB cookie. Wait over a minute, or open a private window, and you may land on the other instance.

See the cross-zone setting

Still in the target group's Attributes, note Cross-zone load balancing: Inherit settings from load balancer, which for an ALB means on.

Expected result: with stickiness on, your browser keeps reaching the same instance thanks to the AWSALB cookie.

Clean up: delete saa-alb, the target group, both instances and saa-alb-sg, unless you continue to the next lab.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt