Over a million developers have joined DZone.

Spring Boot Plus Apache Ignite Data Grid

DZone's Guide to

Spring Boot Plus Apache Ignite Data Grid

Learn how to integrate Spring Boot with Apache Ignite to use Ignite's persistent durable memory feature and execute SQL queries over Ignite caches.

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

Spring Boot Integration With Apache Ignite and Its Durable Memory

In this post, we will show how we can do the following :

  • Integrate Spring Boot with Apache Ignite

  • Enable and use the persistent durable memory feature of Apache Ignite, which can persist your cache data to the file disk, to survive a crash or restart so you can avoid data loss

  • Execute SQL queries over Ignite caches

  • Unit test and integration test Ignite with Spring Boot

  • Make simple Jenkins pipeline references

The code repository in GitHub is here.


What Is Ignite Durable Memory?

The Apache Ignite memory-centric platform is based on the durable memory architecture that allows storing and processing data and indexes both in memory and on disk when the Ignite Native Persistence feature is enabled. The durable memory architecture helps achieve in-memory performance with the durability of disk using all the available resources of the cluster

What Are Ignite Data-Grid SQL Queries?

Ignite supports a very elegant query API with support for Predicate-based Scan Queries, SQL Queries (ANSI-99 compliant), and Text Queries. For SQL queries, Ignite supports in-memory indexing, so all the data lookups are extremely fast. If you are caching your data in off-heap memory, then query indexes will also be cached in off-heap memory as well.

Ignite also provides support for custom indexing via IndexingSpi and the SpiQuery class.

More information about cache queries can be found here.

Integrating Apache Ignite Server Node With Your Spring Boot App

Add the following Maven dependencies to your Spring Boot app pom file:


Define the Ignite configuration via Java DSL for better portability and management as a Spring configuration and the properties values will be loaded from the application.yml file:

public class AlertManagerConfiguration {

    private String baseUrl;
    private String user;
    private String password;
    private boolean enableFilePersistence;
    private int igniteConnectorPort;
    private String igniteServerPortRange;
    private String ignitePersistenceFilePath;

    IgniteConfiguration igniteConfiguration() {
        IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
        // durable file memory persistence
            PersistentStoreConfiguration persistentStoreConfiguration = new PersistentStoreConfiguration();
        // connector configuration
        ConnectorConfiguration connectorConfiguration=new ConnectorConfiguration();
        // common ignite configuration
        BinaryConfiguration binaryConfiguration = new BinaryConfiguration();
        // cluster tcp configuration
        TcpDiscoverySpi tcpDiscoverySpi=new TcpDiscoverySpi();
        TcpDiscoveryVmIpFinder tcpDiscoveryVmIpFinder=new TcpDiscoveryVmIpFinder();
        // need to be changed when it come to real cluster
        igniteConfiguration.setDiscoverySpi(new TcpDiscoverySpi());
        // cache configuration
        CacheConfiguration alerts=new CacheConfiguration();
        // as we have one node for now

        CacheConfiguration alertsConfig=new CacheConfiguration();
        // as we have one node for now
        return igniteConfiguration;

    @Bean(destroyMethod = "close")
    Ignite ignite(IgniteConfiguration igniteConfiguration) throws IgniteException {
        final Ignite start = Ignition.start(igniteConfiguration);
        return start;


Then you can just inject the Ignite instance as a Spring bean, which makes unit testing much easier.

public class IgniteAlertConfigStore implements AlertsConfigStore {

    private static final Logger logger = LoggerFactory.getLogger(IgniteAlertConfigStore.class);
    // here it will be injected as a spring bean
    private Ignite ignite;

    public AlertConfigEntry getConfigForServiceIdCodeId(String serviceId, String codeId) {
        return Optional.ofNullable(getAlertsConfigCache().get(serviceId + "_" + codeId))
                .orElseThrow(() -> new ResourceNotFoundException(String.format("Alert config for %s with %s not found", serviceId,codeId)));

    public void update(String serviceId, String codeId, AlertConfigEntry alertConfigEntry) {
        getAlertsConfigCache().put(serviceId + "_" + codeId, alertConfigEntry);

    public Optional<AlertConfigEntry> getConfigForServiceIdCodeIdCount(String serviceId, String codeId) {
        return Optional.ofNullable(getAlertsConfigCache().get(serviceId + "_" + codeId));


    public Cache<String, AlertConfigEntry> getAlertsConfigCache() {
        return ignite.getOrCreateCache(CacheNames.AlertsConfig.name());

How to enable Ignite durable memory:

  // durable file memory persistence
            PersistentStoreConfiguration persistentStoreConfiguration = new PersistentStoreConfiguration();

How to use Ignite SQL queries in memory storage:

    public List<AlertEntry> getAlertForServiceId(String serviceId) {
        final String sql = "serviceId = ?";
      // create the sql query object with entity type of the value part of the key value cache
        SqlQuery<String, AlertEntry> query = new SqlQuery<>(AlertEntry.class, sql);
      // set the query params
       //then execute it over the cache
        return Optional.ofNullable(getAlertsCache().query(query).getAll().stream().map(stringAlertEntryEntry -> stringAlertEntryEntry.getValue()).collect(Collectors.toList()))
                .orElseThrow(() -> new ResourceNotFoundException(String.format("Alert for %s not found", serviceId)));

How to do atomic thread-safe actions over the same record via cache invoke API:

    public void updateAlertEntry(String serviceId, String serviceCode, AlertEntry alertEntry) {
        final IgniteCache<String, AlertEntry> alertsCache = getAlertsCache();
        // update the alert entry via cache invoke for atomicity
        alertsCache.invoke(alertEntry.getAlertId(), (mutableEntry, objects) -> {
            if (mutableEntry.exists() && mutableEntry.getValue() != null) {
                logger.debug("updating alert entry into the cache store invoke: {},{}", serviceId, serviceCode);
            } else {
                throw new ResourceNotFoundException(String.format("Alert for %s with %s not found", serviceId, serviceCode));
            // by api design nothing needed here
            return null;

How to unit test Apache Ignite usage in the Spring Boot service:

public class IgniteAlertsSoreTest {
    private Ignite ignite;
    Cache<String, List<AlertEntry>>  cache;
    IgniteCache IgniteCache;
    private IgniteAlertsStore igniteAlertsStore;
    //simulate the needed behaviour for the mocked ignite cache
    public void setUp() throws Exception {
        List<AlertEntry> entries=new ArrayList<>();
    public void getAllAlerts() throws Exception {


How to trigger integration tests with Ignite and check test resources:

@SpringBootTest(classes = AlertManagerApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AlertManagerApplicationIT {
    private int port;
    private TestRestTemplate template;
    private URL base;
    public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/");
    // then add your integration test which will include real started ignite server node whoch will be closed once the integration test is done
    public void contextLoads() {



How to run and test the application over Swagger REST API:

  • Build the project via Maven with  mvn clean install 
  • You can run it from IDEA via AlertManagerApplication.java or via java -jar jarName.

Image title

  • Swagger, which contains the REST API and REST API model documentation, will be accessible on the URL below, where you can start triggering different REST API calls exposed by the Spring Boot app: http://localhost:8080/swagger-ui.html#/.

Image title

  • If you STOP the app or restart it and do the query again, you will find all created entities from the last run so it survived the crash plus any possible restart.
  • You can build a portable Docker image of the whole app using the Maven Spotify Docker plugin, if you wish.


IAM is now more than a security project. It’s an enabler for an integration agile enterprise. If you’re currently evaluating an identity solution or exploring IAM, join this webinar.

apache ignite ,spring boot ,in memory data grid ,key value store ,nosql ,integration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}