Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis versus Guava Cache

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.

like image 539
GP007 Avatar asked Aug 18 '15 14:08

GP007


People also ask

Is Redis the best cache?

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.

What is the difference between Redis and cache?

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.

Is Guava cache distributed cache?

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.

What is difference between ehcache and Redis cache?

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.


1 Answers

Disclaimer: I'm by no means an expert in using either Guava or Redis, though I have used both.

Obvious Performance Losses are Obvious

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.

Possible Improvements

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.

like image 92
haylem Avatar answered Sep 22 '22 12:09

haylem