What is the difference between Cache replacement vs Cache invalidation?

Technology CommunityCategory: CachingWhat is the difference between Cache replacement vs Cache invalidation?
VietMX Staff asked 3 years ago
  • Frequently, the cache has a fixed limited size. So, whenever you need to write in the cache (commonly, after a cache miss), you will need to determine if the data you retrieved from the slower source should or should not be written in the cache and, if the size limit was reached, what data would need to be removed from it. That process is called Cache replacement strategy (or policy). Some examples of Cache replacement strategies are:
    • Least recently used (LRU) – Discards the least recently used items first.
    • Least-frequently used (LFU) – Counts how often an item is needed. Those that are used least often are discarded first.
  • Cache invalidation is the process of determining if a piece of data in the cache should or should not be used to service subsequent requests. The most common strategies for cache invalidation are:
    • Expiration time, where the application knows how long the data will be valid. After this time, the data should be removed from the cache causing a “cache miss” in a subsequent request;
    • Freshness caching verification, where the application executes a lightweight procedure to determine if the data is still valid every time the data is retrieved. The downside of this alternative is that it produces some execution overhead;
    • Active application invalidation, where the application actively invalidates the data in the cache, normally when some state change is identified.