Cache-Aside pattern

Cache mostly static data

Published on Tuesday, November 24, 2020

Load frequently accessed data into a cache from a data store to improve the application's performance.


The application can use a cache to improve its performance of the application. However, it is impractical (or something not possible) to cache everything or to always have up to date cache.


Cache everything - is a good solution for small, mostly static data. It is easy to write and maintain - always cache everything and invalidate the entire cache when needed. However, this approach can only work for a small set of problems. We almost always have larger groups of data, some of which are seldom used and others used frequently. The cache-Aside pattern can be a solution for this caching needs.

The cache-Aside pattern is simple - when we need specific data, we first try to get it from the cache. If data is in the cache, get it from the cache; if data is not in the cache, get it from the data source, add it to the cache and then return it. If the application updates the data, the cache needs to be invalidated. After invalidation, the cache can be loaded again or not, depending on the needs.


  • Lifetime - As with most caches, cached data should have an expiration policy that invalidates data and removes it from the cache. How long the data should be cached depends on how soon the data becomes stale.
  • Evicting data - Most caches have limited size available and will evict data. How the data is evicted can vary. Usually, the first data to be evicted is the last one used, but this can and should be configured based on data usage. Depending on the data usage, it is much more practical to keep in memory the most frequently used data for more extended periods (let's say the last 24 hours) than to keep just the newest accessed data, which can be used infrequently.
  • Priming the cache - Many applications prepopulate caches with frequently used data as a startup process. The Cache-Aside pattern takes control after initial loading.
  • Consistency - Cache-Aside pattern does not guarantee the latest data in the cache, but we should take every precaution to make it so.