Backend load balancing delegation in order to support sticky sessions

Bayron Guevara bayron.guevara at
Mon Sep 27 20:07:02 CEST 2010

Hello community,

Currently I’ve installed Varnish 2.0.6 and I'm using a cluster of Web
Servers. In order to find a work around the session clustering issue,
I thought implement session stickiness as part of the load balancing
configuration, so all subsequent requests from the same user would go
to the same webserver and so ensure the session information
consistency. But as I know, Varnish don’t have integrated support to
Stickiness, and therefore I have to delegate this function to a Load
Balancer. Through this infrastructure the client requests would follow
the below path:

User Agent --> Load Balancer Public VIP --> One of the Varnish Servers
--> (IF cache hit --> deliver cached content to Client ) ELSE ( Load
Balancer Private VIP --> One of the Web Servers)

To support sticky sessions my Load Balancer uses the Arrow Point
cookie mechanism which, to be brief, add a cookie named ARPT that
identified the user session. And according to the above path, Varnish
sends requests and receive responses from the Load Balancer private
VIP which needs that the cookie in question be passed transparently to
and from the client. So, Varnish should take care of this cookie in
two ways: the Cookie header in the incoming requests and the
Set-Cookie in the responses from the Load Balancer.

What do you recommend me to do?. I have ideas but I don’t sure. For
example, to extract the desired cookie from Cookie header I’m doing
the following:

### Code Start ####
vcl_receive {
if (req.http.Cookie ~ "ARPT=") {
      set req.http.LB-Cookie = regsub(req.http.Cookie,
".*(ARPT=[^;]+).*", "\1");
unset req.http.Cookie;

sub vcl_miss {
       set bereq.http.Cookie = req.http.LB-Cookie;

sub vcl_pass {
       set bereq.http.Cookie = req.http.LB-Cookie;
###  Code End ####

To keep it in the http response, I guess:

###  Code Start ###
sub vcl_fetch {
if (obj.http.Set-Cookie ~ "ARPT=") {
               #Save Set-Cookie, so it can be used in vcl_deliver
               set obj.http.X-Set-Cookie = obj.http.Set-Cookie;

sub vcl_deliver {
if (obj.hits > 0) {
       } else {
       #It was a cache miss
               #Set back Set-Cookie header in case it's set
               if (resp.http.X-Set-Cookie){
                       set resp.http.Set-Cookie = resp.http.X-Set-Cookie;
                       unset resp.http.X-Set-Cookie;
### Code End ###

I don’t know how to extract a cookie from a Set-Cookie header, but I
tried with this  code:
       obj.http.LB-Cookie = regsub(obj.http.Set-Cookie,
“.*(ARPT=[^;]+(;[^=]+=[^;]+)*).*”, “\1”);
Nevertheless, it appears don’t work fine.

Am I in the right way? Is there a better solution?

Thank you in advance.

Bayron Guevara
Web Programmer
bayron.guevara at

More information about the varnish-misc mailing list