Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haproxy in front of varnish or the other way round?

I can imagine two setups:

Load-balance then cache

                          +-- Cache server #1 (varnish) -- App server #1                          / Load Balancer (haproxy)-+---- Cache server #2 (varnish) -- App server #2                          \                           +-- Cache server #3 (varnish) -- App server #3 

Cache then load-balance

                                                       +-- App server #1                                                       / Cache Server (varnish) --- Load Balancer (haproxy) --+---- App server #2                                                       \                                                        +-- App server #3 

The problem with the first setup is that there are multiple caches, which wastes a lot of memory and makes invalidating cache more complicated.

The problem with the second setup is that there might be a performance hit and two single points of failure (varnish and haproxy) instead of just one (haproxy)?

I'm tempted to go with the second setup because both haproxy and varnish are supposed to be fast and stable: what's your opinion?

like image 567
MiniQuark Avatar asked Mar 16 '13 10:03

MiniQuark


People also ask

Can varnish be used as a load balancer?

Varnish is a powerful HTTP load balancer (reverse proxy), which is also very good at caching. When running multiple TSDs, Varnish comes in handy to distribute the HTTP traffic across the TSDs.

What is round robin in HAProxy?

HAProxy Algorithms Round Robin: This algorithm is the most commonly implemented. It works by using each server behind the load balancer in turns, according to their weights. It's also probably the smoothest and most fair algorithm as the servers' processing time stays equally distributed.

What is frontend and backend in HAProxy?

When HAProxy Enterprise is used as a reverse proxy in front of your backend servers, a frontend section defines the IP addresses and ports that clients can connect to. You may add as many frontend sections as needed to expose various websites or applications to the internet.

What is backend in HAProxy?

HAProxy Enterprise frontend sections accept incoming connections that can then be forwarded to a pool of servers. The backend section is where those pools of servers that will service requests are defined.


1 Answers

I built a similar setup a few years back for a busy web application (only I did it with Squid instead of Varnish), and it worked out well.

I would recommend using your first setup (HAProxy -> Varnish) with two modifications:

  1. Add a secondary HAProxy server using keepalived and a shared virtual IP
  2. Use the balance uri load balancing algorithm to optimize cache hits

Pros:

  • Peace of mind with HAProxy (x2) and Varnish (x3) redundancy
  • Better hit rate efficiency on Varnish with HAProxy URI load balancing option
  • Better performance from the cache servers as they don't need to keep as much in memory
  • Invalidating cache is easier since the same URI will go to the same server every time

Cons:

  • URI balancing works well, but if a cache server goes down, your backend servers will get hit as the other cache server(s) that pick up the slack from the updated URI balancing hash will need to re-retrieve the cached data. Maybe not a big con, but I did have to keep that in mind for my system.
like image 137
Matt Beckman Avatar answered Sep 17 '22 13:09

Matt Beckman