When to Break Colocation
Reference data belongs in REPLICATED caches.
Introduction
The previous three tutorials colocated Customer and Invoice caches so every invoice lands on the same primary node as its customer. That design is correct for fact tables that share a join key. It is wrong for the reference data the same query needs to enrich the result.
Reference tables are small and read-often: a country list, a genre catalog, a media-type lookup. Every analytical query joins against them, and none of them grow with the fact volume. Partitioning a 30-row Country table across 32 partitions and 3 nodes produces partitions that hold one row each and forces the SQL engine to shuffle rows across the network on every join. The cache mode that fits these tables is CacheMode.REPLICATED. Every node keeps a full copy, and every JOIN reads it locally.
This tutorial builds three versions of the same star-schema report and runs the identical query against each. Version A puts every cache in CacheMode.PARTITIONED without colocation. Version B adds fact-fact colocation but keeps the dimensions partitioned. Version C moves the dimensions to CacheMode.REPLICATED. All three versions return correct results with SqlFieldsQuery.setDistributedJoins(true). Version C runs 1.8x to 2.8x faster than Version A because the dimension tables no longer shuffle.
Star schema is a standard dimensional-modeling pattern: one fact table surrounded by small dimension tables, each joining on one foreign key. Readers who need a refresher can check the Wikipedia article on star schema. This tutorial uses the pattern as a vehicle for the cache-mode decision rather than as a lesson in dimensional modeling.
Prerequisites
Completion of Affinity-Aware Compute at Scale. You have declared colocation with
@AffinityKeyMapped, verified it with runtime checks, and computed against it. You have a working 3-node cluster.A running 3-node cluster from the previous tutorials. No configuration change is required.
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.
This tutorial builds a fresh Maven project called star-schema/ alongside affinity-compute/, verify-colocation/, and colocation-keys/. The cluster setup is unchanged.
What You Will Learn
Create three cache-mode variants of the same star schema in one Maven project
Observe the silent-wrong-answer failure mode when
setDistributedJoins(true)is off on a multi-table JOINMeasure the cost of the flag with and without fact-fact colocation
Move dimension tables to
CacheMode.REPLICATEDand measure the speedupApply a decision framework for reference data: row count, write rate, access pattern
Create the Star-Schema Project
Create a Maven project named star-schema/ with the layout:
The pom.xml needs ignite-indexing in addition to ignite-core. The SQL engine lives in the indexing module, and this tutorial runs a four-table SQL query.
The client-side connection config matches the previous tutorials.
The fact-side value objects carry the fields the SQL query needs. Customer has one foreign key to Country. Invoice has two foreign keys, one to Genre and one to MediaType. The @QuerySqlField annotations publish the fields to the SQL engine so the JOINs can reference them by name.
The dimension value objects are two-field POJOs. They look almost identical. Each has a code (the primary key) and a human-readable name.
Genre.java and MediaType.java follow the same shape. Save all three value classes and the Dimensions.java utility below in the same directory.
Three Cache Configurations, One Factory Class
Each of the three versions uses five caches: Customer, Invoice, Country, Genre, MediaType. Across versions, the cache names are suffixed with _a, _b, or _c. The configurations differ in two dimensions:
Invoice key. Composite
InvoiceKeyfor colocation with Customer, or plainIntegerfor no colocation.Dimension cache mode.
CacheMode.PARTITIONEDorCacheMode.REPLICATED.
The table-name suffix (Customer_A, Customer_B, Customer_C) keeps the three versions' SQL tables in separate namespaces so the report query can name exactly one version at a time without collision.
The data loader seeds one version's caches at a time. It reads the dimension rows from Dimensions, then streams 10,000 customers and 30,000 invoices into the fact caches. Customer country codes distribute round-robin across the 30 countries. Invoice genre and media-type codes distribute uniformly across their dimensions. Revenue is deterministic so the report query produces the same top-10 numbers across versions and runs.
The Report Query and the Three-Version Runner
One class runs all three versions. ReportQuery creates each version's caches, loads the data, runs the query, destroys the caches, and moves to the next version. The SQL is identical across versions, and only the table-name suffix changes. Version A's first pass leaves the flag off to reproduce the wrong-answer failure mode. Every other pass turns the flag on.
Run the full experiment:
The first run pays startup costs for the DataStreamer client, so the first version's load is slower than the next two. Wall-clock times for the queries vary ±10% across back-to-back runs, but the ordering is stable.
Expected output. Row totals are identical across runs because the data generator is deterministic. Wall-clock times vary with the hardware. Version A's no-flag wrong-answer sums can also vary because the SQL engine's partial-aggregation path is planner-dependent.
Read the Three Versions
Version A, no flag. Top-10 revenue cells sit below $50,000 each. Every correct version shows cells around $175,000. The no-flag pass returns a real top-10 sorted correctly, but the underlying sums are wrong. The wall-clock is fast because the query does less work. The SQL engine on each node joins only local primary partitions and does not shuffle rows across the network. The partitioned Country and Genre caches each hold 30 or 10 rows split across three nodes, so any given node's map phase finds only some of the dimension rows it needs. The per-node partial aggregates do not merge into the correct global total.
This is the same failure mode the earlier tutorials called out. PARTITIONED caches joined without colocation and without setDistributedJoins(true) return silent partial results. A reader who trusts the output ships the revenue figure in a report and discovers weeks later that half the countries are undercounted.
Version A, flag on. Same schema, correct answer. The flag tells the SQL engine to shuffle rows across the network so every JOIN sees the full dataset. Every fact-fact and fact-dimension pair gets a cross-node merge. The dominant cost is the fact-dimension shuffle. Four tables with 30,000 invoice rows, 10,000 customers, and tiny partitioned dimension tables all move over the network to satisfy the query planner. This is the slowest correct design.
Version B, flag on. Invoice now colocates with Customer via @AffinityKeyMapped, so the fact-fact JOIN runs locally on each node instead of shuffling. The fact-dimension JOINs against partitioned Country, Genre, and MediaType caches still cross the network under the flag. The wall-clock improvement is modest. The fact-fact JOIN was a small share of the total cost, and the fact-dimension shuffle remains the main cost.
Version C, flag on. The fact-fact JOIN is still colocated. The dimension caches are now REPLICATED, so every node has a full copy of Country, Genre, and MediaType. The fact-dimension JOINs read dimension rows from local memory, and no dimension row crosses the network. setDistributedJoins(true) is still set because the SQL engine requires it for correctness on this query shape, but the flag's cost drops because the dimension rows stopped moving. This is the fastest correct design.
The results tell a clear story. Version A is the slowest correct run, Version C is the fastest, and the no-flag runs are wrong. On GridGain 8, Version C is about 2.8x faster than Version A. The REPLICATED design pays off when the shuffle is expensive.
The "Small Enough" Decision Framework
CacheMode.REPLICATED puts every row on every node. That is fast for reads and expensive for writes. A 30-row Country table costs almost nothing to replicate: 30 × number-of-nodes is still tiny. A 30-million-row product catalog would multiply by node count into billions of rows of duplicated memory and amplify every write by three, thirty, or three hundred depending on cluster size.
Three questions decide whether a table should be REPLICATED:
Row count
Under ~10,000 rows
Above ~100,000 rows
Write rate
Rare (updates per day or per week)
Frequent (updates per second)
Access pattern
Joined by many queries
Rarely joined
Between those extremes, measure. The GridGain 8 cluster used for this tutorial has three nodes, and adding a 10,000-row dimension to REPLICATED costs ~10,000 rows times three nodes of memory. On a 30-node cluster the same cache costs ten times as much memory. Write amplification grows with cluster size. One write to a REPLICATED cache hits every node, so on a 30-node cluster one write becomes thirty writes.
Concrete examples from the dimensions in this tutorial and from real applications:
ISO country codes
~30
Yearly
REPLICATED
Music genres
~10-100
Quarterly
REPLICATED
User roles
~10-50
Rare
REPLICATED
Feature flags
~100-500
Per-release
REPLICATED
Product catalog (e-commerce)
10k-10M
Hourly
PARTITIONED
Customer master
100k-100M
Per-request
PARTITIONED
Audit log
grows forever
Per-second
PARTITIONED
Reference data that changes slowly and participates in many queries is the canonical fit. Frequently-written tables or tables that grow with user activity are not.
REPLICATED cache startup cost. A new node joining the cluster must receive the full content of every REPLICATED cache before it can serve reads. On a slow network or with a large REPLICATED cache, node startup stretches from seconds to minutes. Keep REPLICATED caches small enough that the cluster tolerates a node restart within its availability budget.
Write amplification. Every write to a REPLICATED cache applies on every node. On a 3-node cluster, one write is three writes. On a 30-node cluster, one write is thirty. A table with even a few writes per second becomes an expensive cache mode choice as cluster size grows.
Silent partial results without setDistributedJoins(true). Any multi-table JOIN on this query shape needs the flag for correctness, regardless of whether the dimensions are REPLICATED. The REPLICATED design reduces the cost of the flag, but it does not eliminate the need for it. Production code that runs multi-cache analytical queries should always set the flag, and rely on REPLICATED dimensions to keep its cost manageable.
What This Means
Four tutorials close the Design for Data Locality arc.
Design Keys for Colocation declared the colocation contract on fact tables. Verify Colocation Is Working proved the contract held at runtime. Affinity-Aware Compute at Scale ran compute jobs on the nodes that owned the facts. This tutorial closed the loop by showing which tables belong outside the colocation contract.
The schema that results matches what a production cache-centric system looks like: partitioned fact tables with colocation chains where they share keys, replicated dimension tables for reference data, and setDistributedJoins(true) on the multi-cache analytical queries that cross the boundary. The reader who built all four tutorials can design that schema end-to-end, verify it at runtime, and reason about its cost under load.
Summary
CacheMode.REPLICATED and CacheMode.PARTITIONED are both correct, for different tables. Facts go in PARTITIONED caches with colocation chains declared by @AffinityKeyMapped or an equivalent mechanism. Reference tables go in REPLICATED caches when they are small, written rarely, and joined often. The three-version experiment in this tutorial moves a schema through three designs and measures a 1.8x to 2.8x speedup between the naive partitioned-everywhere design and the partitioned-facts plus replicated-dimensions design, depending on the run.
SqlFieldsQuery.setDistributedJoins(true) is the correctness flag for multi-cache JOINs. On the latest GridGain 8 version, the flag is required whenever a query crosses two or more caches whose join keys do not all share an affinity chain. Without it, the query returns silent partial results. The no-flag Version A run in this tutorial returned top-10 revenue rows with totals at roughly one-fifth to one-quarter of correct, a failure mode that looks like a normal result unless the reader sums the numbers against a baseline.
REPLICATED dimensions do not remove the need for the flag. They reduce its cost. Version C runs the same multi-table JOIN 1.8x to 2.8x faster than Version A because the dimension rows live on every node instead of shuffling across the network. The decision framework for REPLICATED is three questions: row count, write rate, access pattern. Tables at the small-and-stable end of all three belong in REPLICATED caches. Tables that fail any one of the three belong in PARTITIONED.
What's Next
This is the final tutorial in the Design for Data Locality path. Two supporting guides build on it:
How to Choose a Partition Count for Your Workload covers the tradeoffs between the 32-partition setup used in this path and the 1,024 default, and how the decision shifts as cluster size grows. Opens in a new tab so this tutorial stays in your sidebar.
How to Debug a Hot Partition diagnoses the failure mode where one node carries most of the load because an affinity key skews toward one partition. Opens in a new tab.
Related
Design Keys for Colocation introduced
@AffinityKeyMappedand the composite-key mechanism this tutorial reuses for Versions B and C.Verify Colocation Is Working taught the runtime checks for affinity contracts. Apply them to Version B and Version C to confirm Invoice colocates with Customer as expected.
Affinity-Aware Compute at Scale covered three affinity-aware compute patterns that run against PARTITIONED caches. They compose with REPLICATED dimensions when the compute needs to enrich results.
This page is: Copyright © 2026 MariaDB. All rights reserved.
Last updated
Was this helpful?

