Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Sticky sessions missing behind a load balancer: how to fix it

OPS-089Medium impactPartlyBy FunnelGuardPublished Updated
On this page

What this issue is

A load balancer spreads visitors across two or more web servers. A session is the small record a server keeps about a visitor: what is in the cart, whether they are logged in, a form token. By default many apps (PHP, for example) save that record as a file on the server that handled the first request.

If the next request lands on a different server, that server has no record. The visitor looks like a stranger. Sticky sessions tell the load balancer to send the same visitor to the same server each time, usually with a cookie. When stickiness is off and sessions are not shared, you get this problem.

How a visitor loses their session
  1. Add to cartLoad balancer sends the request to Server A, which stores the cart
  2. View cartRequest goes to Server B, which has no session
  3. Cart is emptyVisitor sees an empty basket or a login page
  4. CheckoutVisitor gives up

Request 2 reaches a server that never saw the session.

Why it costs you leads or sales

The failure is random, so it is easy to miss. A visitor adds a product, clicks to the cart and finds it empty. Another submits a form and gets an invalid token or security error. Logged-in users are sent back to the login page mid-checkout.

With two servers, roughly half of multi-step journeys can hit a mismatch. The more servers you add, the worse it gets. Your own testing often looks fine because a quick click-through may stay on one server.

Share of checkouts that survive each stepExample numbers
Share of checkouts that survive each step (example)Single server: 100 %; 2 servers, no stickiness: 50 %; 4 servers, no stickiness: 25 %038771005025Single server2 servers, no stickin…4 servers, no stickin…%

Example: with two servers and no stickiness, about half of visitors switch server between steps. Numbers are illustrative.

How to check if your site has it

Find out whether sessions are tied to one server
  1. Confirm you have more than one server

    Ask your host or check your infrastructure. Look for a load balancer, several web nodes, or a CDN with multiple origins.

  2. Look at the session cookie

    In your browser developer tools, open Application > Cookies. Note the session cookie (for example PHPSESSID) and any balancer cookie such as AWSALB or a host-specific one. No balancer cookie suggests stickiness is off.

  3. Find where sessions are stored

    In PHP, check session.save_handler and session.save_path with phpinfo(). Values of files and a local path like /tmp mean each server keeps its own sessions.

  4. Reproduce with repeated requests

    Add an item to the cart, then reload the cart page 10 times. If it empties sometimes, requests are hitting different servers.

  5. Expose the server name

    Add a temporary response header with the hostname, then watch it change between requests while your session cookie stays the same.

Send repeated requests with the same cookie and compare serversbash
curl -s -c jar.txt -o /dev/null https://example.com/
for i in 1 2 3 4 5 6; do
  curl -s -b jar.txt -I https://example.com/cart | grep -i -E 'x-served-by|server|set-cookie'
done

How to fix it

There are two fixes. Shared session storage is the more robust one: any server can read any session. Sticky sessions are quicker but fail if a server drops out, because those visitors lose their sessions. Use shared storage where you can, and stickiness as a stopgap.

Fix by platform
  1. Use a shared object cache or database sessions

    WooCommerce stores carts in the database by default, so logged-out cart loss often comes from a per-server object cache or plugin that uses PHP files for sessions. Point any object cache plugin to one shared Redis or Memcached server.

  2. Check plugins that use PHP sessions

    Some form and membership plugins call session_start(). Switch PHP to shared session storage (see Custom code) or enable stickiness at the load balancer.

  3. Keep salts and keys identical

    Make sure wp-config.php has the same AUTH_KEY and related salts on every server, or logins will fail between nodes.

php.ini: shared sessions in Redistext
session.save_handler = redis
session.save_path = "tcp://10.0.0.5:6379"
Nginx upstream with IP-based stickiness (stopgap)nginx
upstream app {
    ip_hash;
    server 10.0.0.11:80;
    server 10.0.0.12:80;
}

How to confirm it's fixed

Confirmation checklist
  • Add to cart and reload the cart page 20 times: the cart never empties.
  • Log in, then browse several pages: you stay logged in.
  • Submit a form several times in a row: no token or security errors.
  • Stop one web server briefly: sessions still work if you use shared storage.
  • Repeat the tests from a private window and another network.
Before and after

Sessions on each server

  • Cart empties at random
  • Logins drop mid-checkout
  • Hard to reproduce

Shared sessions or stickiness

  • Same cart on every request
  • Logins last
  • Adding servers is safe

How to prevent it happening again

Prevention checklist
  • Keep session storage in the deployment checklist for every new node.
  • Use identical PHP and app configuration on all servers.
  • Never store uploads, carts or tokens on one server's local disk.
  • Test the full cart and login journey after adding or replacing a server.
  • Run a scheduled journey test so a bad rollout is noticed quickly.

How FunnelGuard handles this

FunnelGuard detects this issue only partly. It does not inspect your load balancer or session settings. What it does is run your real journeys (such as add to cart, checkout or a lead form) on a schedule from the EU or the US, and check each step. If a run hits the wrong server and the cart is empty or a form step fails, the step fails, FunnelGuard re-checks from a second region, and you get one alert with a screenshot and a likely cause.

Because the problem is random, one passing run does not prove it is fixed, and a single failure may be the only sign. FunnelGuard cannot tell you that stickiness is missing, which server answered, or where sessions are stored. Cover that by hand: check the balancer settings, the session save handler and the cookies, and run the repeated-request test above. The free plan includes 1 site and 2 journeys with daily checks, and every paid plan has a 3-day free trial.

Frequently asked questions

Questions people ask

Why does my cart empty randomly on my website?

If your site runs on more than one server, the load balancer may send a visitor to a server that does not hold their session. That server sees an empty cart. Store sessions in shared storage such as Redis or turn on sticky sessions.

What are sticky sessions in a load balancer?

Sticky sessions, or session affinity, make a load balancer send the same visitor to the same server for each request, usually using a cookie. This keeps server-side session data available. It is a quick fix but fails if that server goes down.

Are sticky sessions better than shared session storage?

Shared storage is usually more reliable, because any server can serve any visitor and a server failure does not lose sessions. Sticky sessions are easier to switch on but spread load unevenly and break when a node drops out.

How do I enable sticky sessions on AWS?

For an Application Load Balancer, open the target group, go to Attributes, edit Stickiness, turn it on and choose a load balancer generated cookie with a duration. Test afterwards to be sure the cookie is set.

Why do users get logged out when I add a second server?

Each server may store login sessions locally, so a request that reaches the other server finds nothing. Move sessions to shared storage and make sure keys and salts are identical on every server.

How can I tell if sessions are stored on the server?

In PHP, run phpinfo() and check session.save_handler. A value of files with a local path means each server holds its own sessions. Redis, Memcached or a database handler means they can be shared.

Can FunnelGuard detect missing sticky sessions?

Only partly. FunnelGuard runs your real journeys and flags steps that fail, such as an empty cart, but it does not inspect your load balancer or session storage. You still need to check those settings yourself.

Does FunnelGuard catch this?

Partly

FunnelGuard checks part of this.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.