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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Deepak Tyagi user avatar by
Deepak Tyagi
·
Apr. 13, 17 · Tutorial
Like (6)
Save
Tweet
Share
21.92K 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.

Popular on DZone

  • NEXT.JS 13: Be Dynamic Without Limits
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • Revolutionizing Supply Chain Management With AI: Improving Demand Predictions and Optimizing Operations

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: