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

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

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • TAO: A Comprehensive Look at Facebook's Distributed Data Store
  • Implement Hibernate Second-Level Cache With NCache
  • Supporting Offline Mode: Key Strategies and Benefits for iOS Mobile Apps
  • Caching RESTful API Requests With Heroku Data for Redis

Trending

  • Software Specs 2.0: An Elaborate Example
  • Altering XML Tag Position Using Mule 4 With Basic Authentication
  • Smarter IoT Systems With Edge Computing and AI
  • From Code to Customer: Building Fault-Tolerant Microservices With Observability in Mind
  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.1K 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
  • Implement Hibernate Second-Level Cache With NCache
  • Supporting Offline Mode: Key Strategies and Benefits for iOS Mobile Apps
  • Caching RESTful API Requests With Heroku Data for Redis

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: