title: Configuration of Apache and Tomcat for load balancing
body: <p>I'm trying to setup Apache as a load balancer for 2 Tomcat instances with session affinity.</p>
  
  <p>The goal is to have the session stick to one server but to have next session (when it's changed by the backend server) to go to the next available server (let's say using round-robin algorithm for easier implementation). When using the "jvmRoute" in Tomcat and an equivalent "route" in Apache the actual value that does the routing is the route name which does not change and all requests are routed always to the same backend server for a single client.</p>
  
  <p>I found out so far that there's an chicken/egg problem when using just the JSESSIONID cookie. Let's consider the following setup:</p>
  
  <p>2 Tomcat servers listening on ports 8009 and 8010 (AJP13)
  1 Apache server with the following configuration</p>
  
  <pre><code>&lt;Proxy balancer://hello-cluster&gt;
      BalancerMember ajp://127.0.0.1:8009/hello
      BalancerMember ajp://127.0.0.1:8010/hello
  &lt;/Proxy&gt;
  
  ProxyPass /hello balancer://hello-cluster stickysession=JSESSIONID
  </code></pre>
  
  <p>And here's the scenario:</p>
  
  <ol>
  <li>The first request has no cookie so Apache selects the next available server in the load balancer to handle the request.</li>
  <li>The backend Tomcat server sets JSESSIONID but does not note the actual value being returned.</li>
  <li>The next request comes in, Apache notes that there's no backend server noted for the given JSESSIONID so it selects the next available, which in this case the other one as served the first request</li>
  <li>Tomcat notices that the value of JSESSIONID is invalid so it creates a new one.</li>
  <li>Apache does not take a note that the JSESSIONID has changed to pin it down to that backend server.</li>
  <li>Back to pt. 3</li>
  </ol>
  
  <p>Is there a way to convince Apache to note the value returned by Tomcat?</p>