pythontwistedhaproxy

How does HAProxy achieves its speed?


How does HAProxy avoids request time overhead when doing the load balancing?

I tested HAProxy and for fun compared it to a simple port forwarder written in Twisted (Python). In my preliminary tests, making a HTTP request through an HAProxy load balancer adds no overhead [1] in request time compared to making the HTTP request directly to the backend server. Whereas my own python script adds ~3x overhead in response time.

Now my scripts is written in Python and HAProxy in C, so a priori, HAProxy has an advantage of avoiding the call overhead (in going from Python code to syscalls), that the Python code experiences. But can that account for the big discrepancy in performance, or does HAProxy utilize some OS tricks to improve the performace even further? I tried profiling my Python code, but it didn't reveal any hotspots in the Python code, so my guess is that it spends most of the time in syscalls that are not accounted for in the profiling.

[1]: As reported by ab, with 100 concurrent connections and 10,000 total requests. Mean time for HAProxy is 37ms and for my Python script it is 128ms.

Setup

The setup is a TCP load balancer with two backend nodejs servers, just serving static text. On purpose I wanted to test TCP load balancing, and the test protocol then became HTTP. All three machines are virtual hosts from Digital Ocean, single threaded, 512MB Ram, 1 core. The Python script can be seen here and my haproxy.cfg can be found here


Solution

  • Turns out that the HAProxy website already covers this area (my mistake of overlooking it). The answer is basically a lot of low level optimizations. Directly copied from the HAProxy website:

    HAProxy involves several techniques commonly found in Operating Systems architectures to achieve the absolute maximal performance :