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.xmlis Spring-formatted but you only edit a handful of properties.
What You Will Learn
You will:
Connect a thick client that joins the cluster ring
Create a distributed cache and store data with
putandgetRemove entries and observe the result
Store and retrieve multiple entries in a single call with
putAllandgetAll
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.
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.
Create cache-client/src/main/java/com/example/CacheConnect.java:
The connection code has three parts:
TcpDiscoveryVmIpFinderconfigures static IP discovery. The address127.0.0.1:47500matches the published discovery port from your Docker Compose file. In the cluster setup tutorial, the XML configuration usednode1:47500because 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. Thetry-with-resourcesblock 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.
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 dataCache 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
putandgetis a network round-trip to the server node. Afterremove(2), aget(2)returnsnullbecause 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.
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 ofConcurrentMap. The sameput,get,removemethods work whether the data lives in one JVM or across a clusterBulk operations (
putAll,getAll) amortize network round-trips. One call for N entries instead of N calls for N entriesPARTITIONED 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.
Last updated
Was this helpful?

