DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • TAO: A Comprehensive Look at Facebook's Distributed Data Store
  • The Bill You Didn't See Coming
  • Fine-Tuning of Spring Cache
  • Respecting robots.txt in Web Scraping

Trending

  • Why AI Forces a Rethink of Everything We Know About Software Security
  • Java Backend Development in the Era of Kubernetes and Docker
  • Swift Concurrency Part 4: Actors, Executors, and Reentrancy
  • The Prompt Isn't Hiding Inside the Image
  1. DZone
  2. Data Engineering
  3. Data
  4. The Cache Aside Pattern

The Cache Aside Pattern

This design pattern sets up optimal caching, read operations, and data retrieval based on whether you want to preload data or load it as you go.

By 
Deepak Tyagi user avatar
Deepak Tyagi
·
Apr. 13, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
24.7K Views

Join the DZone community and get the full member experience.

Join For Free

The Cache Aside pattern, if used correctly, can improve the performance while reading data from the data store by utilizing cache. This pattern can be used for read and update operations of data to and from the data store.

Read Operations

For read operations, your cache is checked first for the availability of data using a key. If the data is present, then the read call returns the data from the cache and completes the operation. In a case where data is not present in the cache, a round trip is made to the underlying data store to fetch the data, and a cache entry is created against a key before returning the response. This newly created cache entry will make subsequent read operations for the same data fetched from the cache, thus cutting down on the response time and increasing throughput.Image: 1.a

In Spring, you can implement it as described below, where when getRecordForSearch is called, it will be fetched automatically from the cache if it is available (it will not hit the method code at all). Otherwise, it will be retrieved from the data store.

@Cacheable("default", key="#search.keyword)
public Record getRecordForSearch(Search search)

Data Update

If the interfacing application can update the data in the data store and it is present in the cache, then the cache has to reflect the update as well. To get around the out of sync issue and to ensure the data consistency and performance for the data retrieval, two strategies can be utilized, depending on the requirements.

Cache Eviction on Data Updates

In this case, when a method to update the data in the data store is called, it also invalidates the cache entry using the key. In this approach, the cache entry gets loaded only when it is re-requested after the update. In Spring, this can be done as follows:

@CacheEvict("default", key="#search.keyword)
public Record updateRecordForSearch(Search search)

Cache Updates on Data Updates

Cache entries can also be updated if and when the data is updated in the data store to get faster data retrieval and consistency in one shot. In Spring, this can be achieved by doing as follows:

@CachePut("default", key="#search.keyword)
public Record updateRecordForSearch(Search search)


Depending on the need and type of data, different cache loading strategies can be adopted.

  • Load data as you go: If and when data is needed, it is loaded into the cache. The first request fetches it from the data store, but the subsequent requests get the data from the cache.

  • Preload cache: This strategy is best for data loaded with an application's startup, reference data such as country list, currency, important dates, etc. This is great for data that does not change very often.

Share your comments if you have any other caching patterns that you have used and found useful.

Cache (computing) Data (computing) Data store

Opinions expressed by DZone contributors are their own.

Related

  • TAO: A Comprehensive Look at Facebook's Distributed Data Store
  • The Bill You Didn't See Coming
  • Fine-Tuning of Spring Cache
  • Respecting robots.txt in Web Scraping

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook