Use HAProxy to Load Balance by Smartphone or Tablet

Use HAProxy to Load Balance by Smartphone or Tablet

HAProxy provides flexible and scalable software-based load balancing. And now with the WURFL InFuze for HAProxy module, you can quickly inject WURFL’s device intelligence into HAProxy’s load balancing logic.

HAProxy Load Balancing Based on Smartphone, Tablet, or Other Device Form Factor

HAProxy load balance by device
WURFL provides a number of options for load balancing criteria within HAProxy. ¬†We can envision load balancing by operating system, browser type, or the price of the smartphone. In our following example, we load balance based on the device’s form factor.

We separate traffic into three streams: Smartphone, Tablets, and Other (e.g. Desktop, Smart TV, etc.). HAProxy can redirect those streams to specific servers.

# This is an example of how to configure HAProxy to be used with WURFL Device Detection module.
#
# HAProxy needs to be compiled with support for this. 
#
# This configuration performs a redirection to different backends based on wurfl detection
# In this scenario we have three backends:
# - One which serves SMARTPHONES devices (SmartphoneServer - 192.168.140.20)
# - One for TABLET devices (TabletServer - 192.168.140.30) 
# - One (GeneralPurposeServer - 192.168.140.10) for any other type of clients (desktop, smartTV,  etc ... )
#

global
        stats socket /run/haproxy.sock mode 0600 level admin

        # The WURFL data file
        wurfl-data-file         /usr/share/wurfl/wurfl-eval.xml

        # WURFL engine target: one of the following (default is performance)
        wurfl-engine-mode       accuracy

        # WURFL cache: one of the following
        ## double LRU cache
        wurfl-cache-size        100000,30000

        wurfl-information-list-separator ,

        # To properly redirect the client request we need to ask WURFL for the following capabilities:
        wurfl-information-list is_smartphone is_tablet

        log 127.0.0.1 local0

defaults
        mode http
        timeout connect         30s
        timeout client          30s
        timeout server          30s

frontend TheFrontend
        bind                    192.168.200.1:80
        default_backend         GeneralPurposeBackend

        # Here we fill two new headers with the capabilities value obtained by WURFL 
        http-request set-header X-Wurfl-IsSmartphone %[wurfl-get(is_smartphone)]
        http-request set-header X-Wurfl-IsTablet %[wurfl-get(is_tablet)]

        # Here we check the values obtained by WURFL and redirect the request to the proper backend
        acl isSmartphone hdr(X-Wurfl-IsSmartphone) "true"
        acl isTablet hdr(X-Wurfl-IsTablet) "true"
        use_backend SmartphoneBackend if isSmartphone
        use_backend TabletBackend if isTablet

backend GeneralPurposeBackend
        server                  GeneralPurposeServer 192.168.140.10
backend SmartphoneBackend
        server                  SmartphoneServer 192.168.140.20
backend TabletBackend
        server                  TabletServer 192.168.140.30