Backend Development 6 min read

Guava Cache Asynchronous Refresh Techniques for Improving System Performance

This article explains how to configure Guava Cache with expiration and refresh policies, analyzes its thread model under high concurrency, and presents two implementation approaches for asynchronous refresh combined with multi‑level caching to achieve low latency and reduced GC overhead.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Guava Cache Asynchronous Refresh Techniques for Improving System Performance

Guava Cache is a powerful local caching framework for Java, and this article introduces how to use its asynchronous refresh techniques to boost system performance.

The classic configuration mirrors JDK 1.7's ConcurrentHashMap , offering three eviction strategies (time, size, reference) and features such as automatic loading and statistics. Two key policies are expireAfterWrite (expiration) and refreshAfterWrite (refresh).

In high‑concurrency scenarios, expireAfterWrite allows only one thread to execute the load method while others block, whereas refreshAfterWrite lets one thread load new data while other threads receive the stale value, avoiding thread blockage but still potentially stressing the database if many keys expire simultaneously. Tests show Guava Cache does not run load/reload in a background thread.

To improve performance, the article recommends configuring the refresh interval shorter than the expiration interval and adopting an asynchronous refresh strategy where a dedicated thread pool loads new data while all requests continue to receive the old cache value, preventing cache avalanche.

Two ways to achieve asynchronous refresh are presented: (1) overriding the reload method, and (2) implementing an asyncReloading method. Both approaches require a separate thread pool for refresh tasks.

A real‑world case study describes a two‑level cache architecture (local Guava cache + Redis) used in an e‑commerce app homepage. After applying async refresh, latency dropped to about 5 ms and GC frequency decreased, though issues arose with data inconsistency across servers and suboptimal thread‑pool sizing.

The final solutions include combining async refresh with a message‑driven update mechanism to keep caches consistent and tuning the LoadingCache thread‑pool parameters with monitoring and alerts.

In summary, Guava Cache executes load/reload on the requesting thread; performance can be enhanced by setting refreshAfterWrite shorter than expireAfterWrite and employing asynchronous refresh, while still paying attention to cache‑database consistency.

backendJavaPerformanceCacheGuavaAsync Refresh
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login 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.