EhCache
By Terracotta / Software AG
EhCache is a Java-based in-memory caching library that lets applications store frequently accessed data in local process memory to avoid repeated, expensive computation or database queries. It is widely used as a lightweight, embeddable…
Definition
EhCache is a Java-based in-memory caching library that lets applications store frequently accessed data in local process memory to avoid repeated, expensive computation or database queries. It is widely used as a lightweight, embeddable cache, most commonly as the second-level cache provider for Hibernate ORM, and it can optionally be extended into a distributed, clustered configuration through Terracotta for use across multiple application server instances in a cluster.
Overview
EhCache addresses the common performance problem of an application repeatedly recomputing a value or re-querying a database for data that rarely changes between requests. Rather than requiring a separate server process, EhCache is designed to be embedded directly as a library inside a Java application's own memory space, making it one of the simplest ways to add caching without introducing new infrastructure to operate. Mechanically, EhCache exposes a straightforward API where an application defines named caches with configuration such as maximum size, time-to-live, and eviction policy (for example, least-recently-used), and then stores and retrieves entries by key much like a map. When a cache reaches its configured capacity, EhCache evicts entries according to the chosen policy, and it can optionally spill data to disk when memory limits are reached, or persist a cache across application restarts. Its most common integration point is as Hibernate's second-level cache, where EhCache stores previously loaded entity and query results so that subsequent requests for the same data are served from memory rather than issuing a new SQL query. Compared to distributed data grids like Infinispan, Hazelcast, or Pivotal GemFire, EhCache's core design center is deliberately narrower: it began as a single-JVM, in-process cache rather than a clustered system, which makes it simpler to add to an application but limits it to caching data local to one server instance unless extended. Terracotta, the company behind EhCache, historically offered a clustering extension (BigMemory / Terracotta Server Array) that let multiple EhCache instances share a distributed cache tier, bridging the gap toward the distributed grids without changing the familiar EhCache API. In practice, Java applications add EhCache as a dependency and configure it through XML or annotations, most commonly to accelerate Hibernate-backed data access layers, to cache the results of expensive service calls, or to store computed values like rendered page fragments. Its simplicity and lack of required external infrastructure make it a common default choice for teams that need caching but do not yet have (or need) a distributed caching tier. The trade-off is that a plain EhCache instance's data is local to a single JVM, so in a multi-server deployment each server maintains its own independent cache, meaning cached data can be inconsistent across nodes unless a clustering extension or a different technology is used. Teams needing a single shared cache visible identically across every application instance, such as for session state in a load-balanced cluster, typically reach for Redis, Hazelcast, or a clustered EhCache configuration rather than default single-node EhCache.
Key Features
- Embeds directly as an in-process library within a Java application
- Supports configurable eviction policies like least-recently-used
- Provides optional disk overflow when memory limits are reached
- Commonly used as Hibernate's second-level cache provider
- Requires no separate server process for basic single-node use
- Configurable via XML or annotation-based cache definitions
- Can be extended to a distributed tier via Terracotta clustering
- Supports time-to-live and time-to-idle expiration settings