Repository navigation
Conversation
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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two more
ValkeyCacheServicechanges 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 a0x01marker byte. The three possible leading bytes can't collide —0x00is the null sentinel,0xACopens every Java serialization stream,0x01marks 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 hardcodednew 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 asshaded.com.example.Fooin one andcom.example.Fooin the other, and the reader gets aClassNotFoundException. This adds an optionalObjectInputStreamFactoryso 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 thanresolveClass(), because JDK 17+ rejects aresolveClass()result whose name differs from the stream descriptor withInvalidClassException.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/valkeypass.One design question worth raising: this takes
ValkeyCacheServiceto four public constructors, andCOMPRESSION_THRESHOLD_BYTESis 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.