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

Distributed Primitives: The Coordination Problem

Acquire cluster-wide IgniteAtomicLong, IgniteLock, and IgniteQueue primitives by name. Three JUC-shaped APIs replace the libraries an application would otherwise import to coordinate across processes.

Introduction

So far in this path the cluster has acted as a data service. You stored entries, queried them, ran compute against them, and learned how to make the storage durable. This tutorial shifts the cluster's role to a coordination service that the application talks to instead of a coordination library.

A team that needs cluster-wide coordination from a key-value cache pieces it together from a few sources. Counter increments and blocking queue pops typically come from the cache server itself. Distributed locks come from a separate library, with correctness tradeoffs the application is left to defend.

GridGain exposes three JUC-shaped primitives on the Ignite interface. IgniteAtomicLong mirrors the JDK AtomicLong method surface (get, incrementAndGet, compareAndSet), with the difference that every node in the cluster sees the same value through the same coordination path. IgniteLock is a cluster-scoped ReentrantLock, and IgniteQueue is a cluster-scoped BlockingQueue. You build a small project with one runnable class per primitive and watch each one work across two clients.

Prerequisites

  • A running three-node cluster using cache-cluster/docker-compose-3nodes.yml from Understand How Your Cache Is Distributed. The per-node configs in that compose pin discovery and communication ports and register a BasicAddressResolver so a host JVM thick client can reach each container through the loopback address. Peer class loading is enabled on every server.

  • Java 17 or later for the client runtime. The Maven project compiles to Java 17 bytecode, the same version the GridGain 8 server JVM runs.

  • Maven 3.6 or later.

  • Docker Compose 2.23 or later.

  • Two terminal windows. The Lock and Queue scenarios run a second JVM in the second terminal.

This tutorial creates a fresh distributed-primitives/ Maven project. The clusters and projects from earlier tutorials in this path are not modified.

Returning to this tutorial? Restart the three-node cluster.

If the three-node cluster from the earlier tutorials is no longer running, start it before continuing.

docker compose -f cache-cluster/docker-compose-3nodes.yml up -d
docker ps --filter name=gridgain8-node --format "table {{.Names}}\t{{.Status}}"

The output lists three server containers in the Up state. The cluster is in-memory, so any prior data was discarded with the previous session. The primitives this tutorial creates do not depend on existing data.

What You Will Learn

  • How to acquire a cluster-wide primitive by name with a configuration object and a create-if-absent flag

  • Why IgniteAtomicLong is JDK AtomicLong made cluster-wide and how cross-client visibility works

  • How IgniteLock contends across two JVMs and why the wall-clock prints prove serialization

  • How IgniteQueue.take() blocks when empty and unblocks when another process offers

  • Why CollectionConfiguration.setBackups(1) is required when an AtomicLong already shares the cache group

What You Will Build

You build a small Maven project with four Java classes. Three are runnable scenarios that connect a thick client to the three-node cluster and exercise one primitive each. The fourth is a code-only reference that lists the other primitives the cluster exposes with a one-line use case for each.

By the end, you run a counter that two clients share, watch two terminals contend for the same named lock, and watch a producer feed work to a consumer that was waiting on take(). None of these scenarios use a coordination library. The cluster is the coordination service.

Set up the Project

Create the distributed-primitives/ Maven project alongside any other project directories from earlier tutorials. The pom uses the same Java 17 bytecode target as the rest of the path so the host client and the cluster servers agree on class file format.

distributed-primitives/pom.xml

GridGain 8 artifacts are not on Maven Central. The <repositories> block points Maven at the GridGain external Nexus.

Build the project once to confirm dependencies resolve and the empty source tree compiles:

The first run downloads the GridGain core jar; subsequent runs reuse the local cache.

Cluster-Scoped AtomicLong

IgniteAtomicLong is java.util.concurrent.atomic.AtomicLong with one extra property. Every node in the cluster sees the same value. Acquire the primitive by name from any client and the cluster routes the operation to the same backing entry.

