Skip to content

Valkey cache: cluster support and a default TTL - #529

Closed
frew wants to merge 2 commits into
objectify:masterfrom
frew:valkey-cluster-and-default-ttl
Closed

frew wants to merge 2 commits into
objectify:masterfrom
frew:valkey-cluster-and-default-ttl

Conversation

@frew

@frew frew commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #527, from running that cache in production.

1. Cluster support. ValkeyCacheService only worked against a standalone server. MGET/MSET/multi-key DEL fail with CROSSSLOT on a cluster as soon as the keys hash to different slots, which is the normal case for Objectify keys. This holds the client as BaseClient so either a GlideClient or a GlideClusterClient can be supplied, and issues the multi-key operations as independent per-key commands fired concurrently (glide multiplexes them over the connection, so this costs one round trip's worth of latency, not N). The SET ... IFEQ CAS is unchanged except that on a cluster it is routed to the primary owning the key's slot.

2. Default TTL. Every write path went in without an expiration, including the cold-cache null sentinel that getIdentifiables bootstraps on every miss. Under memcache that is harmless — LRU evicts cold entries. Valkey with noeviction does not: the keyspace grows until maxmemory, and then all writes to that server start failing with OOM, including writes from anything else sharing the cluster. Writes now carry a TTL (default 24h, overridable via a new constructor); an entity's own expirationSeconds still wins where it declares one.

Note that this changes the constructor signature (GlideClient → BaseClient, and the lombok-generated constructor becomes explicit so the TTL can be defaulted). ValkeyCacheService hasn't shipped in a release yet, and GlideClient is a BaseClient, so existing source keeps compiling.

Testing. ValkeyCacheServiceTests covers the rewritten multi-key paths and adds coverage for the default TTL on put, putAll, the cold-cache sentinel, and CAS without an explicit expiration. All 25 tests under src/test/java/com/googlecode/objectify/test/valkey pass locally.

The cluster routing itself is not covered by a test: testcontainers can't easily expose a real cluster's advertised node addresses to the host in a way that works on both Linux and Docker Desktop. It has been exercised against a live multi-node Valkey cluster in production. Happy to attempt a Linux-only cluster test if you'd want one.

frew added 2 commits July 26, 2026 09:44
Hold the client as BaseClient, so either a standalone GlideClient or a cluster
GlideClusterClient can be passed in. Replace MGET/MSET/multi-key DEL with
per-key GET/SET/DEL issued concurrently, since multi-key commands fail with
CROSSSLOT on a cluster whenever the keys hash to different slots -- which is the
normal case for Objectify keys.

CAS still uses the native SET ... IFEQ from Valkey 8.1, but on a cluster the
command is routed explicitly to the primary owning the key's slot
(SlotKeyRoute). customCommand isn't declared on BaseClient and returns a
ClusterValue on the cluster client, so both client types are handled in one
place (casAsync).
Every write went to Valkey with no expiration: put/putAll, the cold-cache null
sentinel bootstrapped in getIdentifiables, and putIfUntouched when the entity
declared no expirationSeconds (the common case -- a bare @Cache is 0).

Memcache sheds cold entries via LRU, so a no-TTL write pattern was fine there. A
Valkey server or cluster configured with noeviction cannot: the keyspace grows
until maxmemory, after which every write against that server fails with OOM --
including writes from unrelated callers sharing it.

Apply a default TTL (24h) to all write paths; entities that declare their own
expiration still keep it. The single-arg constructor supplies the default, so
existing callers are unchanged, and a second constructor takes an explicit
value.
@frew

frew commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

Closing — this duplicates #528, which already landed these changes (cluster support via BaseClient, per-key multi-key ops, SlotKeyRoute for CAS, and the bounded default TTL). I branched off a stale master and didn't notice #528 had merged. Sorry for the noise.

@frew frew closed this Jul 26, 2026
@frew

frew commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

@stickfigure Whoops, forgot I had already submitted these changes. Please ignore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant