Spring Boot, caching with EhCache

Do I need another server for Ehcache?

You can use Ehcache in Standalone mode. In this topology, the cache data is held in the application node. So you won't need another server in this mode. Ehcache also provides two other modes:

  1. Distributed – The data is held in a remote server (or array of servers) with a subset of recently used data held in each application node. This topology offers a rich set of consistency options. A distributed topology is the recommended approach in a clustered or scaled-out application environment. It provides the highest level of performance, availability, and scalability.

    The distributed topology is available as a Terracotta open source offering with no client limitations but limitations on Terracotta cluster size. These are removed when using the commercial BigMemory Max.

  2. Replicated – The cached data set is held in each application node and data is copied or invalidated across the nodes without locking. Replication can be either asynchronous or synchronous, where the writing thread blocks while propagation occurs. The only consistency mode supported in this topology is Weak Consistency.

Do I need some another client to work with Ehcache?

You should use Ehcache library in order to be able to communicate with Ehache. But Spring provides a Caching Abstraction which is more elegant to work with and also has the advantage of being independent from the underlying caching implementation. So if you use Spring Caching Abstraction you could easily switch form Ehcache to, say, Hazelcast. You can read more about Spring Caching Abstraction in here.

Spring Boot provides spring-boot-starter-cache starter package which auto-configures a suitable CacheManager according to the implementation as long as the caching support is enabled.

How Ehcache works with multiple instances? Is it even possible to create something like shared cache using Ehcache?

Quoting from Ehcache documentation:

Ehcache provides in-process cache, which you can replicate across multiple nodes. It is also at the core of BigMemory Go and BigMemory Max, Terracotta’s commercial caching and in-memory data-storage products. The Terracotta Server Array provided with BigMemory Max enables mixed in-process/out-of-process configurations with terabyte-size caches. For information about Terracotta’s BigMemory offerings, see the BigMemory Go and BigMemory Max product documentation at http://terracotta.org/documentation.

As stated above, there is a free clustering option available with Ehcache. For this requirement, Redis and Hazelcast are also good options.


The Documentation and examples should answer all your questions:

https://spring.io/blog/2015/06/15/cache-auto-configuration-in-spring-boot-1-3 https://github.com/spring-projects/spring-boot/tree/1.3.x/spring-boot-samples/spring-boot-sample-cache

You can of course simply use embedded EhCache within your Spring Boot application. If you want to share the cache, it depends on your architecture. You could expose REST endpoints to make you cache available to other applications.

If you want a distributed, scaling, high performance cache, you maybe should habe a look at Hazelcast.