title: Haproxy in front of varnish or the other way round?
body: <p>I can imagine two setups:</p>
  
  <p><strong>Load-balance then cache</strong></p>
  
  <pre><code>                          +-- Cache server #1 (varnish) -- App server #1
                           /
  Load Balancer (haproxy)-+---- Cache server #2 (varnish) -- App server #2
                           \
                            +-- Cache server #3 (varnish) -- App server #3
  </code></pre>
  
  <p><strong>Cache then load-balance</strong></p>
  
  <pre><code>                                                       +-- App server #1
                                                        /
  Cache Server (varnish) --- Load Balancer (haproxy) --+---- App server #2
                                                        \
                                                         +-- App server #3
  </code></pre>
  
  <p>The problem with the first setup is that there are multiple caches, which wastes a lot of memory and makes invalidating cache more complicated.</p>
  
  <p>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)?</p>
  
  <p>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?</p>