Data Grid Code Tutorials

Red Hat Data Grid 8.6

Learn how to use Data Grid capabilities

Red Hat Customer Content Services

Abstract

Run code tutorials for remote caches and embedded caches that demonstrate various Data Grid capabilities and usage patterns.

Red Hat Data Grid

Data Grid is a high-performance, distributed in-memory data store.

Schemaless data structure
Flexibility to store different objects as key-value pairs.
Grid-based data storage
Designed to distribute and replicate data across clusters.
Elastic scaling
Dynamically adjust the number of nodes to meet demand without service disruption.
Data interoperability
Store, retrieve, and query data in the grid from different endpoints.

Data Grid documentation

Documentation for Data Grid is available on the Red Hat customer portal.

Data Grid downloads

Access the This content is not included.Data Grid Software Downloads on the Red Hat customer portal.

Note

You must have a Red Hat account to access and download Data Grid software.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.

Chapter 1. Remote caches

Deploy multiple Data Grid Server instances to create remote cache clusters that give you a fault-tolerant and scalable data tier with high-speed access from Hot Rod and REST clients.

1.1. Remote cache tutorials

To run these tutorials you need at least one locally running instance of Data Grid Server. Each tutorial will try to connect to a running server in localhost:11222 with admin/password credentials. However, if a Docker instance is found, and the server is not running, tutorials will spin up a local server with Testcontainers.

You can This content is not included.download the distribution and run the following commands:

$ ./bin/cli.sh user create admin -p "password"
$ ./bin/server.sh
Note

Data Grid Server enables authentication and authorization by default. Creating a user named admin gives you administrative access to Data Grid Server.

Building and running remote cache tutorials

You can build and run remote cache tutorials directly in your IDE or from the command line as follows:

$ ./mvnw -s /path/to/maven-settings.xml clean package exec:exec

1.2. Hot Rod Java client tutorials

  • Data Grid requires Java 11 at a minimum. However, Hot Rod Java clients running in applications that require Java 8 can continue using older versions of client libraries.
Tutorial linkDescription

Content from github.com is not included.Remote cache use example

The simplest code example that demonstrates how a remote distributed cache works.

Content from github.com is not included.Remote cache with cache alias example

Demonstrates how to update a cache config to add an alias and use it to manipulate the cache.

Content from github.com is not included.Per cache configuration

Demonstrates how to configure caches dynamically when we connect to the Data Grid Server.

Content from github.com is not included.Near caches

Demonstrates how configure near caching to improve the read performance in remote caches.

Content from github.com is not included.Cache Admin API

Demonstrates how to use the Administration API to create caches and cache templates dynamically.

Content from github.com is not included.Encoding

Demonstrates how encoding of caches work.

Content from github.com is not included.Custom marshalling

Demonstrates how custom marshalling works in the context of programmatically creating Protostream schemas and marshallers. Users should generally rely on annotations based generated Protostream marshallers unless exceptional circumstances require manual creation.

Content from github.com is not included.Protostream schemas administration

Demonstrates how to use the Administration API to manipulate the Protostream (protobuf format) schemas in Data Grid.

Content from github.com is not included.Client listeners

Detect when data changes in a remote cache with Client Listeners.

Content from github.com is not included.Query

Demonstrates how to query remote cache values.

Content from github.com is not included.Continuous query

Demonstrates how to use Continuous Query and remote caches.

Content from github.com is not included.Transactions

Demonstrates how remote transactions work.

Content from github.com is not included.Secured caches

Demonstrates how to configure caches that have authorization enabled.

Content from github.com is not included.TLS authorization

Demonstrates how to connect to Data Grid Server with TLS authorization.

Content from github.com is not included.Multimap

Demonstrates how remote multimap works.

Content from github.com is not included.Task execution

Demonstrates how to register server tasks and how to execute them from the Hot Rod client.

Content from github.com is not included.JUnit 5 and Testcontainers

Demonstrates how to use the Data Grid and JUnit 5 extension.

Content from github.com is not included.Persistence

Demonstrates how to use the Data Grid and persistent caches.

Content from github.com is not included.Redis Client

Demonstrates how to use the Data Grid and Redis client to read and write using the Resp protocol.

Content from github.com is not included.Reactive API

Demonstrates how to use the Data Grid with the reactive API based on Mutiny.

Data Grid documentation

You can find more resources for Hot Rod Java clients in our documentation at:

Chapter 2. Embedded caches

Add Data Grid as a dependency to your Java project and use embedded caches that increase application performance and give you capabilities to handle complex use cases.

2.1. Embedded cache tutorials

You can run embedded cache tutorials directly in your IDE or from the command line as follows:

$ ./mvnw -s /path/to/maven-settings.xml clean package exec:exec
Tutorial linkDescription

Content from github.com is not included.Distributed caches

Demonstrates how Distributed Caches work.

Content from github.com is not included.Replicated caches

Demonstrates how Replicated Caches work.

Content from github.com is not included.Invalidated caches

Demonstrates how Invalidated Caches work.

Content from github.com is not included.Cache alias

Demonstrates how Cache alias work.

Content from github.com is not included.Transactions

Demonstrates how transactions work.

Content from github.com is not included.Cache security

Demonstrates how to integrate an application with Data Grid security features.

Content from github.com is not included.Streams

Demonstrates how Distributed Streams work.

Content from github.com is not included.JCache integration

Demonstrates how JCache works.

Content from github.com is not included.Functional Maps

