Caches and cache modes
Distributed, replicated and local caches, how many owners an entry has, and how eviction and expiration remove entries.
A Data Grid cluster does not have one behaviour. Each cache has a mode, an owner count, and a rule for forgetting entries. Getting those three wrong is how a cache either melts one pod or silently drops the session people needed. The cluster from Data Grid on OpenShift is the place to try them. Changing a template on a Cache that already has data can force the operator to recreate it. Recreation deletes the entries. Do this on catalog, which you can refill.
Three modes
| Mode | Where an entry lives | Use it for |
|---|---|---|
| Distributed | On owners pods, partitioned by key |
Almost everything. The cache can be larger than one pod. |
| Replicated | Every pod | Small caches that must be readable locally on every pod: reference data, a few thousand keys. |
| Local | The pod that handled the write | A scratch cache that is allowed to vanish, or a near-cache in front of a remote one. |
Distributed is the default you want. Replicated looks simpler until the dataset grows, and then every pod holds every key and every write is sent to every pod. Local looks fast until a client hits another pod and the key is missing. On a Service that load-balances, local is a bug unless the client pins to a pod.
distributedCache:
mode: "SYNC"
owners: "2"
statistics: "true"
encoding:
mediaType: "application/json"
SYNC means the write returns after the owners have the entry. ASYNC returns when the first owner has it and replicates after. Sessions and anything a following read must see use SYNC. A best-effort counter can use ASYNC and will sometimes lose the last update. Say which one you picked in the cache's name or its Git commit message.
Owners and state transfer
owners: 2 on a three-pod cluster survives one pod down. It does not survive two. owners: 1 survives zero pods down: the moment that pod leaves, those keys are gone, unless a store has them. owners equal to the replica count is a replicated cache with extra steps. Prefer the replicated mode if that is what you mean.
When a pod joins or leaves, Data Grid moves entries so the owner count is true again. That is state transfer. It costs network and CPU. Killing pods in a loop to "test resilience" during a transfer is how you test a cluster that is already busy. One delete, then wait for WellFormed, is the test.
The key's segment decides the owners. You do not choose the pod. You can choose that two keys land together by using the same affinity, but the ordinary case is to let the hash do it. If a single key is huge (a multi-megabyte value), it is still one entry on one owner pair. Data Grid does not split a value. Put large objects in object storage and cache the pointer.
Expiration
Expiration is about time. A lifespan is how long the entry may live from the write. max-idle is how long it may live since the last read. Idle expiration is easy to misunderstand: a read slides the window, so a hot key never expires, and idle checks are not a promise about the exact second.
distributedCache:
mode: "SYNC"
owners: "2"
expiration:
lifespan: "60000"
maxIdle: "-1"
encoding:
mediaType: "application/json"
60000 is milliseconds: one minute. maxIdle: "-1" turns idle expiry off. For a session, lifespan should match the session timeout you tell the user, and the application should write the entry again when the session is renewed. Letting the cache invent a shorter lifetime than the application is how users get logged out at random.
Expiration is not a substitute for a delete when the source row changes. A price cached for five minutes is wrong for five minutes after the price changes, unless the writer deletes sku-42.
Eviction
Eviction is about size. When the cache holds more than maxCount entries, or more than maxSize, it removes some, according to whenFull.
distributedCache:
mode: "SYNC"
owners: "2"
memory:
maxCount: "10000"
whenFull: "REMOVE"
encoding:
mediaType: "application/json"
REMOVE drops entries. The application has to tolerate a miss and read from PostgreSQL. That is correct for a cache of catalog rows. It is unacceptable for a cache that is the only copy of a shopping basket, unless a store sits behind it. For that cache, size the memory so eviction does not fire, and alert on the size metric instead.
Eviction counts entries on each pod, and an entry with two owners occupies two pods. A maxCount of 10,000 is not 10,000 for the whole cluster. Read it as a per-pod bound. oc describe on the cache and the statistics exposed in the monitoring lab are how you see whether eviction is happening. If it is happening on a cache you thought was complete, the bound is too low or the key design is unbounded (one key per request id, forever).
See the mode the cluster is actually running
The CR can say owners: 2 while a console edit last week says something else. Read the cache the server has, not only the file:
oc get cache catalog -n datagrid -o jsonpath='{.spec.template}'
oc exec -n datagrid example-infinispan-0 -- \
curl -sk -u "developer:${PASS}" \
https://example-infinispan:11222/rest/v2/caches/catalog?action=config
The config lists the mode and the owner count. If it does not match Git, the operator's update strategy may be waiting for a recreate, or someone created the cache out of band. Recreate drops entries. For catalog that is acceptable. For sessions it logs people out. Decide which cache you are looking at before you apply a "small" template change.
A mode you can check
Create a second cache, replicated-demo, with replicatedCache and a short lifespan. Put a key. You should be able to read it. Wait past the lifespan and the read should miss. Then put a key in catalog and delete one pod, as in the install lab, and read it from another pod.
If you need every pod to hold the data because the values are tiny and read on every request, switch that one cache to replicated and leave catalog distributed. One cluster, several modes, is the normal layout. One mode for every cache is how the reference data ends up on a single owner and the sessions end up copied three times.
What's next
Connecting applications uses catalog from Spring Boot, Quarkus and .NET. Persistence adds a store so eviction and a full outage are different events.