I need to cache some data in my application and I am thinking about use Ehcache. And I have several questions:
- Do I need another server for Ehcache?
- Do I need some another client to work with Ehcache?
- How Ehcache works with multiple instances? Is it even possible to create something like shared cache using Ehcache?
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/master/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.
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:
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.
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 suitableCacheManager
according to the implementation as long as the caching support is enabled.Quoting from Ehcache documentation:
As stated above, there is a free clustering option available with Ehcache. For this requirement, Redis and Hazelcast are also good options.