The first scenario opens two clients in one JVM. Client A creates a counter named page-views, increments it 1000 times, and reports the final value. Client B then attaches as a separate Ignite instance, reads the counter by name, and increments once more. Client A reads again to confirm B's increment is visible.

A single JVM is enough for this scenario. The second Ignite instance is already a separate cluster client, and the cluster does not distinguish two clients in one process from two clients in two processes. The Lock and Queue scenarios that follow use two JVMs because contention and blocking are only observable across processes.

Run it:

Expected output (cluster-startup lines elided):

Client A's 1000 increments produce the expected final value because every incrementAndGet is an atomic cluster-wide operation. No two increments interleave to lose a count. Client B reads 1000 after attaching as a separate Ignite instance, which is the cluster-wide visibility property. Client A's last line reads 1001 because the same counter handle on A sees the value B wrote.

The name page-views is the contract. Any code on any node that calls ignite.atomicLong("page-views", anyInitialValue, anyCreateFlag) receives the same primitive. The initial value matters only on first creation. Pass true for the create flag to create the primitive when it does not yet exist, or false when the calling code asserts that the primitive must already be there. Client B's create=false is the documenting form. After client A has created the counter, B's call asserts that the counter must already exist.

One named primitive lives in the cluster, and two clients on different nodes each see the same value:

Cluster-Scoped Lock

IgniteLock is java.util.concurrent.locks.Lock with cluster scope. Two thick clients running in different processes contend on the same name. Whichever JVM finishes its cluster join first acquires the lock, and the other blocks in lock.lock() until the holder releases.

The acquire signature is ignite.reentrantLock(name, failoverSafe, fair, create). The failoverSafe flag controls what happens when the lock holder's node leaves the cluster. With true, the cluster hands the lock to the waiters automatically. With false, the cluster interrupts the waiters and surfaces the holder's failure. The fair flag chooses between FIFO ordering (true, slower but starvation-free) and unordered grant (false, faster but a busy contender can keep winning). create=true creates the lock if absent.

The hold duration in this scenario is 20 seconds. Cluster join cold-start on the host JVM is roughly 10 seconds, so with both terminals starting at the same time both JVMs are in the cluster while the first holder is still inside its hold. That overlap makes the contention observable.

Open two terminals and run the same command in each, as quickly as possible:

Terminal A output (the JVM that won the race):

Terminal B output (the JVM that blocked):

Read the timestamps left to right. Both terminals call requesting lock within 13ms of each other. Terminal A's cluster join completes first and lock.lock() returns at 20:32:46.364. Terminal B's lock.lock() does not return until 20:33:06.519, which is 153ms after A's release at 20:33:06.366. The 20-second gap on B's side is the wall-clock proof that the lock serialized the two clients.

Which JVM acquires first depends on cold-start jitter. Exactly one holds the lock at a time, and the other blocks the full hold duration. Reverse the terminals or stagger the start by a couple of seconds, and the same shape repeats with the roles swapped.

Cluster-Scoped Queue

IgniteQueue is java.util.concurrent.BlockingQueue with cluster scope. A producer in one JVM offers items, and a consumer in a different JVM takes them off the same named queue. A call to take() parks the consumer thread on an empty queue, and the cluster wakes it when an offer arrives from any client.

One configuration detail is load-bearing here. Acquire the queue with a CollectionConfiguration that has setBackups(1). The default CollectionConfiguration has 0 backups, but the AtomicLong from the previous step already created the shared cache group with 1 backup (the default for AtomicConfiguration). Both primitives' backing caches share the cluster's default-ds-group cache group, and group attributes must agree across caches. Without setBackups(1) on the queue, the cluster refuses the second primitive's creation:

This is the cluster's storage configuration leaking through the API. The fix is a single setBackups(1) call. Primitives that share a cache group inherit the group's configuration, so the queue cannot pick its own backup count once the AtomicLong has set one.

Run the consumer in one terminal first so it joins the cluster and parks on take():

After the consumer prints [consumer] starting takes at ..., run the producer in the second terminal:

Producer output:

Consumer output:

