Skip to main content

How to implement Cache Aside Pattern with Spring?

Problem

You want to boost application performance by loading data from a cache and prevent the network trip to the persistent store (and also the query execution). This can be achieved by loading data from a cache. However, you want to load data on demand or lazily. Also, you want the application to control the cache data management – loading, eviction, and retrieval.

Forces

  • Improve performance by loading data from cache lazily.
  • Application code controls cache data management.
  • The underlying caching system does not provide read-through, write-through/write-behind strategies (strange really ??).

Solution

Use cache aside design pattern to solve the problems outlined above. This is also one of many caching patterns/strategies. I believe it is named in this because aside from managing the data store, application code is responsible for managing the cache also.

Let's now try to understand how this caching technique works and then explore how it solves the problems.

Read

       Cache Miss
  1. The application tries to get data from the cache.
  2. The data is not available in the cache.
  3. The application queries the data store.
  4. The data is retrieved from the datastore.
  5. The application adds the data in the cache (completes lazy loading)
1
Cache Hit
  1. The application tries to get data from the cache.
  2. The cache returns the data
(Performance improvement, yay!!!)

2

Write/Update
  1. The application writes to the data store (new or updated data).
  2. The application then writes to the cache.
(Cache synchronized, yay!!!)

3

Implementation

Spring cache abstraction makes it very easy to implement Cache-aside pattern. Also, it prevents code from being mixed between business logic and caching logic. It uses aspects to separate cross-cutting concerns and saves on reinventing the wheel. The default implementation for Spring cache is ConcurrentHashMap. However, it can transparently integrate with distributed caching systems like Ehcache, Hazelcast, Apache Ignite etc.

Spring cache documentation can be found here.
The important classes from the Employee example are below.

The @Cacheable annotation ensures that when the findOne method is called, first the cache is checked for an employee with the given id. If it is present then, the data is returned from the cache. If the employee record is missing in the cache, then the actual findOne method is called which in turn queries the repository to retrieve the data. The retrieved data is then added to the cache for future access.

The @CachePut annotation is used with the write methods - save and update. As a result, the new and modified data is added back into the cache. This ensures the data in the cache is not stale.


This class drives the quick tests. The logs below shows that the "employee" cache behaves in cache-aside mode.

Consequences
Benefits
  1. Low latency reads
  2. Reduced workload on data store, which may indirectly lead to lower bill from the cloud provider :)
Concerns
  1. The application is responsible for managing the cache. The problem is accentuated if the application framework does not support Spring like caching abstraction. In that case, the code has to be written to manage the cache. There is a chance that the business logic will be polluted with caching logic. Caching logic is a cross-cutting concern and aspect-oriented design can be implemented to keep code clean.
  2.  The cache-aside pattern works best for read-only data. If the data changes fast, then there is a possibility that the underlying data store will be stormed with requests.
  3. There are challenges of synchronizing the cache with the underlying data store. This will happen even in the case of local in-memory cache. One thread may be reading employee record(potentially stale or invalidated entry), while another thread might be updating the data store and hence the cache. The problem is even greater in the case of a distributed cache. It takes a while (due to network latency etc) to synchronize changes across a distributed cache.
  4. Also like any caching pattern, it's always a challenge to set the eviction rule and expiration policy or whether to pin data. This can be set after observing the usage pattern.

Comments

Popular posts from this blog

Why do you need Spring Cloud Config server?

Last month I wrote a primer on concepts around 12 factor app. Before getting into the details of the Spring Cloud Config Server, I must refresh on the principle #3 from the list presented in that post.

3 – ConfigurationStore config in the environments
Configuration information must be separate from the source code. This may seem so obvious, but often we are guilty of leaving critical configuration parameters in the scattered in the code. Instead, applications should have environment specific configuration files. The sensitive information like database password or API key should be stored in these environment configuration files in encrypted format.
 The key takeaways from this postulate for a cloud-native microservices application are:
Do not store configuration as part of the deployable unit (in the case of lead microservice - inside the jar or war if you are still deploying war like the good old days). Instead, store it in an external location and make it easily accessible during run-…

Changing the root web application in Tomcat 6

These days I am very busy developing a large and complex web application, which is nearing its first release. This web based product runs on Tomcat 6 and will be deployed on http://www.mydomain.com
But if I deploy my web application on Tomcat 6, I need to specify the context root. Well this is no good, why will the users of this website be willing to remember the context root. What if the context root changes. In other words the users would not be interest in typing the following - http://www.mydomain.com/myappcontext to visit this site.
Hence it is necessary that I change the default / root web application of Tomcat and point it to my application. I wanted to try and test this on localhost first. As always I Googled and found a number of posts and none seem to work. I also read the documentation it was helpful in understanding the concepts but was not effective as I could not get it working. Finally after few hours of trial and error one of my colleague Joyeeta Majumdar found the so…

Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- Tiles -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${tiles.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>${tiles.version}</version>
<scope>compile</scope>