5

I would like to load-balance my application servers, as well as cache the responses from them.

I've read an article dating back to 2012 on HAProxy's website, that was supposed to clear the confusion between HAProxy and Varnish:

http://blog.haproxy.com/2012/07/04/haproxy-and-varnish-comparison/

This article basically says that HAProxy is a reverse-proxy load balancer only (no cache), while Varnish is a reverse-proxy cache (no load balancing), hence they are complementary.

But today, I can see in Varnish docs that it does load balancing as well:

https://www.varnish-cache.org/trac/wiki/LoadBalancing

So I can only guess that this feature has been introduced since 2012, and that it now directly competes with HAProxy in terms of load balancing.

Now considering that I need a reverse-proxy cache + load balancer, is it true that I can use a single server with Varnish only, that will do both? Any limitations maybe, compared to a typical HAProxy <-> Varnish setup?

3 Answers 3

5

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.

3
  • Good point. However, in my case SSL termination would be done at CDN level, so this is not a problem for me. The caching functionality of Varnish is only required between the CDN and the backend servers to avoid hitting a server twice when expensive computations are involved (each edge location in a CDN has its own cache, so the backend server can be hit by each edge location for the same file). Anything else I should know, or should I be fine with Varnish for caching + load balancing?
    – BenMorel
    Jan 30, 2016 at 23:58
  • Are you saying the CDN will talk to your origin servers which run Varnish? If so I'd use SSL for the origin with haproxy(or other ssl terminator), then back to varnish.
    – Not Now
    Jan 31, 2016 at 0:27
  • The setup I have in mind is: CDN -> Varnish (single server) -> Pool of backend servers. The security of the data served is not an issue, so SSL can be terminated at the CDN and then everything thereafter can talk HTTP. Varnish would serve two purposes : load-balance across backend servers, and cache their output so that multiple CDN edge locations requesting the same object only trigger a single request on a backend server.
    – BenMorel
    Jan 31, 2016 at 0:40
3

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
2
  • While I understand the rationale behind using each software for what it was originally made for, I wonder what can be the risk of using Varnish for both uses? I might be wrong, but unless Varnish has a history of being buggy (which I doubt), I feel like using two softwares when one of them can do the whole job is overkill, and could even double the risk of failure?
    – BenMorel
    Jan 31, 2016 at 16:52
  • It really depends on your use case and whether the load-balancing functionality provided by Varnish fits your use case. If it does and you're happy with the way it works, then fill your boots. Otherwise start looking at other options. I don't think you have double the risk of failure if you've got a Dev environment where you can test new configs and versions before they go to prod. At least, there's no more risk than with any of your OS patches taking the single system off line.
    – GregL
    Feb 1, 2016 at 2:34
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .