Boost Spring Boot Performance with Cache2k: Setup, Benchmarks, and Advanced Config
Discover how to integrate the lightweight, high-performance Cache2k library into Spring Boot 2.7, explore benchmark results that surpass Ehcache3 and Caffeine, and learn basic and advanced configuration techniques—including custom builder settings and Micrometer metrics—for optimal in-memory caching.
When discussing Java in-memory cache libraries, the well-known Ehcache and the newer Caffeine are often mentioned, and Spring Boot 2.7 now adds official dependency management and auto-configuration for Cache2k, confirming its recognition by the Spring team.
Cache2k is an open-source, lightweight, high-performance Java memory cache library.
Benchmarks show that its throughput significantly exceeds that of Ehcache3 and Caffeine.
1. How to Use
① Add Dependency
<code><!-- springboot 2.7 already defines cache2k version globally, no need to specify version -->
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-spring</artifactId>
</dependency>
</code>② Configure Cache Type
③ Spring Cache Annotation Example
<code>@Cacheable(value = "demo", key = "#key")
public String get(String key) {
return "success";
}
</code>2. Advanced Configuration
Personalized Parameters
Implement the
Cache2kBuilderCustomizerinterface to customize cache settings.
<code>public class CustomCache2kBuilderCustomizer implements Cache2kBuilderCustomizer {
/**
* Customize the default cache settings.
*
* @param builder the builder to customize
*/
@Override
public void customize(Cache2kBuilder<?, ?> builder) {
// custom logic
}
}
</code>Micrometer Metrics Monitoring
<code><dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-micrometer</artifactId>
</dependency>
</code>Expose key cache statistics that can be integrated with Prometheus.
<code>Cache{database}(size=50003, capacity=50000, get=102876307, miss=1513517, put=0, load=4388352, reload=0, heapHit=101362790, refresh=2874835, refreshFailed=42166, refreshedHit=2102885, loadException=0, suppressedException=0, new=1513517, expire=587294, remove=8156, clear=0, removeByClear=0, evict=868064, timer=3462129, goneSpin=0, hitRate=98.52%, msecs/load=0.425, asyncLoadsStarted=2874835, asyncLoadsInFlight=0, loaderThreadsLimit=8, loaderThreadsMaxActive=8, created=2016-12-02 03:41:34.367, cleared=-, infoCreated=2016-12-02 14:34:34.503, infoCreationDeltaMs=21, collisions=8288, collisionSlots=7355, longestSlot=5, hashQuality=83, noCollisionPercent=83, impl=HeapCache, eviction0(impl=ClockProPlusEviction, chunkSize=11, coldSize=749, hotSize=24252, hotMaxSize=24250, ghostSize=12501, coldHits=11357227, hotHits=38721511, ghostHits=294065, coldRunCnt=444807, coldScanCnt=698524, hotRunCnt=370773, hotScanCnt=2820434), eviction1(impl=ClockProPlusEviction, chunkSize=11, coldSize=778, hotSize=24224, hotMaxSize=24250, ghostSize=12501, coldHits=11775594, hotHits=39508458, ghostHits=283324, coldRunCnt=423258, coldScanCnt=674762, hotRunCnt=357457, hotScanCnt=2689129), evictionRunning=0, keyMutation=0)
</code>Summary
The Cache2k JAR is only about 400 KB and has no additional dependencies, making it suitable for Android projects.
Despite performance comparable to Caffeine, it is less popular in China due to limited local documentation.
References
Cache2k: https://cache2k.org
Benchmarks: https://cache2k.org/benchmarks.html
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
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.