Classic symptom after putting a load balancer in front of two PHP servers: users get logged out at random, a cart empties itself, a multi-step form forgets what you typed. Nothing is “broken” on either server. The problem is where PHP keeps sessions.
Below is a minimal way to reproduce it, the explanation, and the fix with HAProxy’s cookie-based persistence (sticky sessions).
Reproduce it in one minute
Two Apache + PHP backends (apache1, apache2) behind HAProxy with balance roundrobin. On both servers, create /var/www/html/session.php:
<?php
session_start();
$_SESSION['visits'] = ($_SESSION['visits'] ?? 0) + 1;
echo "<h1>Server: " . gethostname() . "</h1>";
echo "<p>Visits in this session: " . $_SESSION['visits'] . "</p>";
?>
Now request it four times. curl does not keep cookies by itself, so use a cookie jar (-c to save, -b to send) to behave like a browser:
$ rm -f cookies.txt
$ for i in 1 2 3 4; do curl -s -c cookies.txt -b cookies.txt http://www.example.org/session.php; echo; done
<h1>Server: apache2</h1><p>Visits in this session: 1</p>
<h1>Server: apache1</h1><p>Visits in this session: 1</p>
<h1>Server: apache2</h1><p>Visits in this session: 2</p>
<h1>Server: apache1</h1><p>Visits in this session: 2</p>
The counter should go 1, 2, 3, 4. It goes 1, 1, 2, 2.
Why: the session lives on one server’s disk
By default PHP stores session data in files on the server that created them (on Debian, /var/lib/php/sessions). The browser only stores the session ID, in the PHPSESSID cookie.
So with round robin:
- Request 1 goes to
apache2. It creates session filesess_abcwithvisits = 1. - Request 2 goes to
apache1, carryingPHPSESSID=abc.apache1has no file calledsess_abc, so for it this is a new session:visits = 1. - Request 3 goes back to
apache2, which finds its own file:visits = 2.
The session is split in two halves, one per server, and each server only counts the requests it saw.
There are two ways out:
- Shared session storage: keep sessions in Redis, Memcached or a database that both servers use. This is the robust fix for production, because any server can serve any request.
- Sticky sessions: make the load balancer always send a given client to the same server. No application changes. This is what we do here.
The fix: cookie-based persistence in HAProxy
HAProxy can insert its own cookie saying which server handled the client, and route by it afterwards. Two changes in the backend:
backend web_servers
cookie SERVERID insert indirect nocache
option forwardfor
balance roundrobin
option httpchk GET /
server apache1 192.168.100.100:80 check cookie s1
server apache2 192.168.100.101:80 check cookie s2
cookie SERVERID insert: HAProxy adds aSERVERIDcookie to the response.indirect: HAProxy removes its cookie before passing the request to the backend, so your application never sees it.nocache: marks responses carrying the cookie as non-cacheable, so a shared cache can’t hand one user’sSERVERIDto everybody.cookie s1/cookie s2on eachserverline: the value written in the cookie for that server. Each must be unique.
Check and reload:
$ sudo haproxy -c -f /etc/haproxy/haproxy.cfg
$ sudo systemctl reload haproxy
Test again
Start with an empty cookie jar so the first request is balanced normally:
$ rm -f cookies.txt
$ for i in 1 2 3 4; do curl -s -c cookies.txt -b cookies.txt http://www.example.org/session.php; echo; done
<h1>Server: apache1</h1><p>Visits in this session: 1</p>
<h1>Server: apache1</h1><p>Visits in this session: 2</p>
<h1>Server: apache1</h1><p>Visits in this session: 3</p>
<h1>Server: apache1</h1><p>Visits in this session: 4</p>
Same server every time, counter goes up by one.
The response headers show both cookies doing their jobs:
$ curl -sI http://www.example.org/session.php | grep -i set-cookie
set-cookie: PHPSESSID=d9e41cf40a10dd99038dba98420e5092; path=/
set-cookie: SERVERID=s2; path=/
PHPSESSIDis created by PHP and identifies the session.SERVERIDis inserted by HAProxy and identifies the server that holds it.
Common gotchas
Testing with plain curl and thinking it doesn’t work. Without -c/-b, curl never sends cookies back, so every request looks like a new client and gets balanced again. Browsers keep cookies; curl doesn’t unless you tell it to.
Duplicate cookie values. If two server lines have the same cookie value, HAProxy can’t tell them apart. Make each one unique.
A server goes down. If apache1 fails its health check, clients pinned to s1 are sent to another server, and their session (stored on apache1) is gone. Sticky sessions keep users on one server; they don’t make sessions survive a server failure. For that, you need shared session storage.
Uneven load. Persistence means a busy client stays on the same server. With many users it averages out; with a few heavy users it may not.
Summary
- Round robin + PHP’s default file sessions = each server sees only part of the session.
- Quick fix:
cookie SERVERID insert indirect nocachein the backend and a uniquecookie <value>on eachserverline. - Test with a cookie jar (
curl -c cookies.txt -b cookies.txt). - For high availability, move sessions to shared storage (Redis, Memcached or a database).