Spring Cache with Apache Ignite

Suman Das
Suman Das
Jan 30, 2020 · 6 min read

Every now and then, we have all been bogged down by the challenges of dealing with slow services. Reasons such as high load due to multiple users, having a complicated business logic or interaction with a remote database may cause the services to slow down.

Among other performance improvement techniques, caching is one such option by which we can improve the service performance. The Spring Framework provides support for transparently adding caching to an existing Spring application. At its core, the abstraction applies caching to methods, thus reducing the number of executions based on the information available in the cache. The caching logic is applied transparently, without any interference to the invoker.

Apache Ignite is a memory-centric distributed database, caching, and processing platform for transactional, analytical, and streaming workloads delivering in-memory speeds at the petabyte scale.

Ignite provides an in-memory data store where each node in an Ignite cluster by default stores data in RAM. The data is kept in off-heap storage to ensure low latency and consistent access times. The system is multi-model, with the ability to support structured, semi-structured and unstructured data.

In this tutorial, we will learn to configure Apache Ignite as a Spring Cache provider.

Configuring Apache Ignite as a Spring Cache Provider

We will use the following steps to integrate Apache Ignite with Spring Cache:

  1. Integrating Apache Ignite Server Node With Spring Boot
  2. Enabling Caching
  3. Using Caching Annotation
  4. Enabling Ignite Rest API
  5. Spring Boot Caching Example



1. Integrating Apache Ignite Server Node With Spring Boot

Spring provides a CacheManager interface that defines methods for managing a collection of Cache instances. A Cache instance is a wrapper around the underlying cache, and it provides methods for interacting with the underlying cache.

Apache Ignite is shipped with SpringCacheManager - implementation of Spring Cache Abstraction. It provides an annotation-based way to enable caching for Java methods so that the result of a method execution is stored in the Ignite cache. Later, if the same method is called with the same set of parameter values, the result will be retrieved from the cache instead of actually executing the method.

We will need the following Maven dependencies in our Spring Boot app to enable Ignite based caching.


2. Enabling Caching

  1. Start an Ignite node with proper configuration in embedded mode, that is, in the same JVM where the application is running. It can already have predefined caches, but it’s not required — caches will be created automatically on first access if needed.
  2. Configure SpringCacheManager as the cache manager in the Spring application context.

The embedded node can be started by SpringCacheManager itself. In this case, we will need to provide a path to either the Ignite configuration XML file or IgniteConfiguration bean via configurationPath or configuration properties respectively. Let’s enable SpringCacheManager using IgniteConfiguration bean option.

Ignite Configuration

Here, we are using simple In-memory caching. And we have configured two cache named “employee” and “student”. We are not using any WriteThough or WriteBehind features.

3. Using Caching Annotation

Usually, we would use caching for heavy operations, like database access which takes time or cache methods that are called frequently. To simulate the caching behavior we will cache a method with some Sleep value. We can use @Cacheable annotation to enable caching for this method:

Caching the results

@Cacheable annotation has some more options. For instance, we can specify the key of the cache from the request of the method. If nothing is specified, Spring uses all the class fields and uses those as cache key (mostly HashCode) to maintain caching. However, we can override this behavior by providing key information.

For different @Cacheable options please check Spring-Cache.

@CachePut annotation can be used to update the cache.

Update Cache

Similarly, @CacheEvict annotation can be used to delete the cache.

Delete cache

Below are the Caching Annotations available in Spring:

  • @Cacheable : Triggers cache population
  • @CachePut : Updates the cache, without interfering with the method execution
  • @CacheEvict : Triggers cache eviction[removing items from cache]
  • @Caching : Regroups multiple cache operations to be applied on a method
  • @CacheConfig : Shares some common cache-related settings at class-level
  • @EnableCaching : Configuration level annotation, enables Caching

4. Enabling Ignite Rest API

To enable HTTP connectivity, we have addedignite-rest-http as a Maven dependency in our application. Explicit configuration is not required; the connector starts up automatically and listens on port 8080. We can also change the port by specifying -DIGNITE_JETTY_PORT=8081 as VM arguments.

We can check whether the rest API is working by using the below curl command.

curl 'http://localhost:8081/ignite?cmd=version'

Following is a sample GET REST API which we can be used to procure the data from the Cache using the CacheName and key.


For all other Rest API options check Ignite Rest-API

5. Spring Boot Caching Example

5.1 Service

Let’s start with a service that can be used to procure the data, update the data and delete the data. Our main target is to cache the response of the method in the service layer, so we will introduce an intentional delay to simulate the actual backend service call to get the result. In the first hit, the response will be delayed as we will have some simulated delay in the application, but in the subsequent calls, we will get a much faster response. Also, when we update the data then our data in cache will also be updated. Similarly, when we delete the data then it is removed from the cache.


Note: The service layer method is annotated with Spring Caching annotations as explained above.

5.2 Entity

Let’s use a sample Entity called Employee to store the data.

Employee Entity

5.3 RestController

We will create some sample Rest endpoint to test our Caching application.


5.4 Demo

We can now use the Swagger URL to test the Spring Cache application.


If you would like to refer to the full code, do check:


References & Useful Readings

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Suman Das

Written by

Suman Das

Tech Enthusiast, Software Engineer

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Suman Das

Written by

Suman Das

Tech Enthusiast, Software Engineer

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store