Demonstrates how Functional Map API works.

Content from github.com is not included.Map API

Demonstrates how the Map API works with Data Grid caches.

Content from github.com is not included.Multimap

Demonstrates how to use Multimap.

Content from github.com is not included.Metrics

Demonstrates how to enable and extract metrics in Data Grid.

Content from github.com is not included.Queries

Uses Data Grid Query to perform full-text queries on cache values.

Content from github.com is not included.Query with programmatic index definition

Uses Data Grid Query to perform full-text queries on cache values, with programmatic index definition.

Content from github.com is not included.Clustered Listeners

Detects when data changes in an embedded cache with Clustered Listeners.

Content from github.com is not included.Counters

Demonstrates how to use an embedded Clustered Counter.

Content from github.com is not included.Clustered Locks

Demonstrates how to use an embedded Clustered Lock.

Content from github.com is not included.Clustered execution

Demonstrates how to use an embedded Clustered Counter.

Data Grid documentation

You can find more resources about embedded caches in our documentation at:

2.2. Kubernetes and Openshift tutorial

This tutorial contains instructions on how to run Infinispan library mode (as a microservice) in Kubernetes/OpenShift.

Prerequisites: Maven and Docker daemon running in the background.

Prerequisites

  • A running Openshift or Kubernetes cluster

Building the tutorial

This tutorial is built using the maven command:

./mvnw package

Note that target/ directory contains additional directories like docker (with generated Dockerfile) and classes/META-INF/jkube with Kubernetes and OpenShift deployment templates.

Tip

If the Docker Daemon is down, the build will omit processing Dockerfiles. Use docker profile to turn it on manually.

Deploying the tutorial to Kubernetes

This is handle by the JKube maven plugin, just invoke:

mvn k8s:build k8s:push k8s:resource k8s:apply -Doptions.image=<IMAGE_NAME> 1
1
IMAGE_NAME must be replaced with the FQN of the container to deploy to Kubernetes. This container must be created in a repository that you have permissions to push to and is accessible from within your Kubernetes cluster.

Viewing and scaling up

Everything should be up and running at this point. Now login into the OpenShift or Kubernetes cluster and scale the application

kubectl scale --replicas=3  deployment/$(kubectl get rs --namespace=myproject | grep infinispan | awk '{print $1}') --namespace=myproject

Undeploying the tutorial

This is handled by the JKube maven plugin, just invoke:

mvn k8s:undeploy

Chapter 3. Spring and Spring Boot

3.1. Spring and Spring Boot tutorials

Note

These code tutorials use Data Grid Server and require at least one running instance.

Run Spring examples

Two simple tutorials can be run with Spring without Spring Boot:

  • Test caching
$ ./mvnw -s /path/to/maven-settings.xml package exec:exec@spring-caching
  • Test annotations
$ ./mvnw -s /path/to/maven-settings.xml package exec:exec@spring-annotations

To run the Spring Boot tutorials, use the following command.

$ ./mvnw -s /path/to/maven-settings.xml spring-boot:run

Displaying actuator statistics

Navigate to Content from localhost is not included.http://localhost:8080/actuator/metrics in your browser to display a list of available metrics. Cache metrics are prefixed with "cache." Display each metric for each cache using tags. For example for the 'puts' stats in the basque-names cache:

Content from localhost is not included.http://localhost:8080/actuator/metrics/cache.puts?tag=name:basque-names

Collecting statistics with Prometheus

The prometheus.yml file in this project contains a host.docker.internal binding that allows Prometheus to scrap metrics that the Spring actuator exposes.

Change the YOUR_PATH value in the following command to the directory where Prometheus is running and then run:

Podman

$ podman run -d --name=prometheus -p 9090:9090 -v YOUR_PATH/integrations/spring-boot/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

Tutorial linkDescription

Content from github.com is not included.Spring Boot and Spring Cache remote mode

Demonstrates how to use Spring Caches with Spring Boot and the Data Grid Server.

Content from github.com is not included.Spring Boot, Spring Cache, Reactor and Infinispan Server

Demonstrates how to use Spring Caches with Spring Boot Reactor and the Data Grid Server.

Content from github.com is not included.Spring Boot and Spring Session remote mode

Demonstrates how to use Spring Session with Spring Boot and the Data Grid Server.

Content from github.com is not included.Spring Boot and Spring Cache embedded mode

Demonstrates how to use Spring Caches with Spring Boot and Data Grid Embedded.

Content from github.com is not included.Spring Boot and Spring Session embedded mode

Demonstrates how to use Spring Session with Spring Boot and Data Grid Embedded.

Content from github.com is not included.Spring cache embedded without Spring Boot

Demonstrates how to use Spring Cache and Data Grid Embedded without Spring Boot.

Data Grid documentation

You can find more resources in our documentation at:

Legal Notice

Copyright © Red Hat.
Except as otherwise noted below, the text of and illustrations in this documentation are licensed by Red Hat under the Creative Commons Attribution–Share Alike 3.0 Unported license . If you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, the Red Hat logo, JBoss, Hibernate, and RHCE are trademarks or registered trademarks of Red Hat, LLC. or its subsidiaries in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
XFS is a trademark or registered trademark of Hewlett Packard Enterprise Development LP or its subsidiaries in the United States and other countries.
The OpenStack® Word Mark and OpenStack logo are trademarks or registered trademarks of the Linux Foundation, used under license.
All other trademarks are the property of their respective owners.