Choosing the Right Java Local Cache: Guava vs Caffeine vs Ehcache

An in‑depth comparison of popular Java in‑process caching solutions—Guava, Caffeine, and Ehcache—covers their expiration policies, capacity limits, eviction strategies, monitoring features, performance optimizations, and ideal use cases, helping developers select the most suitable cache for their applications.

JD Cloud Developers
JD Cloud Developers
JD Cloud Developers
Choosing the Right Java Local Cache: Guava vs Caffeine vs Ehcache

1. Introduction

The author noticed that, despite using Redis for distributed caching, there is still room for optimization with local in‑process caches. This article surveys three widely used Java local caches—Guava, Caffeine, and Ehcache—provides brief introductions, and compares them to help choose the most appropriate one.

2. Guava Cache Overview

Guava Cache is a comprehensive JVM‑level caching framework provided by Google. It builds on a ConcurrentHashMap‑like structure and adds features such as expiration settings, capacity limits, various eviction policies, and statistics monitoring.

2.1 Expiration Settings

Guava supports two expiration strategies: based on creation time and based on last access time.

(1) Based on creation time

public Cache<String, String> createCache() {
    return CacheBuilder.newBuilder()
        .expireAfterWrite(5L, TimeUnit.MINUTES)
        .build();
}

(2) Based on last access time

public Cache<String, String> createCache() {
    return CacheBuilder.newBuilder()
        .expireAfterAccess(5L, TimeUnit.MINUTES)
        .build();
}

2.2 Capacity Limits and Eviction Policies

Because Guava Cache resides in memory, it can cause out‑of‑memory risk. Therefore, a maximum size must be set, either by entry count or by weight.

(1) Limit by entry count

public Cache<String, User> createCache() {
    return CacheBuilder.newBuilder()
        .maximumSize(100L)
        .build();
}

(2) Limit by weight

public Cache<String, User> createCache() {
    return CacheBuilder.newBuilder()
        .maximumWeight(100L)
        .weigher((key, value) -> (int) Math.ceil(instrumentation.getObjectSize(value) / 1024L))
        .build();
}

When using weight, each object's size in kilobytes is treated as one weight unit, allowing total memory usage to be roughly limited (e.g., ~100 KB).

2.3 Cache Monitoring

Guava Cache can record statistics such as load and hit rates by enabling recordStats() during cache creation.

public Cache<String, User> createCache() {
    return CacheBuilder.newBuilder()
        .recordStats()
        .build();
}

2.4 Advantages, Disadvantages, and Use Cases

Advantages: fast read/write, reduced I/O because data stays in memory. Disadvantages: limited by JVM heap size, possible memory pressure, and cache inconsistency across distributed nodes (cache drift). Suitable for read‑heavy, write‑light scenarios where strict data consistency is not required.

3. Caffeine Overview

Caffeine is a Google‑originated cache that improves upon Guava Cache with significantly higher performance while keeping a similar API, making migration easy.

public Cache<String, String> createCache() {
    return Caffeine.newBuilder()
        .initialCapacity(1000)
        .maximumSize(100L)
        .expireAfterWrite(5L, TimeUnit.MINUTES)
        .recordStats()
        .build();
}

3.1 Performance Optimizations over Guava

(1) Asynchronous eviction handling – Caffeine performs cleanup in a separate thread pool, preventing query latency spikes.

(2) Improved ConcurrentHashMap implementation – Java 8 upgraded the internal structure to array + linked list + red‑black tree and refined locking (synchronized + CAS), reducing contention and enhancing read‑heavy performance.

(3) New eviction algorithm W‑TinyLFU – combines a tiny window cache, a filter, and a main cache. Items first enter the window (≈1 % of total). When evicted from the window, they are compared against the filter; frequently accessed items move to the main cache, which uses separate LRU segments for protection and eviction, achieving high hit rates with low memory overhead.

3.2 Advantages, Disadvantages, and Use Cases

Advantages: superior performance compared to Guava. Disadvantages: still suffers from cache drift in distributed deployments and requires JDK 8 or higher.

Use cases: read‑heavy, write‑light workloads where data consistency is not critical; pure in‑memory caching on JDK 8+ when higher throughput than Guava is desired.

4. Ehcache Overview

Unlike Guava and Caffeine, which are limited to heap memory, the latest Ehcache combines heap, off‑heap, and disk tiers, breaking the memory‑size barrier. Off‑heap storage avoids JVM GC pauses, while disk tier provides larger capacity at the cost of slower access.

Cache creation example specifying heap, off‑heap, and disk sizes:

ResourcePoolsBuilder.newResourcePoolsBuilder()
    .heap(20, MemoryUnit.MB)
    .offheap(10, MemoryUnit.MB)
    .disk(5, MemoryUnit.GB);

Ehcache also supports clustering to mitigate cache drift, enabling data sharing across distributed nodes (details to be covered in a future article).

5. Comparison of Local Caches

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.

JavaCacheCaffeineGuavaEhcache
JD Cloud Developers
Written by

JD Cloud Developers

JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.

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.