Caffeine Cache in Spring Boot: Algorithm Advantages, Configuration, and Usage
This article introduces Caffeine Cache as a high‑performance local cache for Java, explains its W‑TinyLFU algorithm advantages over Guava, details various eviction and loading strategies, and provides step‑by‑step Spring Boot integration with Maven dependencies, configuration properties, bean definitions, and annotation‑driven usage examples.
In this tutorial the author, a senior architect, presents Caffeine Cache, a modern Java caching library that builds on Guava Cache but improves eviction performance using the W‑TinyLFU algorithm, which combines LFU and LRU to achieve near‑optimal hit rates.
The article first compares common eviction policies (FIFO, LRU, LFU) and explains the limitations of each, then describes how W‑TinyLFU mitigates these issues by using a Count‑Min Sketch with a sliding‑window decay to track recent access frequencies.
Various cache filling strategies are covered:
Manual loading : provide a function to compute a value when a key is missing.
Synchronous loading : use a CacheLoader implementation that loads values during cache.get(key) .
Asynchronous loading : employ AsyncLoadingCache with a CompletableFuture for non‑blocking loads.
Three eviction policies are explained:
Size‑based eviction (maximumSize or maximumWeight).
Time‑based eviction (expireAfterAccess, expireAfterWrite, custom Expiry ).
Reference‑based eviction (weakKeys, weakValues, softValues) with a table of Java reference types.
Additional features such as removal listeners, cache writers for persisting entries, and statistics collection ( recordStats() , CacheStats ) are demonstrated with code snippets wrapped in ... tags.
For Spring Boot integration the article shows the Maven dependency: <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.6.2</version> </dependency> and the @EnableCaching annotation on the main application class.
Cache configuration can be done via application.properties or YAML, e.g.: spring.cache.caffeine.spec=initialCapacity=50,maximumSize=500,expireAfterWrite=10s or spring: cache: type: caffeine caffeine: spec: maximumSize=1024,refreshAfterWrite=60s A custom CacheLoader bean is required when using refreshAfterWrite .
The author also provides a Java @Configuration class that creates a SimpleCacheManager with multiple CaffeineCache instances, showing how to set TTL, maximum size, and record statistics.
Annotation‑driven cache operations are illustrated with @Cacheable , @CachePut , and @CacheEvict examples, including SpEL key expressions and synchronization options. A table of SpEL root objects (methodName, args, target, etc.) is included for reference.
Finally, the article mentions promotional messages and QR codes for a "Top‑Level Architect" community, but the technical content remains a comprehensive guide to using Caffeine Cache in Java backend applications.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.