mercredi 19 octobre 2016

Possibility of stale data in cache-aside pattern

Just to re-cape cache-aside pattern it defines following steps when fetching and updating data.

Fetching Item

  1. Return the item from cache if found in it.
  2. If not found in cache, read from data store.
  3. Put the read item in cache and return it.

Updating Item

  1. Write the item in data store.
  2. Remove the corresponding entry from cache.

This works perfectly in almost all cases, but it seems to fail in one theoretical scenario.

What if step 1 & 2 of updating item, happen between step 2 & 3 of fetching item. In other words, consider that initially data store had the value 'A' and it was not in cache. So when fetching item, we read 'A' from data store but before we put into the cache, the item was updated to 'B' in another thread (So 'B' was written in data store and tried to remove the entry from cache, which was not there at that time). Now when the fetching thread puts the item it read (i.e. 'A') in cache. So now 'A' will stay cached, and further fetches will return stale data, until item expires or updated again.

So am I missing something here, is my understanding of pattern is wrong. Or that the scenario is just practically impossible, and there is no need to worry about it.

Also I would like to know if some changes can be made in the pattern to avoid this problem.

Aucun commentaire:

Enregistrer un commentaire