Routing urls to different backends in haproxy


#1

Haproxy is a very advanced layer7 opensource loadbalancer software. If you want to decrease priority of some urls in your setup, you can define ACL rules to detect traffic type and route to different backends which can have different servers or maximum connection parameter like below:

frontend http
    bind *:80
    mode http
    acl url_slow path_beg /import/log
    acl url_slow path_beg /job/execute
    use_backend slow-backend if url_slow
    default_backend rails-backend
    
backend rails-backend
    option http-server-close
    balance roundrobin
    option httpchk GET /check HTTP/1.0
    server app1 10.0.0.1:80 check inter 5000 rise 2 fall 2 weight 50 maxconn 100
    server app2 10.0.0.2:80 check inter 5000 rise 2 fall 2 weight 50 maxconn 100
    
backend slow-backend
    option http-server-close
    balance roundrobin
    option httpchk GET /check HTTP/1.0
    server app1 10.0.0.1:80 check inter 5000 rise 2 fall 2 weight 5 maxconn 1
    server app2 10.0.0.2:80 check inter 5000 rise 2 fall 2 weight 5 maxconn 1
    server app3 10.0.0.3:80 check inter 5000 rise 2 fall 2 weight 90 maxconn 100

In this example, we define an acl named url_slow to select specific urls which begins with specific words. Haproxy has different selectors, we used path_begin here. You can define multiple acl like this.

After acl setup, you can select different backends for specific acl. We used slow-backend here for acl url_slow. With this setup, our application servers app1 and app2 used in both backends but limited with different maxconn parameters 1 and 100. With this setup when you have high traffic on the paths which takes much time to complete, it won’t be fill your main application servers app1 and app2


#2

This looks cool, have you explored what if in the event all the url_slow backend server are dead, can it fall back to rails_backend?