Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 584
user3528733 Avatar asked Apr 29 '16 09:04

user3528733


2 Answers

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.

like image 171
Benjamin M Avatar answered Oct 24 '22 08:10

Benjamin M


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.

like image 23
5 revs, 2 users 87% Avatar answered Oct 24 '22 09:10

5 revs, 2 users 87%