Cache aside strategy : delete or update the cache after the write?
Asked Answered
V

1

3

I am trying to understand something about the the cache aside strategy. The data is read from the cache if found (cache hit), and if not found (cache miss) read from the Database + updated in the cache.

On writes, it is put in the main database, and then the cache should be updated by :

  • A : deleting the corresponding entry in the cache (so the next read will encounter a cache miss and upadte the cache)
  • or B : updating the corresponding entry in the cache (so the next read will encounter a cache hit if it happens before the cache entry TTL)

I don't understand what are the pros and cons of solutions A and B. With redis, both seem pretty simple to implement, but I guess there are other considerations.

Vizier answered 20/6, 2023 at 20:1 Comment(0)
B
4

Both solutions try to make the database changes be synced to cache ASAP, i.e. make the database and cache be consistent as much as possible, so that application code can read the (possible) latest data.

The following are cons for each solution.

Solution A: deleting item in cache

If the item is updated very frequently, this solution deletes the cache entry all the time, and you'll get lots of cache miss. Most requests hit the database, and make the cache useless.

Solution B: Updating item in cache

This solution might insert infrequent items into the cache, and lower the cache hit ratio. When an item is updated in database, you've no idea whether it's a hot item. If it's a cold one, and you insert it into cache, a hot item might be evicted from the cache (since the cache size is limited).

Cache inconsistent problem still exists

Although both solutions try to make the cache be consistent as much as possible. They cannot guarantee that. Take the following case for example.

  1. client A reads the cache, and found that it does not exist.
  2. client A reads the database and get the value: old-value.
  3. client B updates the database with a new value: new-value.
  4. client B updates cache with new-value or deletes entry in cache (although it might not exist in cache).
  5. client A update the cache with old-value.

In this case, cache still holds old value.

Solution C: make friends with inconsistency

Since you're using cache, you cannot avoid inconsistency with database. So in most cases, we don't need to update/delete cache items when updating database. Check this and this for more detail.

Beanie answered 21/6, 2023 at 2:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.