Is HAProxy still necessary when Varnish can act as a load balancer?

Solution 1:

Varnish does not support SSL

Running a site/service on SSL is a necessity for anything half serious, so thats one reason to use haproxy which will do SSL termination for you.

Solution 2:

While both HAProxy and Varnish can load-balance, only one of them is built for it.

You could just use Apache for your purposes as well since it can proxy and cache as well, but it's hardly an optimal solution.

In my opinion, you're best to use each product for what it's best at.

What I do is install both on the same box and configure Varnish to use HAProxy as its only backend.

The CDN gets pointed at HAPROXY, which then load-balances between Varnish and the backends, sending to Varnish by default unless the request comes from Varnish based on headers or source IP.

This way you can:

  • Easily bypass Varnish for upgrades or testing
  • Scale out either tier to more boxes as your load increases
  • Use each software for its core purpose

Solution 3:

As with most software that implements basic load-balancing, Varnish really only support round-robin and random balancing algorithms.

HAProxy, on the other hand, is built for load-balancing, and thus supports:

  • round robin (with weighting)
  • a static round robin approach
  • least connections
  • first server available
  • bucketing based on source
  • bucketing based on URI
  • bucketing based on a URL parameter
  • bucketing based on an HTTP header
  • bucketing based on a cookie

Whether or not Varnish's load-balancing is sufficient depends on your needs, but it certainly hasn't superseded HAProxy.