title: How to handle multiple update requests on load-balanced servers
body: <p>I have a client application which is adding items to a cart. An "add" operation is firing an update request via a HTTP REST call to a remote endpoint. Please note: this request contains <strong>the complete cart</strong> as a whole, not solely the item being added. This request is then load-balanced between two servers using a round-robin alogrithm.</p>
  
  <p>The problem I'm trying to tackle is that the client does not wait for the "add" request to return before to launch another "add" request, if the user does so. This is good from an end-user perspective because the user doesn't have to wait. But this is a nightmare from a server perspective : you can't be sure in which order the requests will be processed because of the load-balancer. </p>
  
  <p>Here is an example:</p>
  
  <ul>
  <li>The user adds the item #1 to the cart. The request A is sent.</li>
  <li>The user adds the item #2 to the cart. The request B is sent. Please note that the request B is sent before the request A has received a response.</li>
  <li>The request A is load-balanced on server 1, and the request B is load-balanced on server 2</li>
  <li>For some reasons, the server 1 is slower than server 2, so that the request B is processed first => the cart has item #1 and #2</li>
  <li>The server 1 processes the request A => the cart has item #1 only (reminder: each update request contains the wole cart)</li>
  </ul>
  
  <p>I'm not so sure about how to handle this. So far, the possible solutions I can think of are:</p>
  
  <ul>
  <li>Send a timestamp with the request and keep the timestamp in database. Before to update the cart, check that the timestamp of the request is higher. Otherwise drop the request. But this rely heavily on client-side behaviour.</li>
  <li>Set a version number on the cart itself, and increment it at each update. This would force the client to wait for the response before to send another update. Not very satisfactory from an end-user perspective, because he has to wait.</li>
  <li>Set a "session affinity" on the load-balancer so that the requests from a particular client are piped to the same server each time. The problem is that it affects the balance on the server load.</li>
  </ul>
  
  <p>I'm pretty sure I'm not the first one to face such issue, but I failed at finding a similar case, quite surprisingly. I must probably have asked the wrong question or keywords! Anyway, I'd be very interested to share your thoughts and experience on this problem.</p>