Ruby-on-rails Redis 和 Memcache 还是 Redis?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4188620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redis and Memcache or just Redis?
提问by markquezada
I'm using memcached for some caching in my Rails 3 app through the simple Rails.cacheinterface and now I'd like to do some background job processing with redis and resque.
我正在通过简单的Rails.cache界面在我的 Rails 3 应用程序中使用 memcached 进行一些缓存,现在我想用 redis 和 resque 做一些后台作业处理。
I think they're different enough to warrant using both. On heroku though, there are separate fees to use both memcached and redis. Does it make sense to use both or should I migrate to just using redis?
我认为它们的不同足以保证同时使用两者。但是,在 heroku 上,使用 memcached 和 redis 需要支付单独的费用。使用两者是否有意义,还是应该迁移到仅使用 redis?
I like using memcached for caching because least recently used keys automatically get pushed out of the cache and I don't need the cache data to persist. Redis is mostly new to me, but I understand that it's persistent by default and that keys do not expire out of the cache automatically.
我喜欢使用 memcached 进行缓存,因为最近最少使用的键会自动从缓存中推出,而且我不需要缓存数据来持久化。Redis 对我来说大多是新手,但我知道它默认情况下是持久的,并且键不会自动从缓存中过期。
EDIT: Just wanted to be more clear with my question. I know it's feasible to use only Redis instead of both. I guess I just want to know if there are any specific disadvantages in doing so? Considering both implementation and infrastructure, are there any reasons why I shouldn't just use Redis? (I.e., is memcached faster for simple caching?) I haven't found anything definitive either way.
编辑:只是想更清楚我的问题。我知道只使用 Redis 而不是两者都是可行的。我想我只是想知道这样做是否有任何特定的缺点?考虑到实现和基础设施,有什么理由我不应该只使用 Redis?(即,对于简单的缓存,memcached 是否更快?)无论哪种方式,我都没有找到任何明确的东西。
采纳答案by Tom Clarkson
Assuming that migrating from memcached to redis for the caching you already do is easy enough, I'd go with redis only to keep things simple.
假设从 memcached 迁移到 redis 以进行您已经做的缓存很容易,我会使用 redis 只是为了让事情变得简单。
In redis persistence is optional, so you can use it much like memcached if that is what you want. You may even find that making your cache persistent is useful to avoid lots of cache misses after a restart. Expiry is available also - the algorithm is a bit different from memcached, but not enough to matter for most purposes - see http://redis.io/commands/expirefor details.
在 redis 中,持久性是可选的,因此如果您需要,您可以像使用 memcached 一样使用它。您甚至可能会发现,使缓存持久化对于避免重新启动后的大量缓存未命中很有用。到期也可用 - 该算法与 memcached 有点不同,但对于大多数用途来说还不够重要 -有关详细信息,请参阅http://redis.io/commands/expire。
回答by Luca Guidi
I'm the author of redis-store, there is no need to use directly Redis commands, just use the :expires_inoption like this:
我是redis-store的作者,不需要直接使用 Redis 命令,只需使用这样的:expires_in选项:
ActionController::Base.cache_store = :redis_store, :expires_in => 5.minutes
ActionController::Base.cache_store = :redis_store, :expires_in => 5.minutes
The advantage of using Redis is fastness, and with my gem, is that you already have stores for Rack::Cache, Rails.cacheor I18n.
使用 Redis 的优点是速度快,而且使用我的 gem,您已经拥有Rack::Cache,Rails.cache或 的商店I18n。
回答by Brian Armstrong
I've seen a few large rails sites that use both Memcached and Redis. Memcached is used for ephemeral things that are nice to keep hot in memory but can be lost/regenerated if needed, and Redis for persistent storage. Both are used to take a load off the main DB for reading/write heavy operations.
我见过一些使用 Memcached 和 Redis 的大型 Rails 站点。Memcached 用于临时事物,这些事物可以很好地在内存中保持热度,但可以在需要时丢失/重新生成,而 Redis 则用于持久存储。两者都用于减轻主数据库的负载以进行读/写繁重的操作。
More details:
更多细节:
Memcached:used for page/fragment/response caching and it's ok to hit the memory limit on Memcached because it will LRU (least recently used) to expire the old stuff, and frequently keep accessed keys hot in memory. It's important that anything in Memcached could be recreated from the DB if needed (it's not your only copy). But you can keep dumping things into it, and Memcached will figure which are used most frequently and keep those hot in memory. You don't have to worry about removing things from Memcached.
Memcached:用于页面/片段/响应缓存,在 Memcached 上达到内存限制是可以的,因为它会使 LRU(最近最少使用)使旧内容过期,并经常使访问的键在内存中保持热状态。如果需要,可以从数据库重新创建 Memcached 中的任何内容(这不是您唯一的副本),这一点很重要。但是您可以继续将内容转储到其中,Memcached 会找出最常使用的内容并将其保留在内存中。您不必担心从 Memcached 中删除内容。
redis:you use this for data that you would not want to lose, and is small enough to fit in memory. This usually includes resque/sidekiq jobs, counters for rate limiting, split test results, or anything that you wouldn't want to lose/recreate. You don't want to exceed the memory limit here, so you have to be a little more careful about what you store and clean up later.
redis:您将它用于您不想丢失的数据,并且足够小以适合内存。这通常包括 resque/sidekiq 作业、速率限制计数器、拆分测试结果或任何您不想丢失/重新创建的内容。您不想在这里超出内存限制,因此您必须更加小心存储和稍后清理的内容。
Redis starts to suffer performance problems once it exceeds its memory limit (correct me if I'm wrong). It's possible to solve this by configuring Redis to act like Memcached and LRU expire stuff, so it never reaches its memory limit. But you would not want to do this with everything you are keeping in Redis, like resque jobs. So instead of people often keep the default, Rails.cache set to use Memcached (using the dalligem). And then they keep a separate $redis = ... global variable to do redis operations.
一旦超过其内存限制,Redis 就会开始遇到性能问题(如果我错了,请纠正我)。可以通过将 Redis 配置为类似于 Memcached 和 LRU 过期内容来解决此问题,因此它永远不会达到其内存限制。但是您不会希望对保存在 Redis 中的所有内容(例如 resque 作业)执行此操作。所以人们通常保持默认,Rails.cache 设置为使用 Memcached(使用dalligem)。然后他们保留一个单独的 $redis = ... 全局变量来进行 redis 操作。
# in config/application.rb
config.cache_store = :dalli_store # memcached
# in config/initializers/redis.rb
$redis = $redis = Redis.connect(url: ENV['REDIS_URL'])
There might be an easy way to do this all in Redis - perhaps by having two separate Redis instances, one with an LRU hard memory limit, similar to Memcache, and another for persistent storage? I haven't seen this used, but I'm guessing it would be doable.
在 Redis 中可能有一种简单的方法来完成这一切——也许通过拥有两个单独的 Redis 实例,一个具有 LRU 硬内存限制,类似于 Memcache,另一个用于持久存储?我还没有看到使用过这个,但我猜它是可行的。
回答by efalcao
I would consider checking out my answer on this subject:
我会考虑查看我对这个主题的回答:
Rails and caching, is it easy to switch between memcache and redis?
Rails 和缓存,memcache 和redis 之间切换方便吗?
Essentially, through my experience, I would advocate for keeping them separate: memcached for caching and redis for data structures and more persistant storage
从本质上讲,根据我的经验,我主张将它们分开:memcached 用于缓存,redis 用于数据结构和更持久的存储
回答by Yarin
I asked the team at Redis Labs(who provide the Memcached Cloudand Redis Cloudadd ons) about which product they would recommend for Rails caching. They said that in general they would recommend Redis Cloud, that Memcached Cloud is mainly offered for legacy purposes, and pointed out that their Memcached Cloud service is in fact build on top of Redis Cloud.
我询问了Redis Labs的团队(他们提供Memcached Cloud和Redis Cloud附加组件)他们会推荐哪种产品用于 Rails 缓存。他们表示总体上会推荐 Redis Cloud,Memcached Cloud 主要用于遗留用途,并指出他们的 Memcached Cloud 服务实际上是建立在 Redis Cloud 之上的。
回答by slezica
I don't know what you're using them for, but actually using both may give you a performance advantage: Memcached has far better performance running across multiple cores than Redis, so caching the most important data with Memcached and keeping the rest in Redis, taking advantage of its capabilities as database, could increase performance.
我不知道你用它们做什么,但实际上同时使用它们可能会给你带来性能优势:Memcached 在多核上运行的性能比 Redis 好得多,所以用 Memcached 缓存最重要的数据并将其余数据保留在 Redis 中,利用其作为数据库的功能,可以提高性能。

