Comparison of Java Local Caching Solutions: Guava, Caffeine, and Ehcache
This article reviews three popular Java in‑process caching libraries—Guava, Caffeine, and Ehcache—explaining their configuration options, performance characteristics, monitoring capabilities, advantages and disadvantages, and provides a side‑by‑side comparison to help developers choose the most suitable local cache for their applications.
The author noticed that despite using Redis for distributed caching, there is still room for optimization with local caches, and therefore investigated three widely used Java in‑process caching solutions: Guava, Caffeine, and Ehcache.
Guava Cache Overview
Guava Cache is a JVM‑local cache provided by Google. It extends a ConcurrentHashMap‑like structure with features such as expiration policies, size limits, eviction strategies, and statistics.
Expiration Settings
Two expiration strategies are supported:
Expire after write (based on creation time): the entry expires a fixed time after being put into the cache.
Expire after access (based on last access): the expiration timer resets on each read.
public Cache
createCache() {
return CacheBuilder.newBuilder()
.expireAfterWrite(5L, TimeUnit.MINUTES)
.build();
} public Cache
createCache() {
return CacheBuilder.newBuilder()
.expireAfterAccess(5L, TimeUnit.MINUTES)
.build();
}Size Limits and Eviction Policies
Guava Cache can limit the maximum number of entries or the total weight of entries, and supports FIFO and LRU eviction when the limit is reached.
public Cache
createCache() {
return CacheBuilder.newBuilder()
.maximumSize(100L)
.build();
} public Cache
createCache() {
return CacheBuilder.newBuilder()
.maximumWeight(100L)
.weigher((key, value) -> (int) Math.ceil(instrumentation.getObjectSize(value) / 1024L))
.build();
}Cache Monitoring
By calling recordStats() when building the cache, Guava provides hit/miss statistics.
public Cache
createCache() {
return CacheBuilder.newBuilder()
.recordStats()
.build();
}Pros, Cons, and Suitable Scenarios
Pros: fast in‑memory reads, no network latency. Cons: limited by JVM heap size, possible cache drift in distributed deployments. Suitable for read‑heavy, write‑light workloads where strict consistency is not required.
Caffeine Overview
Caffeine is a successor to Guava Cache, offering the same API but significantly higher performance.
public Cache
createCache() {
return Caffeine.newBuilder()
.initialCapacity(1000)
.maximumSize(100L)
.expireAfterWrite(5L, TimeUnit.MINUTES)
.recordStats()
.build();
}
public Cache
createCache() {
return CacheBuilder.newBuilder()
.initialCapacity(1000)
.maximumSize(100L)
.expireAfterWrite(5L, TimeUnit.MINUTES)
.recordStats()
.build();
}Key Performance Optimizations over Guava
Asynchronous eviction: eviction cleanup runs in a separate thread pool, avoiding request‑blocking.
ConcurrentHashMap improvements: Java 8 enhancements (array‑list‑tree buckets, synchronized + CAS) reduce lock contention.
W‑TinyLFU eviction algorithm: combines a tiny window cache, a filter, and a main cache with LRU protection, achieving high hit rates with low memory overhead.
These optimizations make Caffeine the preferred choice when higher throughput is needed.
Pros, Cons, and Suitable Scenarios
Pros: superior performance compared to Guava. Cons: still suffers from cache drift; requires JDK 8 or newer. Suitable for read‑heavy, write‑light workloads where consistency is not critical and a pure in‑memory cache is acceptable.
Ehcache Overview
Ehcache extends beyond pure JVM memory by supporting on‑heap, off‑heap, and disk tiers, thus breaking the heap size limitation.
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(20, MemoryUnit.MB)
.offheap(10, MemoryUnit.MB)
.disk(5, MemoryUnit.GB);Ehcache also offers clustering to avoid cache drift, enabling distributed cache consistency.
Comparison of Local Caches
Framework
Hit Rate
Speed
Eviction Algorithm
Ease of Use
Cluster Support
Typical Scenario
Guava Cache
Medium
Third
LRU, LFU, FIFO
Easy
Not supported
Read‑heavy, write‑light, tolerates some drift
Caffeine
High
First
W‑TinyLFU
Easy
Not supported
Read‑heavy, write‑light, prefer Caffeine over Guava
Ehcache
Medium
Second
LRU, LFU, FIFO
Medium
Supported
Distributed systems requiring higher consistency
In summary, choose Guava for simple, low‑traffic scenarios, Caffeine for the best in‑process performance, and Ehcache when you need off‑heap or disk persistence and clustering capabilities.
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
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.