Caching Patterns & Strategies.

Play this article

Caching Aside Pattern

This is also known as lazy loading. In this approach, the cache is updated after the data is requested. In order to read data from the database, the cache is checked to determine whether the data is available.

If the data is available (cache hit), the cached data is returned and the response is issued to the caller. However, if the data is not available (cache miss), the database is queried for the data. The cache is then populated with the data retrieved from the database.

The main catch of this approach is that it offers a straightforward way of caching. An advantage of this pattern is that the cache only contains data that the application requests which helps keep the cache size small and less expensive.

A disadvantage of this pattern is that data is only loaded to the cache after a cache miss because initially there's no data stored in the cache. Also, there's overhead in the initial response times of queries because round-trips to the cache and database are required.

Write-Through Pattern

In this approach, the cache is updated immediately when the primary database is updated. The order of how the cache is populated is reversed. When the backend process updates the primary database, the data in the cache is also updated. This pattern is usually used alongside the lazy loading pattern to update the cache when there's a cache miss. The advantage of this pattern is that there are fewer database reads hence better performance. Better performance and user experience are guaranteed because there's a high chance of data being found in cache.