user3528733 user3528733 - 6 months ago 93
Java Question

Spring Boot, caching with EhCache

I need to cache some data in my application and I am thinking about use Ehcache. And I have several questions:


  1. Do I need another server for Ehcache?

  2. Do I need some another client to work with Ehcache?

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


Answer

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.

Comments