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.
Join the DZone community and get the full member experience.Join For Free
Get the Edge with a Professional Java IDE. 30-day free trial.
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.
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.
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)
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.
Opinions expressed by DZone contributors are their own.