Caffeine vs Guava Cache vs Ehcache in Java Compared
Compare Caffeine, Guava Cache and Ehcache for in process caching in Java: eviction, refresh, statistics, Spring support and when you need a shared cache.

A cache is the easiest performance win in a service and the easiest way to introduce a bug that only shows up in production. The library choice is the smaller half of the decision. This post compares the three in process caches Java developers actually meet, Caffeine, Guava Cache and Ehcache, builds the same cache with each and then covers the parts that cause incidents: bounds, refresh, statistics and the moment a local cache stops being enough. For the Spring integration of the winner, Spring Boot from first project to production, which covers the cache abstraction in context, is the follow-up.
The cache we will build
A service looks up exchange rates. The rate for a currency pair changes every few minutes, the lookup is an HTTP call to a provider and the application asks for the same pair thousands of times per minute. The requirements are standard: cache a rate for five minutes, hold at most ten thousand pairs, load missing values on demand and let one thread load while others wait.
package dev.javahacks.rates;
import java.math.BigDecimal;
public interface RateClient {
BigDecimal fetch(String pair);
}Caffeine
Caffeine's LoadingCache takes a loader and handles the "one thread loads, the rest wait" behaviour for you.
package dev.javahacks.rates;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import java.math.BigDecimal;
import java.time.Duration;
public class CaffeineRates {
private final LoadingCache<String, BigDecimal> cache;
public CaffeineRates(RateClient client) {
this.cache = Caffeine.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(Duration.ofMinutes(5))
.refreshAfterWrite(Duration.ofMinutes(4))
.recordStats()
.build(client::fetch);
}
public BigDecimal rate(String pair) {
return cache.get(pair);
}
public String stats() {
return cache.stats().toString();
}
}refreshAfterWrite is the feature that most distinguishes a good cache setup. After four minutes, the next read returns the cached value immediately and triggers an asynchronous reload, so no request ever pays the load latency except the very first one. expireAfterWrite stays as the hard limit in case refreshing fails. Caffeine's eviction policy is Window TinyLFU, which keeps frequently used entries over merely recent ones and gives it a higher hit rate than a plain LRU on typical workloads; the policy is described in the Caffeine project wiki.
Guava Cache
Guava's CacheBuilder has nearly the same shape, which is no accident: Caffeine was written by Guava Cache's author as its successor.
package dev.javahacks.rates;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.math.BigDecimal;
import java.time.Duration;
public class GuavaRates {
private final LoadingCache<String, BigDecimal> cache;
public GuavaRates(RateClient client) {
this.cache = CacheBuilder.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(Duration.ofMinutes(5))
.refreshAfterWrite(Duration.ofMinutes(4))
.recordStats()
.build(CacheLoader.from(client::fetch));
}
public BigDecimal rate(String pair) {
return cache.getUnchecked(pair);
}
}If you are reading this because an old codebase uses Guava Cache, the migration is mostly renaming imports, which is why there is rarely a reason to start a new project on it. The differences you will notice are getUnchecked instead of get (Guava's get throws a checked ExecutionException), no asynchronous loading in the loading cache and a lower hit rate on skewed workloads. Guava does still ship and is maintained, so nothing forces a migration unless you need those features.
Ehcache
Ehcache 3 is a different animal: it is a configurable cache manager with tiers, and it implements JSR 107 (the javax.cache API, now javax.cache still, as JCache has not moved to Jakarta names).
package dev.javahacks.rates;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.*;
import org.ehcache.config.units.MemoryUnit;
import java.math.BigDecimal;
import java.time.Duration;
public class EhcacheRates {
private final CacheManager manager;
private final Cache<String, BigDecimal> cache;
private final RateClient client;
public EhcacheRates(RateClient client) {
this.client = client;
this.manager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("rates", CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class, BigDecimal.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10_000, EntryUnit.ENTRIES)
.offheap(64, MemoryUnit.MB))
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMinutes(5))))
.build(true);
this.cache = manager.getCache("rates", String.class, BigDecimal.class);
}
public BigDecimal rate(String pair) {
BigDecimal cached = cache.get(pair);
if (cached != null) {
return cached;
}
BigDecimal loaded = client.fetch(pair);
cache.put(pair, loaded);
return loaded;
}
}Two things stand out. The configuration declares resource pools, here ten thousand heap entries backed by 64 MB off heap, so a value that is evicted from the heap can still be served from off heap memory without a reload. And there is no loading cache in the basic API: the get, load and put sequence above is a race, because two threads can both miss and both call the provider. That is the "thundering herd" the other two libraries prevent, and with Ehcache you either accept it, add your own lock per key or use its CacheLoaderWriter. Off heap storage also requires serialization of keys and values, which is a cost and a constraint: the classes you cache must be serializable by Ehcache's serializer.
Verifying the herd is handled
The property worth testing is that concurrent misses trigger one load. Here is a test that works against the Caffeine version and shows what a wrong implementation would do:
class CaffeineRatesTest {
@Test
void concurrentMissesLoadOnce() throws Exception {
var calls = new AtomicInteger();
RateClient slow = pair -> {
calls.incrementAndGet();
try { Thread.sleep(200); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
return new BigDecimal("1.0842");
};
var rates = new CaffeineRates(slow);
try (var pool = Executors.newFixedThreadPool(8)) {
var futures = IntStream.range(0, 8)
.mapToObj(i -> pool.submit(() -> rates.rate("EURUSD")))
.toList();
for (var f : futures) f.get();
}
assertThat(calls.get()).isEqualTo(1);
}
}Eight threads ask for the same key during a 200 ms load, and the provider is called once. Run the same test against the hand written Ehcache version and calls would usually be higher than one, which is the whole case for the loading abstraction.
What differs, at a glance
| Caffeine | Guava Cache | Ehcache 3 | |
|---|---|---|---|
| Eviction | Window TinyLFU | Segmented LRU | LRU or LFU per tier |
| Async refresh | Yes | Limited | Via loader writer |
| Off heap and disk tiers | No | No | Yes |
| JSR 107 | Separate adapter, JCache module | No | Yes |
| Loader that coalesces misses | Yes | Yes | Not in the basic API |
| Spring Boot support | Auto configured | Removed from Spring in 5.0 | Via JCache |
| Maintainer note | Active | Maintenance | Active |
The three mistakes that cause incidents
No bound. A cache with no maximumSize and no expiry is a map with a nicer name. It grows until the heap does, and the symptom is a slow rise in old generation use followed by an OutOfMemoryError. Always set one limit; prefer both.
Caching mutable objects. The cache returns a reference to the object it holds. If a caller modifies it, every later reader sees the change. Cache immutable values, such as records and BigDecimal, or copy on the way out.
Forgetting statistics. recordStats() costs almost nothing and gives hit rate, load time and eviction counts. A hit rate of ten percent means the cache is only costing memory. Expose the numbers through Micrometer; Spring Boot registers Caffeine metrics automatically when Actuator is on the classpath.
Sizing the cache honestly
maximumSize(10_000) is a guess until you know what an entry costs. A BigDecimal plus a short string key is a few hundred bytes with object headers, so ten thousand entries is a few megabytes and nothing to worry about. A cache of rendered pages or decoded images is different, and counting entries is the wrong unit: use maximumWeight with a weigher that returns an estimate of each value's size in bytes, so one huge entry cannot hide among small ones. Then watch the eviction count in the statistics. A high eviction rate with a low hit rate says the cache is too small for the working set; a near zero eviction rate and a high hit rate says you can probably shrink it and give the memory back to the heap.
When an in process cache is not enough
A local cache is per JVM. With four replicas, each holds its own copy, each loads its own, and for a few minutes after a change they can disagree. For exchange rates that is fine. For a feature flag that must flip everywhere at once, a permissions cache after a revoke or a shopping basket, it is not. Then you want a shared store such as Redis or a data grid, which the post on Hazelcast and Redis for shared state in Java covers, usually with a small Caffeine cache in front to absorb reads.
The corollary is invalidation. Any cache of data that changes needs an answer to "who removes this entry when the source changes", and in a cluster the answer is either a short time to live, a pub/sub message or a version in the key. Pick one on purpose.
When this does not apply
If the lookup is cheap, a join the database already serves in a millisecond, a cache is added complexity for nothing. If the data is per request, use a local variable. For Hibernate entities, the second level cache is its own subject with its own rules, covered in using the second level cache with Hibernate. The primary sources are the Caffeine documentation and the Ehcache 3 documentation.


