I have a code in which i have implemented cache mechanism. Previously it was Guava based caching now i am shifting to Redis considering the needs of centralized cache.
But I am concerned about its performance as i have seen drastically low performance with redis when compared to guave.
I have measured performance for an api which gets a class object from cache
In case of Guava it was 5ms, whereas in Redis it gets 200ms.
This is average response in case of load test , in case of single request response does not differ much.
I have implemented Spring data Redis with cache abstraction.
Below is the sample Redis configuration:
@Bean
public RedisConnectionFactory redisConnectionFactory(@Value("${redis.host}") String redisHost,
@Value("${redis.port}") Integer redisPort) {
JedisConnectionFactory cf = new JedisConnectionFactory();
cf.setHostName(redisHost);
cf.setPort(redisPort);
cf.setUsePool(true);
JedisPoolConfig jedisPool = new JedisPoolConfig();
jedisPool.setMaxTotal(500);
cf.setPoolConfig(jedisPool);
return cf;
}
@Bean(name = "redisTemplate")
RedisTemplate<Object,Object> redisTemplate() {
final RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>();
template.setConnectionFactory(applicationContext.getBean(RedisConnectionFactory.class));
return template;
}
@Bean
public CacheManager cacheManager() {
if(isRedisEnabled)
{
RedisTemplate<?,?> template = (RedisTemplate<?, ?>) applicationContext.getBean("redisTemplate");
RedisCacheManager redisCacheManager = new PieRedisCacheManager(template);
redisCacheManager.setUsePrefix(true);
try
{
template.getConnectionFactory().getConnection();
}
catch(Exception e)
{
LOG.error("Unable to connect to redis Server ,closing application : "+e);
SpringApplication.exit(applicationContext);
}
return redisCacheManager;
}
else
{
GuavaCacheManager guavaCacheManager = new GuavaCacheManager();
guavaCacheManager.setCacheBuilder(CacheBuilder.newBuilder());
return guavaCacheManager;
}
}
Apart from that, For redis server configuration i have tried disabling all persistence as i don't need it. But still low performance.
My main query is that is it the configuration which is causing this or Redis is very low performing compared to Guava?
Can by more configuration tuning redis performance be compared to that of guava?
Please Suggest.
Conclusion: Memcached was one of the first popular open source memory caching solutions and works well for basic key value workloads. However, if you are working in the enterprise, or are just looking for a more modern, feature rich and actively developed product, Redis is the best solution.
When deciding whether to use Redis or Memcached a major difference between these two is data persistence. While Redis is an in-memory (mostly) data store and it is not volatile, Memcached is an in-memory cache and it is volatile.
Hazelcast is for distributed caching, meaning many services share the same cache, whereas Guava/Caffeine is a local cache per each service (not shared). It depends on your business requirements.
You can think Redis as a shared data structure, while Ehcache is a memory block storing serialized data objects. This is the main difference. Redis as a shared data structure means you can put some predefined data structure (such as String, List, Set etc) in one language and retrieve it in another language.
Disclaimer: I'm by no means an expert in using either Guava or Redis, though I have used both.
For starters, it would appear to me that it is perfectly normal that you encounter a performance decrease when switching from Guava to Redis.
Mostly because:
Guava provides caches that are in-memory and local to your application's running JVM. So your application can readily query without resorting to any inter-process communication.
Redis is a separate key-value store application, running in its own process. As a result, you have to communicate with it in some way to establish a connection and send requests.
So even if you were on the same machine, and even if Redis inherent performance was better than Guava's caches (which is probably the case, to be honest, for the general case), you would definitely see a performance hit anyways.
That being said, you could probably improve your performance by way of configuration and architectural choices:
Make sure you connect to Redis by using a local IP. This would help to avoid any address resolving when attempting to establish connections.
Make sure to connect to Redis over a protocol that's as lightweight as possible. As I assume you are using a local Redis server, and that you abide to the previous point, you won't need any bells-and-whistles, secure protocol, etc...
Any other usual Redis configuration tweaks that may apply to your scenario.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With