Understand How Your Cache Is Distributed
Watch a cache spread 10,000 entries across three nodes and then four.
Introduction
The cache API looks like a ConcurrentMap, and on a single-node cluster it behaves like one. It is not one. Entries are distributed across nodes by a hash of the key, each partition has a primary and, with backups=1, a backup on a different node. The partition layout shifts when the topology changes. A one-node development cluster hides every detail.
You make the distribution visible here. You create a partitioned cache, seed it with 10,000 entries, and use the Affinity API to ask the cluster which node owns what. Then you scale from one node to three and watch entries redistribute without any code running. Scale to four and watch it happen again.
Prerequisites
A running single-node cluster from Start a Local Cache Development Cluster
Cache API familiarity from Work with the Cache API
Java 17 or later for the client runtime. The Maven project compiles to Java 17 bytecode, the same version the server JVM runs.
Maven 3.6 or later
Docker Compose 2.23 or later
The first step in this tutorial adds replacement compose files and per-node configs to your cache-cluster/ directory. Your existing single-node setup stays in place unchanged.
What You Will Learn
In this tutorial, you:
Configure a partitioned cache with an explicit partition count and
backups=1Use
ignite.affinity(cacheName)to inspect which node owns each partitionObserve that a single-node cluster owns every primary partition and zero backups
Scale the cluster to three nodes and watch partitions redistribute without code changes
Scale to four nodes and confirm that rebalance is incremental and preserves the cache data
Predict how key-to-partition and partition-to-node mappings change under topology changes
Create the Cache-Distribution Project
Create a Maven project named cache-distribution/. It needs one source file and a pom.xml that targets Java 17 bytecode and suppresses the engine's startup logs so the program's output stands out.
Create a single source file at src/main/java/com/example/distribution/Distribution.java. The class opens a thick client, creates a partitioned cache if one does not exist, seeds 10,000 entries, and prints three views of the distribution:
Compile it:
Add the Cluster Compose Files
The single-node cluster from Start a Local Cache Development Cluster does not expose the communication port (47100) that the thick client needs for direct cache operations. The prior tutorials worked around that through the server's initial discovery channel; this tutorial opens enough connections to need the comm port too.
Create three replacement compose files plus four per-node configs in your existing cache-cluster/ directory. Your existing docker-compose.yml and ignite-config.xml stay in place and are ignored for the rest of this tutorial.
The per-node configs differ in three places: a unique discovery port, a unique communication port, and a BasicAddressResolver entry that exposes the container's hostname under 127.0.0.1 so the thick client on your host can reach each node through a distinct localhost port.
Run on one Node to see the Baseline
Stop the cluster from the previous tutorials if it is still running, then start the single-node variant you just created:
Run the program:
Expected output (trimmed; the engine prints its own startup banner above what appears here):
The node1, node2, ... labels in the output are client-side synonyms the program assigns by sorting cluster members on their consistent ID. The cluster itself does not know about them. They exist because Docker's auto-generated container IDs would otherwise show up in the output as unreadable hex strings.
Three things to notice.
The cache has 32 partitions even though only one node exists. Partition count is a cache property set at creation time. It does not change with topology. One node owns all 32 primaries because there is nowhere else to put them.
backups=1 was configured, but the summary shows zero backups. A backup on the same node as its primary would not survive the node's failure, so the engine refuses to place one there. With only one node the setting has nowhere to take effect; a second node activates it.
Key 42 lives on partition 10. RendezvousAffinityFunction hashes the key and picks a partition from the 32 available. The hash and the partition count are the only inputs, so key 42 maps to partition 10 on every run against any cluster.
Scale to Three Nodes
Stop the single-node cluster and bring up the three-node version. Each node exposes discovery and communication on a distinct host port so the thick client can reach all three.
down destroys the single-node cluster and its in-memory cache. up -d starts three fresh nodes. The next program run finds an empty cache and reseeds.
Wait for all three nodes to join. The cluster is ready when node1's logs show servers=3:
Expected output (trimmed to the interesting fields):
Re-run the same program:
Expected output:
Your exact numbers will vary. Rendezvous depends on each node's cluster-assigned identity, which Docker regenerates every restart. The invariants hold regardless:
Primary count per node sums to 32.
Backup count per node also sums to 32. Every partition now has a backup on a different node.
Entry count per node sums to 10,000.
Three things to notice.
The client code did not change. The Affinity API reads the cluster's current topology at the moment it is called, so the same primaryPartitions(node) call returns 32 on one node and about 11 on three nodes.
Key 42 has a real backup. Partition 10 sits on node1 with a backup on node2. If node1 fails, node2 is promoted and the partition stays available. That is what backups=1 buys you: survival of one simultaneous node loss per partition.
Entry counts are roughly even, not exact. Rendezvous spreads keys evenly in expectation, but any finite sample shows variance. The variance shrinks as the sample grows; a production cache with millions of entries distributes close to uniform.
Scale to Four Nodes and Watch the Data Follow
Keep the three-node cluster running. Start the four-node variant alongside it:
Docker Compose reconciles: the three running services match the four-node file exactly, so they stay up and only node4 starts. The engine detects the topology change and begins rebalance.
Confirm all four nodes are in the topology:
Expected output:
Re-run the program one more time:
Expected output:
Look at the second output line: Cache already holds 10000 entries. Skipping seed. The program found the cache still populated from the three-node seed, so the seed loop did not run. No code migrated data. No script rebalanced partitions. The engine added node4 to the topology, copied the right partitions onto it, and kept the cache serving reads and writes throughout.
Three things to notice.
The cache survived the scale event. Adding a server to a live cluster did not require an application-layer migration, a read-modify-write loop, or any special coordination from the client. The 10,000 entries that landed on three nodes in the previous step are still there, now spread across four.
Rebalance is incremental, not wholesale. Each existing node gave up a few primary partitions to node4. No node moved all of its partitions. Adding one node to a ten-node cluster would move roughly a tenth of the partitions, which keeps the cost bounded for large clusters.
The key-to-partition mapping is still deterministic. Key 42 is still on partition 10. In this run partition 10 moved from node1 to node4, with node1 now holding the backup. In your run it might stay on node1 or land somewhere else; the Rendezvous function reassigns partitions based on the new topology. Either way the key is in the same partition it has always been.
Summary
You have the mental model for every cache on any cluster, including a single-node development cluster where the distribution is invisible.
The cache is partitioned. Every key hashes to a partition. Every partition has a primary and, with backups=1, a backup on a different node. The partition count is a cache property set at creation time. Which node owns a given partition depends on the live topology and shifts when nodes join or leave.
A single node owns everything because it has to. The partitions exist from the moment the cache is created, but with one node there is no other node to spread to. A second node activates backups=1.
Rebalance is automatic, incremental, and loss-preserving. When a node joins or leaves, the engine reassigns the minimum number of partitions to match the new topology, copies the underlying entries, and keeps the cache serving reads and writes.
32 is a teaching number, not a production number. The default of 1024 is the right choice for most workloads. You picked 32 so the per-node counts stayed small enough to count. A later guide covers when and how to change the default.
Beyond Key-Value and the Design for Data Locality path build directly on this mental model. The next layer is controlling placement, and control starts with knowing what the engine does on your behalf.
What's Next
From Key-Value to SQL: The JOIN Problem uses partitioning directly. JOIN correctness depends on putting related rows in the same partition, and the tutorial walks through the failure mode before showing the fix.
Design for Data Locality, a dedicated path for readers who will design a production schema, covers affinity keys, compute gravity, and the tradeoffs of partition count.
How to Choose a Partition Count for Your Workload covers the 1024 default and when to deviate.
Related
Use Transactions with the Cache API added multi-key atomicity to the Foundations path. Transactions and distribution combine in the Beyond Key-Value path, where colocated writes commit faster than cross-node writes.
Work with the Cache API introduced the thick client used throughout.
This page is: Copyright © 2026 MariaDB. All rights reserved.
Last updated
Was this helpful?

