简体   繁体   English

Redis与番石榴缓存

[英]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. 以前它是基于番石榴的缓存,现在我正在转向Redis考虑集中缓存的需求。

But I am concerned about its performance as i have seen drastically low performance with redis when compared to guave. 但是我担心它的性能,因为与guave相比,我看到redis的性能极低。

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. 我测量了api的性能,它从缓存中获取一个类对象。在Guava的情况下,它是5ms,而在Redis中,它获得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. 我已经使用缓存抽象实现了Spring数据Redis。

Below is the sample Redis configuration: 以下是Redis配置示例:

  @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. 除此之外,对于redis服务器配置,我已经尝试禁用所有持久性,因为我不需要它。 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? 我的主要问题是,与Guava相比,导致此问题的配置还是Redis的性能非常低?
Can by more configuration tuning redis performance be compared to that of guava? 可以通过更多的配置调整redis性能与番石榴的性能进行比较吗?
Please Suggest. 请建议。

Disclaimer: I'm by no means an expert in using either Guava or Redis, though I have used both. 免责声明:我不是使用Guava或Redis的专家,尽管我已经同时使用了它们。

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. 对于初学者来说,在我看来,从Guava切换到Redis时遇到性能下降是完全正常的。

Mostly because: 主要是因为:

  • Guava provides caches that are in-memory and local to your application's running JVM. Guava为应用程序运行的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. Redis是一个独立的键值存储应用程序,在其自己的进程中运行。 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. 所以,即使你在同一台机器上,即使Redis固有的性能优于Guava的缓存(事实上可能就是这样,但对于一般情况而言),你肯定会看到性能上的打击。

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. 确保使用本地IP连接到Redis。 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. 确保通过尽可能轻量级的协议连接到Redis。 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... 正如我假设您使用的是本地Redis服务器,并且您遵守上一点,您将不需要任何铃声和口哨声,安全协议等...

  • Any other usual Redis configuration tweaks that may apply to your scenario. 任何其他可能适用于您的方案的常见Redis配置调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM