Infinispan Cache: In a Nutshell

Posted by Kartik Sachdeva

One of the hottest new additions to JBoss galaxy is Infinispan. It is an open source, Java-based data grid platform. Data grids are highly concurrent distributed data structures that allow users to address large volumes of memory and store data in a way that it is quick to access.

Type of Infinispan Cache

There are two ways in which you can interact with Infinispan:

  1. Embedded Mode: in this mode, you can start an Infinispan instance within your JVM.
  2. Client/ Server Mode: in this mode, you can start a remote Infinispan instance and connect to it using a client connector.



CacheManager is the primary mechanism for handling the Cache. It is a heavyweight object, thus it is recommended to instantiate it and reuse it across the code.

Cache interface is JSR-107 Cache compatible interface, which provides concurrent, distributed cache structure and contains standard methods for adding (put) and retrieving (get) data from the Cache. Once instantiated, you can safely reuse this object in the code.

Features of Infinispan Cache

Infinispan caches are very powerful and can be configured and tuned to suit the needs of most applications. Some of the features are:


The purpose of a cache is to store short-lived data that needs to be refreshed or updated regularly; in short, it is supposed to store an entry only for a certain period of time.


Cache grows indefinitely because of unbounded nature. However, in Infinispan you can set a maximum size for a cache and when it reaches this limit, the entries accessed least recently are evicted. You can set the size of the cache based on the number of entries.


A cache, being an in-memory data structure, will lose data the moment the application terminates. You can overcome this limitation by backing a cache with a persistent store. There is a multitude of cache stores available, ranging from file-based to JDBC to popular NoSQL and Cloud stores like MongoDB, S3, and Cassandra. Select the cache store depending on the features you require.

Queries and indexing

Infinispan supports searching of Java objects stored in the grid using powerful search APIs that complement its main Cache API.

Caching Modes

Infinispan offers four caching modes: local, invalidation, replicated and distributed. All the modes support nearly all of the features described above.

Local caches

Local caches are entirely local, as they do not share data with any other node, even when the CacheManager is being clustered. Because of this, they are the fastest type of cache. You can configure the local cache like this:

Invalidation caches

An invalidation cache is a special type of clustered cache that does not share any data: only invalidation messages are sent to other members in the cluster, thus avoiding the presence of stale data. An invalidation cache is configured as follows:

Replicated caches

In replicated caches, the entries added on any node are replicated to all other nodes in the cluster. This ensures that all reads are always performed locally. Use this type of cache for read-heavy data, where writes are infrequent. You can configure replicated caches as follows:

Distributed caches

Distributed caches allow you to scale capacity linearly by adding new nodes to the cluster. You only need to specify the number of owners (i.e. copies of data) required. A distributed cache is configured as follows:

Setting up Distributed Infinispan Cache with Spring

Add needed dependencies

Need Infinispan Dependencies and Spring Cache & Spring Web MVC dependencies


Configure Infinispan Cache

Open and config Infinispan

Create an infinispan.xml file then place it at src/main/resources
Infinispan.xml file defines a customer cache:


Create Data Model

Create a simple Customer Model that implements Serializable


Create Caching Service

Create caching service with four functions:


Let us understand each of the functions created in the code above.

  1. @Cacheable(key=”#id”)

public Customer findByCode(long id): it will find a Customer in the cache using the key = “#id”. If the customer with the key does not exist in the cache, it will create the customer with the id and then cache it.
@Cacheable: Annotation indicating that the result of invoking a method (or all methods in a class) can be cached.

2. @CachePut (key=”#id”)

public Customer putCustomerToCache(long id): we can use put a Customer to Cache method to modify an instance that has been cached.
@CachePut: Annotation indicating that a method (or all methods on a class) triggers a cache put operation.

3. @CacheEvict(allEntries = true)

public void evictAllEntries(): it removes all instances in the cache.
@CacheEvict: Annotation indicating that a method (or all methods on a class) triggers a cache evict operation.

4. @CacheEvict(key=”#id”)

public void evictEntry(long id): it removes an instance from the cache.

Create Web Controller


Enable Caching

Using @EnableCaching method enables Spring’s annotation-driven cache management capability, similar to the support found in Spring’s XML namespace.

Benefits of Infinispan Cache

  • Scalability: All data access patterns use peer-to-peer communication where nodes directly speak to each other, which scales very well.
  • Data distribution: The goal of data distribution is to maintain enough copies of state in the cluster so it can be durable and fault tolerant.
  • Powerful: Infinispan 8 is based on Java 8, allowing you to use the very powerful Java Streams API to perform mass filtering and computations on a Cache, by taking advantage of data distribution and locality
  • Easy-to-use: Infinispan Server comes with an easy to use administration console. Before using it you need to create an admin user
  • Support for compute grids: Infinispan 8 adds the ability to pass a runnable around the grid, enabling pushing the complex processing towards the server where data is local and pull back results using a future.


Infinispan with its range of benefits and features is one of the most talked about platforms. Now that we have an idea of its capabilities as a cache within an application, we can use it to our advantage.

Let us know your thoughts and experience in the comments below.

That is all from us.

Until next time!

Related Posts

  • Understanding Chain of Responsibility PatternUnderstanding Chain of Responsibility Pattern

    The chain of responsibility helps to create a chain of receiver objects to complete a request. This pattern decouples the sender and receiver of every request based on the style…

  • Teradata Query Grid : With Database, NoSQL, HadoopTeradata Query Grid : With Database, NoSQL, Hadoop

    Teradata 15.0 has come up with various exciting features and enhanced capabilities, Teradata Query Grid being one of them. Teradata database is now able to connect Hadoop with this Query Grid,…

  • In a Nutshell: JAVA Executor FrameworkIn a Nutshell: JAVA Executor Framework

    Java executor framework is a very useful mechanism introduced in Java 1.5 to manage threads in large-scale applications. Before the executor framework, multithreading applications were developed using thread group and…

  • Column Store Index in SQL Server 2012

    This post is about the new feature, i.e., Column Store Index which is available since SQL 2012 version. Microsoft has released column store index to improve the performance by 10x.…

  • Teradata Intelligent Memory (TIM)

    Overview of Teradata Teradata is a RDBMS (Relational Database Management System). This system is solely based on off-the-shelf (readymade) symmetric multiprocessing (SMP) technology combined with communication networking, connecting SMP systems…

  • KAFKA-Druid Integration with Ingestion DIP Real Time Data

    The following blog explains how we can leverage the power of Druid to ingest the DIP data into Druid (a high performance, column oriented, distributed data store), via Kafka Tranquility…

Leave a Reply

Your email address will not be published. Required fields are marked *