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

15,765

Solution 1

As long as you don't initialize the Memcached client yourself but you rely on Rails.cache common API, switching from Memcached to Redis is just a matter of installing redis-store and changing the configuration from

config.cache_store = :memcached_store

to

config.cache_store = :redis_store

More info about Rails.cache.

Solution 2

I hate to mess with your goals, but I would advise against using redis over memcached for generic rails caching.

I use redis and resque extensively in a large rails application and I thought it would be nice to consolidate caching, raw redis and resque into one. I ran into a few big issues:

  1. First off, it was slower. It could have totally been my specific usage, the redis-store library or redis itself. I'm not going to badmouth anything and your mileage may vary, but it would suck to dump a lot of time switching to redis when memcached "just works"
  2. Memcached is nice because it's extremely easy to add servers and use consistent hashing to accomplish your goals. Redis has this also, but in my experience it was difficult to simultaneously treat redis as both a monolithic datastore in some parts of my app and in other parts treat it as a distributed, consistently hashed blobs of caching storage.

Good luck with your project. I love redis AND memcached and use them in all my projects, but I let one do it's job as a kick-ass data structure server and let the other one kick ass at caching.

Solution 3

The neat parts of Redis include caching "list-based" things - pushing/popping things from this list as they happen in your app.

Rather than de-serializing a large value from memcached, editing it, then re-serializing it.

This would be done in ruby code in a custom filter, vs. the basic rails cache.

Share:
15,765

Related videos on Youtube

Blankman
Author by

Blankman

... .. . blank

Updated on September 23, 2020

Comments

  • Blankman
    Blankman almost 4 years

    Is there a common api such that if I switch between Redis or Memcached I don't have to change my code, just a config setting?

  • ColinM
    ColinM over 11 years
    memcached has more throughput than Redis as a simple key-value store because memcached is multi-threaded and Redis is single-threaded. So, Redis will soon consume 100% usage of one CPU core hitting it's peak performance whereas memcached will happily use as many CPU cores as you have and therefore scale much higher in it's maximum throughput. On a single-core system (who uses those?) Redis may be faster, but Redis is really only beneficial when you make use of it's additional data types or persistence or other features.
  • Yarin
    Yarin over 10 years
    This really doesn't address the question, and moreover I don't think your one-time experience warrants advising against using Redis for caching, as it seems a lot of people are using it successfully for exactly that.
  • Mohamad
    Mohamad almost 9 years
    How do can one use Redis for both fragment caching and Sidekiq jobs without eventually running out of memory? Is it possible to use the same Redis instance to always persist Sidekiq jobs, but use a first-in-first-out for fragments?
  • Aglystas
    Aglystas almost 6 years
    This comment saved me lots of time debugging an application. Memcache is much faster in my rails app even on a smaller 2 core system.