title: Chaining haproxys with optimal connection re-use and keep-alive
body: <p>I need to understand the optimal connection configurations for chaining 2 haproxys to provide maximum connection reuse between the edge proxy, the app proxy and the app containers.  </p>
  
  <p>Where should I be correctly using <code>http-server-close</code>,  <code>http-reuse</code> or <code>keep-alive</code> settings?</p>
  
  <p>I have haproxy instances sitting in edge regions which proxy over private networks to a central data center where another haproxy provides the application routing based on url paths to application containers.  All application containers are completely stateless rest servers.</p>
  
  <p>The setup is as follows:</p>
  
  <pre><code>haproxy-edge(s) -&gt; haproxy-app(s) -&gt; app-component(s)
  </code></pre>
  
  <p>Each haproxy-edge serves thousands of concurrent browser and api connections and does ssl offloading etc.  </p>
  
  <p>haproxy-app can only be reached via connections from haproxy-edge and does path routing, sets consistent response headers etc.</p>
  
  <p>haproxy-edge connection settings</p>
  
  <pre><code>defaults
    mode http
    option redispatch
    option httplog
    option dontlognull
    option log-health-checks
    option http-ignore-probes
    option http-server-close
    timeout connect 5s
    timeout client 15s
    timeout server 300s
    timeout http-keep-alive 4s
    timeout http-request 10s
    timeout tunnel 2m
    timeout client-fin 1s
    timeout server-fin 1s
    ...
  backend ...
  # Routes to haproxy-app. No backend specific connection settings at all 
  
  </code></pre>
  
  <p>haproxy-app connection settings</p>
  
  <pre><code>defaults
    mode http
    balance roundrobin
    option redispatch
    option httplog
    option dontlognull
    option http-ignore-probes
    option http-server-close
    timeout connect 5s
    timeout client 15s
    timeout server 300s
    #timeout http-keep-alive 4s
    timeout http-request 10s
    timeout tunnel 2m
    timeout client-fin 1s
    timeout server-fin 1s
  frontend http-in
    ...
    tcp-request inspect-delay 5s
    option http-server-close
    ...
  backend ...
  # Routes to app components. No backend specific connection settings at all
  </code></pre>
  
  <p>I see no reuse of connections in haproxy stats page and the number of sessions/connections seems to be similar at both haproxys, but would expect many on edge to fewer reused connections in haproxy-app.</p>