haproxy load-balancing for PHP applications with sticky sessions

We like applications that are written with a shared-nothing approach: it greatly simplifies running multiple instances on multiple hosts and allows for simple, robust load-balancer configuration.

Recently, we had to deploy a PHP application that – in the last minute – turned out to use PHP sessions and thus required sticky sessions.

We haven’t used sticky sessions in a while and the amount of reading required to find the specific working setup was substantial, so we’ll repeat here what a post at networkinghowtos.com already figured out:

backend default
    appsession PHPSESSID len 64 timeout 3h request-learn prefix

As you can see there isn’t much magic to it – the haproxy manual has a good in-detail explanation of the appsession option. The biggest point of this option is that you do not have haproxy injecting another session identifier but simply piggybacks on the existing one that PHP determines. Also, this option combines nicely with “leastconn” balancing if your application only uses cookies on a few selected pages and many users do not trigger getting a session cookie.

2 thoughts on “haproxy load-balancing for PHP applications with sticky sessions”

  1. Haproxy is great. But I just stick with nginx because of it’s all around functionality. I could use it as plain web server or load balancer like haproxy.

    1. We explicitly prefer haproxy because we like to use each to for what it does best. haproxy has much better options and also logging for load balancing purposes than nginx does. We do love nginx for HTTP/S termination, though.

Leave a Reply

%d bloggers like this: