How Off‑Heap Caching Eliminates GC Scanning for Massive Product Data

The article explains why a large product cache moved from an in‑heap Caffeine cache to an off‑heap OHC solution, detailing serialization trade‑offs, per‑dimension cache configuration, singleflight loading, and the performance gains observed during high‑traffic promotions.

samdeepthink
samdeepthink
samdeepthink
How Off‑Heap Caching Eliminates GC Scanning for Massive Product Data

In a C‑end product service the cache size grew to gigabytes, and keeping all entries in an in‑heap Caffeine cache caused the garbage collector to scan many objects, increasing pause times.

To avoid this, the team switched to OHC (off‑heap cache), which stores data outside the JVM heap so GC does not scan the cached objects, yielding clear benefits for large‑scale caches.

Because OHC stores raw byte arrays, values must be serialized before insertion and deserialized on retrieval. The implementation uses Kryo for its speed and compactness, and a ThreadLocal provides a separate Kryo instance per thread since Kryo is not thread‑safe.

The product cache is not a single monolithic cache; it is divided by data dimensions—BASE, PRICE, SKU, BRAND, CATEGORY, IMAGE, ATTRIBUTE, DESCRIPTION—each with its own loader that handles loading, serialization, and miss handling. Each dimension can be configured with its own capacity and TTL, allowing hot base data to have larger capacity and longer TTL while low‑frequency, large description data gets tighter limits, improving overall hit rate.

OHC’s getWithLoader() method includes built‑in singleflight capability: for a given key on a machine, only one thread performs the load while others wait for the result, preventing cache stampede when hot entries expire.

In practice OHC is employed only during major promotional events as an extra local cache layer. The cache is pre‑warmed before the event, serves most requests during the event, and is bypassed afterward, reverting traffic to Redis, thus avoiding long‑term two‑level cache consistency maintenance.

Stress tests and production metrics met expectations: OHC achieved a hit rate of 92%–95%, most requests were satisfied locally, only a small fraction reached Redis, and database accesses dropped to less than 1% of total requests.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaPerformanceGC optimizationoff‑heap cachecache partitioningKryosingleflight
samdeepthink
Written by

samdeepthink

Knowledge Planet: Old Dock's Tech Chronicles Zhihu: SamDeepThinking A technical manager who still codes heavily on the front line. From junior developer to tech lead, then tech manager, now leading the whole front‑ and back‑end development team—leveling up along the way. I have some insights on programming, career development, and tech management.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.