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 -
# - One for TABLET devices (TabletServer - 
# - One (GeneralPurposeServer - for any other type of clients (desktop, smartTV,  etc ... )

        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 local0

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

frontend TheFrontend
        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
backend SmartphoneBackend
        server                  SmartphoneServer
backend TabletBackend
        server                  TabletServer