'Does Redis cache have advantage over Spring cache if used only for simple cache?
I am new to caching thing and learning some different solutions for my spring boot app. I was looking at Spring Cache and it is simple caching mechanism (that was what I look for) than I saw redis cache as well. And there are so many resources like "spring+redis cache". When I look at simple usage, I saw no difference. Even the annotations are same(Cacheable, CacheEvict, CachePut etc.), and I could not see a difference of usage except extra redis configuration and redis docker container etc... And none of these spring+redis cache articles tell what is the difference between just spring cache and spring+redis cache.
What is the advantage of redis cache over spring cache ? Or can you tell a simple use case that definitely I need to use redis cache and I cannot achieve it with spring cache ?
Solution 1:[1]
There is no such thing as a "Spring cache" from an implementation perspective. However, there is Spring's Cache Abstraction providing general caching facilities, and it originated in the core Spring Framework.
The Cache Abstraction is fundamentally an Adapter enabling different caching providers to be plugged into the framework and used generically for caching purposes. Caching can be applied in any tier of your Spring [Boot] application, for example, in the data access layer, or service layer, and so on. Caching can even be applied in multiple tiers simultaneously. Caching is, after all, a cross-cutting concern, and is, not coincidently, implemented with Spring AOP.
The abstraction provides a facade including an API along with Annotations for the most common caching operations (for example: Cache.put(key, value)
, Cache.get(key)
, Cache.evict(key)
, etc). These caching operations are fairly standard and common across most caching provider implementations (e.g. Redis, Hazelcast, Apache Geode, and even Java's Map
and ConcurrentMap
implementations; a cache really is a Map-like data structure).
Every caching provider must provide an implementation of the Cache
and CacheManager
interfaces defined by Spring's Cache Abstraction in order to be able to be plugged into the framework for caching purposes. In this way, every caching provider can be treated generically using Spring's caching API (or Annotations, and even the JCache annotations) so that you are 1) not tied to any specific caching provider, which then 2) allows you to interchange caching providers as your application use cases or requirements change.
Make sense so far?
Out-of-the-box, Spring (Boot) configures the ConcurrentMap
caching implementation (see here, then here (source) and finally, here followed by this) as the caching provider plugged into the framework when no other caching provider (e.g. Redis) is present on the application classpath.
However, there is a BIG difference between the ConcurrentMap
caching implementation and the Redis provider implementation. The later plugs in a Redis client which can remotely connect to a cluster of Redis servers. This means, in essence, you can have a "distributed" cache, where the load can be uniformly distributed, assuming you configure your caching provider, like Redis, correctly.
Not all caching providers are equal. Some offer many additional services, and some, like Apache Geode, can even function as the system of record (SoR) for your entire application, and maybe even eventually replace your RDBMS.
Even though most caching providers are capable of complex eviction (LRU, LFU) and expiration, Spring's Cache Abstraction does not specify the contract for these capabilities. That is left for you to decide and configure based on the cache provider you choose.
Fully distributed caching providers like Apache Geode and Hazelcast also provide memory management capabilities (after all, keys/values are stored in-memory). They can even be configured for persistence (hence the SoR UC), support different topologies: client/server, WAN (multi-site), embedded, and have support for different caching patterns (Cache-Aside, Inline Caching, Near Caching, etc) as well as different caching UC (e.g. (HTTP) Session state caching).
DISCLAIMER: I am the Spring Data engineer behind the Spring for Apache Geode offerings, so I will leave you with a chapter I devoted to "caching" in the Spring Boot for Apache Geode reference guide.
This post should hopefully give you enough to chew on for awhile.
If you have additional questions, please ask in the comments.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | John Blum |