For the complete documentation index, see llms.txt. This page is also available as Markdown.

Work With the Cache API

Connect a Java thick client to your cache-centric cluster, create distributed caches, and perform put, get, and bulk operations with IgniteCache.

Introduction

Your cluster is running. Now you connect a Java application and store data in it.

The central interface in the cache-centric architecture is IgniteCache<K,V>. If you have used java.util.concurrent.ConcurrentMap, the API will feel familiar: put, get, remove, putAll, getAll. The difference is that the data lives on the cluster instead of in a single JVM. By the end of this tutorial, you will see that the distributed cache follows the same Java collections contract applied across a cluster.

In this tutorial, you create a Maven project, connect a thick client to your single-node cluster, and perform individual and bulk cache operations using sample records from the Music Store dataset you work with throughout this learning path. The sample data is defined in the code, so no dataset loading is required. By the end, you will have a working Java application that stores and retrieves data from a distributed cache.

This tutorial labels itself "beginner" for GridGain 8. The surrounding Java stack (Maven, JDBC, Spring XML) is assumed prior knowledge. If any of those is new, skim the linked primer before starting.

Prerequisites

  • A running single-node cluster from Start a Local Cache Development Cluster

  • Java 17 or later

  • Maven 3.6 or later

  • Comfort reading Spring bean XML. The ignite-config.xml is Spring-formatted but you only edit a handful of properties.

Returning to these tutorials? Verify your cluster is running.

Check that the cluster container is up:

docker ps --filter name=gridgain8-node1 --format "table {{.Names}}\t{{.Status}}"

Expected output:

NAMES             STATUS
gridgain8-node1   Up X hours

If the container is stopped, restart it from the directory containing your docker-compose.yml:

docker compose -f cache-cluster/docker-compose.yml start

If the cluster was destroyed (docker compose down), recreate it:

docker compose -f cache-cluster/docker-compose.yml up -d

The cluster runs in-memory. Destroying and recreating it starts with an empty cache.

What You Will Learn

You will:

  • Connect a thick client that joins the cluster ring

  • Create a distributed cache and store data with put and get

  • Remove entries and observe the result

  • Store and retrieve multiple entries in a single call with putAll and getAll

Set up the Maven Project

Create a project directory alongside your cache-cluster directory and add a pom.xml:

Create cache-client/pom.xml:

GridGain 8 artifacts are published to GridGain's Nexus repository, not Maven Central. The <repositories> block tells Maven where to find the gridgain-core dependency.

The gridgain-core artifact contains the thick client, the cache API, and the discovery layer. A single dependency is all you need to connect to the cluster and perform cache operations.

The --add-opens flags in the exec plugin give the GridGain client reflective access to JDK internals. The thick client uses sun.nio.ch for direct memory buffers and java.nio for network I/O, and java.io for object serialization. Without these flags, Java 16+ blocks the access and the client fails to start.

The exec.mainClass property in <properties> lets you override the main class from the command line with -Dexec.mainClass=com.example.OtherClass. You use this to run different classes in later steps.

This project uses exec:exec (not exec:java). The exec:exec goal launches a separate JVM process, which is the only way to pass --add-opens flags. The exec:java goal runs inside Maven's JVM and ignores JVM arguments in the plugin configuration.

Connect a Thick Client to the Cluster

The GridGain thick client is a Java process that joins the cluster's discovery ring as a non-data member. It participates in the topology directly: it receives the partition map (the lookup table that maps each key's hash to a specific server node), routes operations to the node that owns the data, and can enable features like near-cache that keep frequently accessed data in the client's own memory.

GridGain 8 also offers a thin client (IgniteClient), which connects over a lightweight TCP socket without joining the topology. The thick client is the standard choice for this generation because it supports the full API surface: compute, service grid, continuous queries, and near-cache. The thin client was added later and covers a narrower set of operations.

In GridGain 9, the thin client is the primary client API. In GridGain 8, the thick client fills that role.

Create cache-client/src/main/java/com/example/CacheConnect.java:

The connection code has three parts:

  • TcpDiscoveryVmIpFinder configures static IP discovery. The address 127.0.0.1:47500 matches the published discovery port from your Docker Compose file. In the cluster setup tutorial, the XML configuration used node1:47500 because containers discover each other by Docker hostname. Here, your Java application runs on the host machine and connects through the published port.

  • setClientMode(true) tells the node to join the ring without accepting data partitions (the subsets of cached data distributed across server nodes). Your client sends operations to the server, which handles all the storage.

  • Ignition.start(cfg) starts the client node and blocks until it joins the ring. The try-with-resources block ensures the node leaves the ring cleanly when your program exits.

Compile and run:

The GridGain client produces startup output before your application code runs. Most of it is informational. Look for the topology snapshot line:

The GridGain 8 topology snapshot includes an aliveNodes array listing every node in the cluster with its ID, client/server role, and version. The important fields to look for are servers=1, clients=1, state=ACTIVE.

The topology snapshot now shows clients=1. Your thick client has joined the ring. The "Cluster nodes: 2" count includes both the server node and your client node.

The thick client prints performance suggestions, version checks, and configuration warnings on startup. These are safe to ignore. GridGain 8 is verbose: it logs the full configuration, classpath, exchange timings, and connection details at INFO level by default. Scan past the noise and look for the topology snapshot line showing servers=1, clients=1.

The thick client may take a few seconds to connect on the first run. The client discovers the server node's internal Docker IP address (e.g., 172.20.0.2), tries to connect to it, times out, and then falls back to localhost. The setFailureDetectionTimeout(1000) in the code above reduces this wait from the default 10 seconds to 1 second. You may still see a Connection timed out warning in the log. This is a Docker networking artifact, not a product performance issue. Production deployments where the client and server share a network do not experience this delay.

