Skip to content

Valkey cache: compress large values, allow a caller-supplied ObjectInputStream - #530

Open
frew wants to merge 2 commits into
objectify:masterfrom
frew:valkey-compression-and-ois-hook
Open

frew wants to merge 2 commits into
objectify:masterfrom
frew:valkey-compression-and-ois-hook

Conversation

@frew

@frew frew commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Two more ValkeyCacheService changes on top of #528, both running in production.

1. Compress large values (43ca27b2). Serialized values at or above 2 KB are Deflate-compressed before storage, behind a 0x01 marker byte. The three possible leading bytes can't collide — 0x00 is the null sentinel, 0xAC opens every Java serialization stream, 0x01 marks a compressed payload — so the read path identifies the encoding from the first byte and accepts either form. Entries written before this change, and anything under the threshold, still deserialize as plain serializations, which makes it safe to roll out and to roll back.

The 2 KB threshold is deliberately lower than the ~16 KB spymemcached's default transcoder used. Memcache evicts transparently under memory pressure; a Valkey instance is bounded by maxmemory, so compressing more of the keyspace is worth a little CPU on mid-size values.

2. Pluggable ObjectInputStream (e58d41de). The read path hardcoded new ObjectInputStream(...), which resolves classes by their exact stored name. That breaks when the writing and reading processes disagree about class names — most commonly when one was built with its dependencies relocated by a shading step, so the same class is stored as shaded.com.example.Foo in one and com.example.Foo in the other, and the reader gets a ClassNotFoundException. This adds an optional ObjectInputStreamFactory so a caller can supply a subclass that remaps names, letting processes with different relocation schemes share one cache.

The default is unchanged and the existing constructors delegate to it, so it's purely additive. No remapping implementation is included — what a name maps to is deployment-specific. The test supplies one to prove the hook works; note it swaps in readClassDescriptor() rather than resolveClass(), because JDK 17+ rejects a resolveClass() result whose name differs from the stream descriptor with InvalidClassException.

Testing. 8 new tests: compression round-trips above and below the threshold, a mid-size value between the old and new thresholds, reads of legacy uncompressed entries (including large ones), a zero-length value left by an external writer surfacing as a wrapped failure rather than an index error, and both new constructors. All 33 tests under src/test/java/com/googlecode/objectify/test/valkey pass.

One design question worth raising: this takes ValkeyCacheService to four public constructors, and COMPRESSION_THRESHOLD_BYTES is a public constant rather than something a caller can tune. I kept it matching what's deployed rather than redesigning, but if you'd want the threshold configurable too, constructor overloads stop scaling well and it probably wants a small builder instead. Happy to reshape it either way.

frew added 2 commits July 26, 2026 10:06
Serialized values at or above 2 KB are now Deflate-compressed before storage,
prefixed with a 0x01 marker byte so the read path can tell the encodings apart.
The three possible leading bytes can't collide: 0x00 is the null sentinel, 0xAC
opens every Java serialization stream, and 0x01 marks a compressed payload.

Reads accept either form, so this is safe to roll out and to roll back: entries
written before the change, and anything below the threshold, are still plain
serializations and still deserialize correctly.

The threshold is deliberately lower than the ~16 KB spymemcached's default
transcoder used. Memcache evicts transparently under pressure; a Valkey instance
is bounded by maxmemory, so compressing a larger share of the keyspace is worth
a little CPU on mid-size values.

Covers round-tripping above and below the threshold, that a mid-size value
between the two thresholds is compressed, reading legacy uncompressed entries
(including large ones), and that a zero-length value left by an external writer
surfaces as a wrapped failure rather than an index error.
The read path hardcoded `new ObjectInputStream(...)`, which resolves classes by
their exact stored name. That breaks when the process writing the cache and the
process reading it disagree about class names -- most commonly when one of them
was built with its dependencies relocated by a shading step, so the same class
is stored as `shaded.com.example.Foo` in one and `com.example.Foo` in the other.
The reader gets a ClassNotFoundException.

Accept an optional ObjectInputStreamFactory so callers can supply a subclass
that remaps names, letting processes with different relocation schemes share one
cache. The default is unchanged (plain ObjectInputStream), and the existing
constructors delegate to it, so this is additive.

No remapping implementation is included -- what a name should map to is
deployment-specific. The test supplies one to prove the hook works, and does the
swap in readClassDescriptor() rather than resolveClass(), since JDK 17+ rejects
a resolveClass() result whose name differs from the stream descriptor.
@frew

frew commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

@stickfigure Two more updates from running in production. Think they're relatively self-explanatory. Used Claude for the internal -> public conversion - happy to kill some of the more verbose comments or similar if helpful.

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