HAProxy ACL based on percentage

We use F5 in production environments, but to test a functional setup of A/B testing, we used HAProxy 1.5-dev21. We have a single domain, and would like to forward requests to one of two backends based on a cookie value.

For example, www.mydomain.com would go to one of two backend server pools based on the cookie value.

This works great when the cookie is already set.

When the cookie is absent, however, we set use_backend as a new backend that contains all servers from both pool A and B. As such, the first request will go to one or the other, and subsequent requests will be be persisted to that site, which wrote its own specific cookie value in the response (“is_new” or “is_old”). For the two different experiences, we used roundrobin, as we simply wanted 50/50.

Below is the configuration in HAProxy:

frontend ft_web
  bind 0.0.0.0:80
  mode http
  acl cookie_set hdr(cookie) aName
  acl is_old hdr(cookie) aName=old
  acl is_new hdr(cookie) aName=new
  use_backend bk_web1 if is_new
  use_backend bk_web2 if is_old
  use_backend bk_web if !cookie_set

backend bk_web
  mode http
  balance roundrobin
  cookie JSESSIONID prefix
  server serv1 1.1.1.1:7003 cookie a1
  server serv2 1.1.1.2:7003 cookie b1
  server serv3 2.2.2.1:7003 cookie c1
  server serv4 2.2.2.2:7003 cookie d1

backend bk_web1
  mode http
  balance roundrobin
  cookie JSESSIONID prefix
  server serv1 1.1.1.1:7003 cookie a1
  server serv2 1.1.1.2:7003 cookie b1

backend bk_web2
  mode http
  balance roundrobin
  cookie JSESSIONID prefix
  server serv3 2.2.2.1:7003 cookie c1
  server serv4 2.2.2.2:7003 cookie d1

2 comments for “HAProxy ACL based on percentage

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.