Search Sreeni's Insights

Filter by content type
technical June 6, 2019 6 min read

How to cache the data based on the demand? Cache-aside, lazy loading and read-through - Part 2

The cache-aside pattern with lazy loading and read-through, their pros and cons, and use cases from e-commerce and the mutual fund world.

Share:

How to cache the data based on the demand? Cache-aside, lazy loading and read-through - Part 2

Choosing the right caching strategy is very important, because a wrong choice gives us either stale data or no performance gain at all. Most of the time, the first question is “who is responsible for filling the cache?”, is it the application, or is it somebody else? In the previous article, we discussed why we need caching and the types of cache. In this article, we are going to see the cache-aside pattern, and its two strategies, lazy loading and read-through.

TL;DR Summary

  • Cache-aside pattern caches the data based on the demand. It improves the performance and helps the application to get the required consistency level.
  • Lazy loading - the application checks the cache first, and on a miss it reads from the database and updates the cache.
  • Read-through is similar to lazy loading, except the caching responsibility is delegated to a library or a stand-alone cache provider.
  • Pick both only for read-heavy operations, where you have a high level of control over the expiry.
  • The cache can have stale data in both, so it is important to invalidate it by setting an appropriate TTL (time to live).
  • With dynamic scale-out, a distributed cache avoids the empty local cache problem of the new instances.

What is the cache-aside pattern?

Cache-aside pattern focuses on solving the problem of caching the data based on the demand. It helps to improve the performance of the application, and also helps the application to get the required consistency level by choosing the right strategy based on the application requirement.

Think about a notebook of phone numbers. You don’t write all the numbers in advance. When you need a number, you look at the notebook first, and if it is not there, you search the directory and write it in the notebook for the next time. That is the cache-aside.

Lazy loading

Pick this strategy when your application,

  • Consists of read-heavy operations
  • Has high level of control over its expiry

The following diagram depicts the lazy loading flow,

Lazy loading
Lazy loading

Follow the following steps to understand how it works,

  1. User initiates the request.
  2. Application does the lookup in cache for the requested data.
  3. When data is available, it is considered as cache hit. Data is returned from the cache.
  4. When data is not available, application reads it from the database, updates the cache, then returns it to the user.
ProsCons
+ Only requested data will be in cache, hence it avoids storing the data which is not requested by the user− Due to lazy load strategy, more cache-miss will occur, which could lead to a read from the database and increase the latency
+ Fallback plan helps to avoid the failure upon cache failure or corruption− Quite often, the cache will have stale data, given that this strategy loads the data only when it is required. It is important to invalidate the cache by setting an appropriate TTL

Info: Given the dynamic scale-out rules of app services, instances will be added and removed based on the demand. It has a huge impact on serving from cache by the new instance, because of its local empty cache. With the help of a distributed cache, this can be avoided completely. We will see it in Part 4.

Read-through

Read-through is very similar to the lazy loading, except it delegates the caching responsibility to the library or stand-alone cache provider. The selection criteria is same as cache-aside, but the implementation is taken away from the application.

Pick this strategy when your application,

  • Consists of read-heavy operations
  • Has high level of control over its expiry
  • Uses a standalone data provider library which has its own cache or database
Read-through
Read-through

The application only asks the data provider for the data. The provider checks the cache, reads from the database on a miss, and writes the result to the cache. The application doesn’t know anything about it.

Read-through has similar pros and cons as the lazy loading strategy.

Lazy loading vs read-through

Lazy loadingRead-through
Who fills the cache?The applicationThe data provider library
Application codeHas the cache logicOnly asks for the data
Pick whenRead-heavy, high control over expirySame, and you use a standalone data provider library
Pros and consStale data and more cache missesSame as lazy loading

As a best practice, implement the cache-aside logic as extension methods over the cache provider, so the business code stays clean. We will see an example in Part 5.

Use cases

Let us see how these two strategies look in the e-commerce and mutual fund world. These are illustrative scenarios.

E-commerce: product details page (lazy loading)

A product details page is read-heavy, and the product data changes only when the business updates it. The first shopper who opens the product causes a cache miss, the application reads the product from the database and stores it in the cache. The next shoppers are served from the cache. Only the products which are really viewed will be in the cache, so we don’t waste the memory for the products nobody opens.

Set the TTL based on how fast the business wants a product change (for example, a description or an image) to be visible. For the price, refer Part 4, because a stale price is more dangerous than a stale description.

Mutual fund: fund factsheet (lazy loading)

A fund factsheet is requested by many investors, and the content changes rarely. The same lazy loading flow works well here. The key can be the fund unit code (for example, Unit_001), so every investor who opens the same fund is reading the same cached copy.

Read-through: catalog access through a data provider

When the application reads the catalog or the fund data through a stand-alone data provider library which has its own cache, the read-through is the better choice. The application code just asks for the product or the fund, and the provider decides whether to read from the cache or from the database.

Use caseStrategyReason
Product details pageLazy loadingRead-heavy, we control the expiry
Fund factsheetLazy loadingRead-heavy, content changes rarely
Catalog or fund data through a data provider libraryRead-throughCaching responsibility is with the library

Wrap up

In this article, we have looked at the cache-aside pattern, how lazy loading and read-through work, and their pros and cons. Thanks for reading this article. In the next article, I will walk you through the write strategies, write-through and write-behind, and the refresh-ahead strategy, and how to choose the right one.

Related Posts