Persistence
Cache stores on Data Grid, including a JDBC store that writes entries through to PostgreSQL so a cluster restart can reload them.
A distributed cache with two owners survives a pod. It does not survive all the pods, a namespace deletion, or an eviction that threw the entry out. A cache store writes the entry somewhere else. The store in this lab is a table in PostgreSQL, reached with a JDBC driver. A file store on a PVC is the smaller alternative when you only need the entry to outlive a restart and you do not need SQL.
This is not the backup CR. A store is on the write path, all the time. A backup is a point-in-time archive you take on purpose. Cross-site replication, in the next labs, is a second cluster, not a disk.
The database is a Crunchy PostgresCluster like the one in the Crunchy DC/DR lab. The Service name below, payments-primary.payments.svc, is that lab's primary. Use yours.
What the store changes
With a write-through JDBC store, a put returns after PostgreSQL has the row. A restart loads the entries back. A put also gets slower by a database round trip, and the database is now on the critical path of the cache. If that is not acceptable, a write-behind store acknowledges the client first and flushes after. Write-behind can lose the last writes if the node dies before the flush. For a cache of a shopping basket, write-through. For a cache you can rebuild from the source table, you often do not want a store at all: refill from the source.
Shared must be true when more than one pod writes to the same table. A non-shared store assumes this pod owns the file. On a distributed cache that is wrong, and two pods will fight.
Passivation writes to the store only when the entry is evicted from memory. It is a poor fit for a restart story, because an entry still in memory has never been written. Leave passivation off if the point is to reload after a cold start.
The driver
The Data Grid image does not contain the PostgreSQL JDBC driver. The operator can fetch it as a dependency, if the cluster can reach a Maven repository:
apiVersion: infinispan.org/v1
kind: Infinispan
metadata:
name: example-infinispan
namespace: datagrid
spec:
replicas: 3
service:
type: DataGrid
dependencies:
artifacts:
- maven: org.postgresql:postgresql:42.7.4
On a disconnected cluster, mirror that jar to a repository the cluster can reach, or follow the operator guide for a volume of custom libraries. A pod that starts and then throws ClassNotFoundException: org.postgresql.Driver on the first write is this step, not the cache template.
Create the database and the user. The cache will create its own tables if createOnStart is true. The user needs to create tables in that database, or you create them yourself and turn createOnStart off.
# On the Postgres primary pod, as a role that can create a database
psql -c "CREATE USER dg WITH PASSWORD 'change-me';"
psql -c "CREATE DATABASE datagrid OWNER dg;"
Change the password. Do not leave change-me in a cluster that anyone else can reach, and do not commit the real one. The cache template below has to contain a password the server will use. For the lab, that is a Secret you inject and a template you keep out of the public Git history. A production password belongs in a credential store the server is configured to unlock.
The cache
apiVersion: infinispan.org/v2alpha1
kind: Cache
metadata:
name: basket
namespace: datagrid
spec:
clusterName: example-infinispan
name: basket
template: |
distributedCache:
mode: "SYNC"
owners: "2"
statistics: "true"
encoding:
mediaType: "application/json"
persistence:
jdbcStore:
shared: "true"
dialect: POSTGRES
table:
createOnStart: "true"
dropOnExit: "false"
prefix: "basket"
connectionPool:
connectionUrl: "jdbc:postgresql://payments-primary.payments.svc:5432/datagrid"
driver: "org.postgresql.Driver"
username: "dg"
password: "change-me"
dropOnExit: "false" is the whole game. true drops the tables when the cache stops, which is the opposite of persistence. dialect: POSTGRES has to match the server. A wrong dialect creates a table the next statement cannot use.
Put a key through REST, as in the install lab, then look at PostgreSQL:
psql -d datagrid -c "\dt basket*"
psql -d datagrid -c "SELECT count(*) FROM <table the previous command listed>;"
The table name includes the prefix and a cache-specific suffix. One row after one put means write-through is working. If the count stays at zero, the put never reached this cache, or the store is not on the path. oc logs on the Data Grid pod at the moment of the put is the next read.
Reload
Scale the cluster to zero and back. The operator does this if you set spec.replicas: 0, wait for the pods to terminate, then set replicas: 3 again.
oc patch infinispan example-infinispan -n datagrid --type merge \
--patch '{"spec":{"replicas":0}}'
oc wait -n datagrid pod -l app=infinispan-pod --for=delete --timeout=300s
oc patch infinispan example-infinispan -n datagrid --type merge \
--patch '{"spec":{"replicas":3}}'
oc wait -n datagrid infinispan/example-infinispan --for=condition=WellFormed --timeout=600s
Read the key. It should be there, loaded from the table. If it is not, dropOnExit was true, the pods never shared the database you queried, or preload is disabled and the entry loads only on the next get — a get that returns the value still proves the store. An entry that expired before the restart will be gone. Expiration still applies. A store is not a time machine.
Do not point this store at the same PostgreSQL instance that is the system of record for the same rows, in the same tables. You will have two writers. The cache has its own database or its own schema. The application remains the writer of the real customer table. The cache store is a copy for the cache.
What's next
Querying indexes the fields inside the value. Cross-site replication copies the cache to the DR cluster, which a single PostgreSQL in the DC does not do unless that PostgreSQL has its own standby.