Ehcache
Java's most popular production caching library — implements JCache (JSR-107) standard with extensions. Ehcache 3 supports tiered storage: on-heap (fastest, JVM heap), off-heap (direct memory, avoids GC), and disk persistence (survives JVM restart). Used as Hibernate second-level cache, Spring @Cacheable backend, or standalone cache. Key features: programmatic or XML configuration, cache event listeners, cache loaders/writers, statistics, and JMX monitoring. Ehcache is embedded (no external server required) — single JVM use case. For distributed caching, pair with Terracotta or switch to Redis/Hazelcast.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
No network exposure for embedded Ehcache. Disk tier stores agent data unencrypted — encrypt disk store path if caching sensitive agent data. JMX endpoint should be secured if exposing cache statistics externally. Off-heap memory is not zeroed on eviction — avoid caching credentials in Ehcache.
⚡ Reliability
Best When
You need Hibernate second-level cache, JSR-107 JCache compliance, off-heap memory usage, or disk-persistent agent caching in a single JVM — Ehcache is the reference JCache implementation with production-grade features.
Avoid When
You need distributed agent cache (use Hazelcast/Redis), you only need simple on-heap caching (use Caffeine), or your application isn't Java.
Use Cases
- • Cache agent Hibernate query results as second-level cache — @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) on agent entities uses Ehcache for L2 caching, reducing database load for frequently-read agent configurations
- • Spring @Cacheable with Ehcache backend — @Cacheable('agentProfiles') caches agent profile lookups in on-heap Ehcache with TTL for agent service methods
- • Off-heap caching for large agent result sets — Ehcache off-heap tier stores large agent response caches outside JVM heap, preventing GC pressure while keeping cache close to application
- • Disk-persistent agent session cache — Ehcache disk tier preserves agent session state across JVM restarts for agent services that need cache warm-up avoidance
- • Cache statistics for agent performance monitoring — Ehcache JMX statistics expose hit rate, miss rate, and eviction count for agent cache tuning via JConsole or Micrometer integration
Not For
- • Distributed multi-instance caching — Ehcache 3 open source is single-JVM; use Hazelcast, Redis, or Ehcache Terracotta for shared agent cache across multiple service instances
- • New agent projects — Caffeine Cache has better performance than Ehcache for on-heap caching with cleaner API; Ehcache advantage is JCache standard compliance, Hibernate L2 cache, and off-heap/disk tiers
- • Non-JVM applications — Ehcache is Java-only; use Redis or Memcached for polyglot agent service caching
Interface
Authentication
Embedded caching library — no auth concepts. No external connections. JMX management requires JVM auth if JMX is exposed externally.
Pricing
Ehcache 3 is Apache 2.0 licensed, maintained by Terracotta (acquired by Software AG). Open source for single-JVM use. Distributed clustering requires Terracotta commercial license.
Agent Metadata
Known Gotchas
- ⚠ CacheManager must be closed explicitly — Ehcache CacheManager holds off-heap and disk resources; not calling cacheManager.close() on shutdown leaks memory and disk files; use try-with-resources or @PreDestroy for agent service cleanup
- ⚠ Serialization required for off-heap and disk tiers — on-heap cache stores object references; off-heap and disk tiers require serialization; agent entity classes must implement Serializable or use Ehcache's custom serializer; adding non-serializable fields breaks tiered caching
- ⚠ Hibernate second-level cache requires query cache configuration — @Cache on entity enables entity cache; query results cache requires hibernate.cache.use_query_cache=true AND explicit query.setCacheable(true) per query; missing either means queries bypass L2 cache
- ⚠ Cache key must implement equals/hashCode — Ehcache uses key.equals() for lookup; agent entity IDs used as keys must have consistent equals implementation; using mutable objects as cache keys causes ghost entries that never match
- ⚠ Off-heap sizing affects JVM memory — off-heap memory is allocated outside JVM heap but within JVM process; setting large off-heap (e.g., 10GB) reduces OS memory available to other processes; size based on actual agent cache hit rate analysis
- ⚠ Spring Boot auto-configuration requires ehcache.xml — Spring Cache with Ehcache 3 requires spring.cache.jcache.config=classpath:ehcache.xml; without config file, auto-configuration creates default cache with no TTL or size limits causing unbounded agent cache growth
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for Ehcache.
Scores are editorial opinions as of 2026-03-06.