Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Build embedded cache clusters with Quarkus and Red Hat Data Grid

December 17, 2020
Daniel Oh
Related topics:
QuarkusMicroservicesArtificial intelligenceJava
Related products:
Red Hat Data Grid

    There are many ways to configure the cache in a microservices system. As a rule of thumb, you should use caching only in one place; for example, you should not use the cache in both the HTTP and application layers. Distributed caching both increases cloud-native application performance and minimizes the overhead of creating new microservices.

    Infinispan is an open source, in-memory data grid that can run as a distributed cache or a NoSQL datastore. You could use it as a cache, such as for session clustering, or as a data grid in front of the database. Red Hat Data Grid builds on Infinispan with additional features and support for enterprise production environments.

    Data Grid lets you access a distributed cache via an embedded Java library or a language-independent remote service. The remote service works with protocols such as Hot Rod, REST, and Memcached. In this article, you will learn how to build a distributed cache system with Quarkus and Data Grid. We'll use Quarkus to integrate two clustered, embedded Data Grid caches deployed to Red Hat OpenShift Container Platform (RHOCP). Figure 1 shows the distributed cache architecture for this example.

    Clustered embedded RHDG
    Clustered embedded RHDG
    Figure 1: Clustered embedded Data Grid caches in an OpenShift Container Platform deployment.

    Build a distributed cache system with Quarkus and Data Grid

    In this example, we’ll create a simple ScoreCard application to store data in two clustered embedded caches. We'll go through the process step-by-step.

    Step 1: Create and configure a Quarkus application

    The first step is to set up the Quarkus application. You can use Quarkus Tools to generate a Quarkus project in your preferred IDE. See the complete source code for the example Quarkus application.

    Step 2: Add the Infinispan-embedded and OpenShift extensions

    After you've configured your Quarkus project with an artifactId (embedded-caches-quarkus), you will need to add the required Quarkus extensions. Add the following dependencies to your Maven pom.xml:

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-infinispan-embedded</artifactId>
    </dependency>
    <dependency>
      <groupId>org.infinispan.protostream</groupId>
      <artifactId>protostream-processor</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-openshift</artifactId>
    </dependency>
    

    As an alternative, run the following Maven plug-in command from your Quarkus project's base directory:

    ./mvnw quarkus:add-extension -Dextensions="infinispan-embedded,openshift"
    

    Step 3: Create a caching service

    Next, we'll create a caching service. Red Hat Data Grid provides an EmbeddedCacheManager interface for creating, modifying, and managing clustered caches. The CacheManager interface runs in the same Java virtual machine (JVM) as the client application. Create a new service named ScoreService.java in your Quarkus project, then add the following code to save, delete, and retrieve score data in the example application:

    EmbeddedCacheManager cacheManager;
    public List<Score> getAll() {
      return new ArrayList<>(scoreCache.values());
    }
    
    public void save(Score entry) {
      scoreCache.put(getKey(entry), entry);
    }
    
    public void delete(Score entry) {
      scoreCache.remove(getKey(entry));
    }

    Step 4: Initialize the cache nodes

    Next, you’ll define a GlobalConfigurationBuilder to initialize all cache nodes at startup. The existing cache data will be replicated automatically when a new node is created. Add the following onStart method in ScoreService.java class:

    void onStart(@Observes @Priority(value = 1) StartupEvent ev) {
    
       GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
       global.transport().clusterName("ScoreCard");
       cacheManager = new DefaultCacheManager(global.build());
    
       ConfigurationBuilder config = new ConfigurationBuilder();
       config.expiration().lifespan(5, TimeUnit.MINUTES).clustering().cacheMode(CacheMode.REPL_SYNC);
    
       cacheManager.defineConfiguration("scoreboard", config.build());
       scoreCache = cacheManager.getCache("scoreboard");
       scoreCache.addListener(new CacheListener());
    
       log.info("Cache initialized");
    
    }
    

    Step 5: Configure the transport layer

    Red Hat Data Grid uses a JGroups library to join clusters and replicate data over the transport layer. To deploy the Quarkus application with the embedded Infinispan caches, we need to add the following DNS_PING configuration in the resources/default-configs/jgroups-kubernetes.xml file:

    <dns.DNS_PING dns_query="jcache-quarkus-ping" num_discovery_runs="3" />
    

    Step 6: Add a CacheListener

    Red Hat Data Grid provides CacheListener APIs for clients to get notifications about cache events, such as when a new cache entry is created. We can implement a listener by adding the @Listener annotation to a plain old Java object (POJO) class. Let’s create a new CacheListener.java class and add the following code to it:

    @Listener
    public class CacheListener {
    
       @CacheEntryCreated
       public void entryCreated(CacheEntryCreatedEvent<String, Score> event) {
           System.out.printf("-- Entry for %s created \n", event.getType());
       }
    
       @CacheEntryModified
       public void entryUpdated(CacheEntryModifiedEvent<String, Score> event){
           System.out.printf("-- Entry for %s modified\n", event.getType());
       }
    
    }
    

    Step 7: Build and deploy the Quarkus application to OpenShift

    The OpenShift extension that we added earlier is a wrapper extension that combines the Kubernetes and container-image-s2i extensions with a default configuration. The extension makes it easy to get started with Quarkus on OpenShift.

    Quarkus lets us automatically generate OpenShift resources based on a default or user-supplied configuration. To deploy our Quarkus application to OpenShift, we only need to add the following configuration to the application's application.properties file:

    quarkus.openshift.expose=true
    quarkus.kubernetes-client.trust-certs=true
    quarkus.container-image.build=true
    quarkus.kubernetes.deploy=true
    

    Use the following command to log into the OpenShift cluster:

    $ oc login --token=<YOUR_USERNAME_TOKEN> --server=<OPENSHIFT API URL>
    

    Then, execute the following Maven plug-in command:

    ./mvnw clean package
    

    Step 8: Scale-up to two pods

    Assuming that we've successfully deployed the Quarkus application, our next step is to scale up to two pods. Run the following command:

    oc scale dc/embedded-caches-quarkus --replicas=2
    

    To verify the pods, go to OpenShift's topology view in your developer console. Check to see whether the two pods are running in the embedded-caches-quarkus project, as shown in Figure 2.

    The two pods are shown in OpenShift's topology view.
    Figure 2: The two pods  are running in our Quarkus application

    Step 9: Test the application

    As a final step, let's test the application. We can start by using a RESTful API invocation to add a few scores in the clustered embedded caches:

    curl --header "Content-Type: application/json" --request PATCH -d '{"card":[5,4,4,10,3,0,0,0,0,0,0,0,0,0,0,0,0,0],"course":"Bethpage","currentHole":4,"playerId":"4","playerName":"Daniel"}' <APP_ROUTE_URL>
    

    If you are using the sample application, you need to run a predefined bash script (in this case, it's sh scripts/load.sh).

    Return to the topology view and click View logs for each pod in its own web browser. This lets you monitor cache events from CacheListener. You should see the same cache entry logs in both Data Grid nodes. Figure 3 shows the output from Node 1.

    The CacheListener output from Node 1.
    Figure 3: Monitoring cache events from the CacheListener (Node 1).

    Figure 4 shows the output from Node 2.

    The CacheListener output from Node 2.
    Figure 4: Monitoring cache events from the CacheListener (Node 2).

    What’s next for Quarkus and Red Hat Data Grid

    Red Hat Data Grid increases application response times and lets developers dramatically improve performance while providing availability, reliability, and elastic scale. Using Quarkus' serverless function for the front end and external Red Hat Data Grid servers, you can integrate these benefits into a serverless architecture designed for high-performance, auto-scaling, and superfast response times. 

    Last updated: May 17, 2021

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.