Caffeine
High-performance near-optimal in-memory cache for Java — Spring Boot's default cache implementation replacing Guava Cache. Caffeine implements the Window TinyLFU eviction algorithm providing near-optimal hit rates (better than LRU/LFU). Key features: time-based expiry (expireAfterWrite, expireAfterAccess), size-based eviction, reference-based eviction (weak/soft keys/values), async loading (AsyncLoadingCache), stats collection, and Guava Cache API compatibility. Part of the Caffeine family that includes Simulator (for policy evaluation) and JCF (JSR-107/JCache implementation).
Score Breakdown
⚙ Agent Friendliness
🔒 Security
In-memory process-local — no network exposure. Sensitive data cached in heap memory; JVM memory dump could expose cached agent data. Don't cache raw credentials or PII without encryption. Cache eviction prevents unbounded data accumulation.
⚡ Reliability
Best When
You need a fast, self-managing in-memory cache within a single JVM process — Caffeine's Window TinyLFU algorithm gives best-in-class hit rates for repeated agent computations or API responses.
Avoid When
You need distributed caching across multiple service instances (use Redis), persistence across restarts, or data sets larger than JVM heap.
Use Cases
- • Cache agent LLM API responses to reduce latency and cost — cache repeated agent queries by prompt hash with TTL matching freshness requirements
- • Store agent session context in-process cache — CaffeineCache with expireAfterAccess for recently active agent sessions; auto-eviction prevents unbounded memory growth
- • Cache agent configuration and feature flags loaded from database or config service — LoadingCache<String, Config> auto-loads on miss with configurable refresh interval
- • Memoize expensive agent computation results using Caffeine's AsyncLoadingCache — async cache population doesn't block caller while computing agent embeddings or similarity scores
- • Use Spring Boot's @Cacheable annotations backed by Caffeine — spring.cache.type=caffeine + caffeine spec configuration for application-wide agent result caching
Not For
- • Distributed/shared cache between multiple JVM instances — Caffeine is JVM-local; use Redis (Jedis/Lettuce) or Hazelcast for distributed cache across agent service instances
- • Persistent cache surviving JVM restarts — Caffeine is in-memory only; use EhCache with disk store or Redis for persistent agent data caching
- • Very large data sets — JVM heap limits apply; for caches exceeding available heap, use off-heap solutions (EhCache 3 off-heap, MapDB) or Redis
Interface
Authentication
In-memory cache library — no auth concepts. Cache data is process-local.
Pricing
Caffeine is Apache 2.0 licensed, maintained by Ben Manes. Free for all use.
Agent Metadata
Known Gotchas
- ⚠ Cache miss returns null not Optional — Cache.getIfPresent(key) returns null on miss; always null-check or use LoadingCache.get(key) with automatic loading; getIfPresent in reactive code causes NullPointerException if not handled
- ⚠ expireAfterWrite vs expireAfterAccess — expireAfterWrite expires entries N time after write regardless of access; expireAfterAccess resets expiry on each access; choose based on whether agent data freshness is write-based or activity-based
- ⚠ Weak reference eviction is GC-dependent — weakKeys/weakValues uses WeakReference and entry may be evicted whenever GC runs, not just when cache is full; don't rely on weakKeys for cache entries that should persist across GC cycles
- ⚠ maximumSize vs maximumWeight — maximumSize limits entry count; maximumWeight uses a Weigher function for byte-based or complexity-based limits; mixing both in same cache spec throws IllegalStateException
- ⚠ Spring Boot @Cacheable key generation — Spring's default key is all method params; complex agent objects as cache keys need proper hashCode/equals; use @Cacheable(key = "#agentId") for explicit key selection
- ⚠ Stats collection has overhead — Caffeine.newBuilder().recordStats() enables hit/miss counting; small (~5%) overhead; disable in production if performance is critical and metrics aren't needed
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for Caffeine.
Scores are editorial opinions as of 2026-03-06.