Redis and Memcache or just Redis?

I use memcached for some caching in my Rails 3 application via the simple Rails.cache interface, and now I would like to do some background job processing using redis and resque.

I think they are different enough to warrant the use of both. However, the hero has separate fees for using memcached and redis. Does it make sense to use both, or should I switch to using redis?

I like to use memcached for caching, because the least recently used keys are automatically popped out of the cache, and I don't need cache data to save. Redis is basically a newbie for me, but I understand that it is still the default and that the keys do not exit the cache automatically.

EDIT: Just wanted to be clearer with my question. I know that only Redis can be used instead of both. I guess I just want to know if there are any special flaws? Given both implementation and infrastructure, are there any reasons why I shouldn't just use Redis? (Ie, memcached is faster for simple caching?) I did not find anything final anyway.

+79
ruby-on-rails memcached heroku redis
Nov 15 '10 at 20:40
source share
6 answers

Assuming the transition from memcached to redis for caching that you already do is simple enough, I would go with redis just to make things simple.

Redis persistence is optional, so you can use it just like memcached if that is what you want. You may even find that persistent cache is useful to avoid many cache misses after a restart. Expiration is also available - the algorithm is slightly different from memcached, but this is not enough for most purposes. See http://redis.io/commands/expire for details.

+47
Nov 16 2018-10-11T00:
source share

I am the author of redis-store , there is no need to use Redis commands directly, just use the :expires_in parameter as follows:

ActionController::Base.cache_store = :redis_store, :expires_in => 5.minutes

The advantage of using Redis is speed, and with my gem, you already have stores for Rack::Cache , Rails.cache or I18n .

+42
04 Oct '11 at 19:10
source share

I have seen several sites with large rails that use both memcached and redis. Memcached is used to emphemeral things that are pleasant to store in memory, but can be lost / regenerated, if necessary, and reused for permanent storage. Both are used to unload the main db for heavy read / write operations.

More details:

memcached: used to cache pages / snippets / responses, and it’s normal to hit the memory limit on memcached because it will be LRU (recently used) to obsolete old stuff and is often used to access hotkeys. It is important that something in memcached can be recreated from the database if necessary (this is not your only copy). But you can continue to throw things into it, and memcached will appear, which are used most often and store them in memory. You do not need to worry about removing things from memcached.

redis: you use this for data that you do not want to lose, and are small enough to fit into memory. Typically, these are resque / sidekiq tasks, counters for speed limits, test separation results, or something you would not like to lose / recreate. You don't want to exceed the memory limit here, so you need to be a little more careful about what you store and clean later.

Redis begins to experience performance problems if it exceeds the memory limit (correct me if I am wrong). This can be resolved by configuring redis to act as memcached and LRU so that it does not go to the limit of memory. But you do not want to do this with everything that you keep in redis, for example, with the help of rezki. So instead, people often use the default Rails.cache to use memcached (using dalli pearls). And then they save a separate global variable $ redis = ... for performing redis operations.

 # in config/application.rb config.cache_store = :dalli_store # memcached # in config/initializers/redis.rb $redis = $redis = Redis.connect(url: ENV['REDIS_URL']) 

Maybe there is an easy way to do it all in redis - perhaps having two separate instances of redis, one with an LRU hard drive limit similar to memcache, and the other for permanent storage? I have not seen this used, but I assume that it is doable.

+16
Sep 21 '12 at 8:40
source share

I would like to check my answer on this topic:

Rails and caching, is it easy to switch between memcache and redis?

Essentially, in my experience, I would defend them to separate them: memcached for caching and redis for data structures and more persistent storage

+15
Dec 03 2018-10-12T00:
source share

I asked the team at Redis Labs (which provide the Memcached and Redis Cloud ) about which product they recommend for caching Rails. They said they generally recommend Redis Cloud, that Memcached Cloud is mainly offered for legacy purposes, and indicates that their Memcached Cloud service is actually built on top of Redis Cloud.

+6
Feb 15 '14 at 16:09
source share

I don’t know what you use them for, but in fact using both of them can give you a performance advantage: Memcached has much better multi-core performance than Redis, therefore caching the most important data with Memcached and saving rest in Redis, using its capabilities as a database, can improve performance.

+4
Nov 16 '10 at 1:57
source share



All Articles