Create a Cache and Store Data

IgniteCache<K,V> is the primary interface for distributed key-value storage. It extends the same contract as java.util.concurrent.ConcurrentMap: you put a key-value pair, and you get the value back by its key. The difference is that the data is distributed across the cluster. With one server node, all data lives on that node. When you add nodes later, the data partitions across them automatically.

Each class in this tutorial includes the same connection setup. In a production application, you would extract this into a shared utility. Here, keeping each file self-contained means you can run any step independently.

Create cache-client/src/main/java/com/example/CacheCrud.java:

Run it:

After the GridGain startup output, you see:

Key behaviors to note:

  • getOrCreateCache("customers") creates a named cache on the cluster or returns the existing one. The name is the cache's identity across the cluster. Any client that connects and requests "customers" accesses the same distributed data

  • Cache naming works like connecting to a named database or collection. The name is a string, and the cache persists on the cluster as long as the cluster is running

  • Default configuration creates a PARTITIONED cache with zero backup copies. The cache modes section below explains what this means

  • Each put and get is a network round-trip to the server node. After remove(2), a get(2) returns null because the entry no longer exists

Perform Bulk Operations

The previous step stored three customers with three separate put calls. Each call was a network round-trip to the server. Storing 10 customers the same way means 10 round-trips. Storing 1,000 means 1,000 round-trips.

putAll and getAll batch multiple entries into a single network call. The cost model shifts from one round-trip per entry to one round-trip for the entire batch. This is the same principle behind JDBC batch inserts: amortize network latency across many operations instead of paying it once per row.

Create cache-client/src/main/java/com/example/CacheBulk.java:

Run it:

putAll takes a Map<K,V> and stores every entry on the cluster. This method comes from the Map interface, the same putAll that HashMap provides. getAll is a GridGain addition that retrieves multiple keys in a single cluster call and returns a Map<K,V> of the results. The TreeMap wrapper sorts the retrieved entries by key so the output is deterministic regardless of the order the server returns them.

With a single server node, both calls go to the same node. In a multi-node cluster, the client partitions the batch by key hash and sends each subset to the owning node in parallel. The bulk methods handle the fan-out and collection transparently.

Cache Modes and Backups

Every cache has a mode that determines how data is distributed across the cluster. The two modes are PARTITIONED and REPLICATED.

PARTITIONED (the default) assigns each key to one primary node by hashing the key. With multiple server nodes, different keys land on different nodes. This is how the cache scales horizontally: adding nodes increases total memory and throughput because each node holds a fraction of the data. With a single node, all keys land on the same node, so you do not observe the partitioning effect yet.

REPLICATED stores a full copy of every entry on every server node. Reads are always local because every node holds the full dataset. Writes carry higher cost since each entry must propagate to every server node. Replicated caches work well for small, read-heavy datasets like lookup tables or configuration data.

Backups are redundant copies of each partition. The default is zero backups, which means losing a node loses the data on that node. Production deployments typically set backups to 1 or higher. Cache configuration (including mode and backups) is covered in a later tutorial when you work with CacheConfiguration.

The cache you created in this tutorial used the default: PARTITIONED mode with zero backups. All the operations worked the same way they would on a multi-node cluster. The only difference is that a multi-node cluster distributes the keys across nodes, which you observe when you add nodes in a later tutorial.

How does this compare to a database?

A common question when evaluating a caching layer is whether it is faster than the database for simple lookups.

A warm MariaDB primary-key lookup on a localhost hop is typically faster than a single cache hop for the same query. A prepared statement on an indexed primary key over a fully cached buffer pool is the best-case scenario for a relational database. The cache adds latency that a direct database call avoids: partition mapping, serialization through the binary object format, and topology awareness checks. On a single node with a small dataset, these costs are not offset by any distributed advantage.

The cache's value here is not the single-operation latency. It is the ability to absorb volume so the database has capacity for everything else. Caching wins in different scenarios:

  • Read amplification: When multiple application instances read the same data, the cache absorbs the load instead of the database. This is the most common reason to add a caching layer

  • Distributed scale: With data partitioned across nodes, each node handles a fraction of the requests. Total throughput scales with node count

Later tutorials in this path cover additional capabilities where the performance gap reverses:

  • Near-cache: The thick client keeps frequently accessed data in its own JVM memory, eliminating the network hop entirely

  • Compute colocation: Processing logic runs on the node that holds the data, avoiding network transfer

A later tutorial in this path connects the cache to a MariaDB database under sustained read load and demonstrates the cache-aside pattern where these advantages become measurable.

Summary

You built a Java application that connects to your cache-centric cluster as a thick client and performs distributed cache operations. The key concepts:

  • Thick client joins the cluster ring as a non-data member, routing operations directly to the owning server node

  • IgniteCache<K,V> is the distributed equivalent of ConcurrentMap. The same put, get, remove methods work whether the data lives in one JVM or across a cluster

  • Bulk operations (putAll, getAll) amortize network round-trips. One call for N entries instead of N calls for N entries

  • PARTITIONED mode (the default) distributes keys by hash. REPLICATED mode copies all data to every node

Next Step

The next tutorial, Cache Sizing and Expiry, solves the two problems that make an unbounded cache unusable in production. You add LRU eviction to bound the on-heap tier, set TTL with a JCache ExpiryPolicy, and watch a real-time snapshot of the cache shrinking itself as entries leave under both rules.

This page is: Copyright © 2026 MariaDB. All rights reserved.

spinner

Last updated

Was this helpful?