The consumer's first waited 12682 ms covers the producer's cluster join (about 10 seconds) and the producer's one-second pre-offer pause. After that, the wait pattern matches the producer's offer cadence. Each take returns within 50ms of the corresponding offer. The queue size 0 line on every producer offer means the consumer drained each item before the producer could read the local size. Cross-process delivery is sub-millisecond once both clients are connected.

take() is the BlockingQueue contract. The thread parks, and the cluster wakes it when an offer arrives at the queue's backing cache. poll() is the non-blocking alternative, returning null immediately on an empty queue. offer() always returns true on this configuration because capacity 0 means unbounded. On a bounded queue, offer() returns false when full. The blocking variant for that case is put(), which waits until space is available.

Other Primitives at a Glance

The three primitives in this tutorial cover the most common cluster-coordination uses (counter, lock, queue). The Ignite interface exposes six more, all acquired with the same ignite.<name>(...) shape. Each call takes a name, an optional initial state, an optional configuration object, and a create-if-absent flag.

Compile it to confirm the imports resolve and the call sites match the API on your version:

BUILD SUCCESS confirms the imports resolve and every call site compiles against the version on the classpath. The class is reference material, not an executable. The product docs cover each primitive's semantics in depth.

Type ignite. on a thick-client Ignite reference in your IDE and trigger auto-complete. Every coordination primitive the cluster offers is on that interface: atomicLong, atomicSequence, atomicReference, atomicStamped, countDownLatch, semaphore, reentrantLock, queue, set. The cluster's coordination toolkit fits on one screen.

Summary

Two ideas carry forward.

Cluster-wide primitives are named. Every primitive the cluster exposes is acquired with ignite.<primitive>(name, ...). Pass the same name from any client on any node, and receive the same primitive. Storage mode, backups, capacity, fairness, and failover behavior live on the configuration object passed at creation. The name is the contract.

JUC-shaped APIs are familiar. IgniteAtomicLong matches the method surface of java.util.concurrent.atomic.AtomicLong, IgniteLock extends java.util.concurrent.locks.Lock, and IgniteQueue extends java.util.concurrent.BlockingQueue. The Java developer's existing JUC vocabulary applies directly. The cluster handles the distribution, replication, and failover behind those interfaces.

The deeper shift is architectural. The cluster has been a data service throughout the rest of the path: storage, queries, compute, durable persistence. This tutorial uses it as a coordination service. The application no longer needs a separate coordination library for counters, locks, or queues across processes, because the cluster already offers JUC-shaped primitives that span every node. For a team that has been wiring distributed locks together with a separate library, the substitution is direct. A cluster-scoped Lock lives next to the data the application is already coordinating around, and the application stops carrying the integration.

Three adjacent topics belong in their own content:

  • AtomicConfiguration and CollectionConfiguration tuning, including atomicity mode, custom cache groups, and how to isolate primitives that need different backup counts so they do not collide on default-ds-group.

  • Queue capacity and bounded behavior, including put() blocking on a full queue, offer(timeout) for bounded waits, and how to size capacity for backpressure.

  • The other six primitives, particularly IgniteCountDownLatch for fan-out coordination, IgniteSemaphore for bounded concurrency across nodes, and IgniteAtomicSequence for high-throughput ID generation.

The official primitive reference covers these in depth: Data Structures.

What's Next

The next tutorial in the path, Real-Time Event Processing: The Streaming Problem, turns the question around. Instead of asking the cluster for state through atomic reads or blocking takes, register a continuous query and let the cluster push state changes to the client as they happen.

  • Compute Where the Data Lives is the previous shift in the cluster's role: from storage to in-place computation. Coordination primitives often pair with compute, for example a worker that uses IgniteSemaphore to bound concurrent calls into a downstream system.

  • Beyond Cache: The Persistence Problem makes the cluster a durable store. Combined with this tutorial, the cluster is a durable coordination service. Queues, sets, and atomics survive a cluster restart when persistence is enabled on the data region the primitive's backing cache lands in. Locks, semaphores, and count-down latches live in a volatile data region and do not survive.

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

Last updated

Was this helpful?