title: Making sure (distributed) caches only store the latest value in a distributed system
body: <p>Let's say I want to use a built-in solution such as Redis or Memcached to cache database rows (as an example), to avoid recurrent costly trips to the database.</p>
  <p>For the sake of the argument, let's assume I have a TABLE(id, x, y) and that I want to cache all rows so I never have to read directly from the database.</p>
  <p>Questions:</p>
  <ol>
  <li>Consider the following case: NodeA tries to update a given row's field <code>x</code> while NodeB tries to update <code>y</code>, then both simultaneously try to update the cache line. If they try to &quot;manually&quot; update the field they just changed to the row in the cache, if we follow the typical last-write-wins, one of the fields is going to be discarded, which is catastrophic. This makes me think I need to always fill the cache's rows with a full row read from the database.</li>
  <li>But this by itself won't necessarily help me. If NodeA writes to <code>x</code> and loads the entire row in memory and then NodeB writes to <code>y</code> and reads the entire row in memory, if NodeB writes to the cache before NodeA then NodeB's changes will be overwritten! This makes me believe I need to always somehow version the rows both in the DB and in the cache. Is this the case? Memcached seems to have a compare and set primitive, but I see no such thing in Redis.</li>
  <li>Even if 1. and 2. are not an issue, I still need to guarantee that my write / read has read-after-write consistency, otherwise it may happen that what I'm reading and intending to put in the cache is not necessarily the most up-to-date version. If that's the case, how can I make sure of this? By requiring w + r &gt; n?</li>
  </ol>
  <p>This seems to be a very common use-case, I'd guess it's pretty much a solved problem. What can I try to resolve this?</p>