Yarin
Yarin

Reputation: 183429

Rails 4 + Memcached on Heroku

We need a Rails caching solution that works with a multi-dyno formation on Heroku. Specifically, we need worker dynos processing long-running tasks to write to a cache that our web dynos can read from.

Apparently the only way to implement a shared cache across dynos is by using Memcached. However I'm having trouble finding objective instructions on how to get this done. (The Heroku docs are written by Memcached add-on vendors like MemCachier that are promoting their product.)

My questions:

Upvotes: 10

Views: 2855

Answers (2)

Yarin
Yarin

Reputation: 183429

(@ItamarHaber answered the question and sold me on memcached cloud. Just wanted to show exactly how we implemented it)

Using Memcached Cloud add-on with Rails 4 : (derived from instructions for Rails 3)

Add Dalli to your Gemfile:

gem 'dalli'

Set the cache_store in config/environments/production.rb:

  # NOTE: ENV vars aren't available during slug comiplation, so must check if they exist:
  if ENV["MEMCACHEDCLOUD_SERVERS"]
    config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }
  end

UPDATE:

After a little more research we realized that Redis could provide us with all the benefits of Memcached caching plus a slew of other features. Redis Labs, the makers of the Memcached Cloud add on, also offer the Redis Cloud add on, which is just as easy to use:

Using Redis Cloud add-on with Rails 4 :

Add Redis to your Gemfile:

gem 'redis-rails'

Set the cache_store in config/environments/production.rb:

  # NOTE: ENV vars aren't available during slug comiplation, so must check if they exist:
  if ENV["REDISCLOUD_URL"]
    config.cache_store = :redis_store, ENV["REDISCLOUD_URL"], { expires_in: 90.minutes }
  end

Upvotes: 5

Itamar Haber
Itamar Haber

Reputation: 49932

Due diligence - I work at Redis Labs, the company that provides the Memcached Cloud addon.

  • I'm not familiar with anyone running any datastore/database directly off a dyno - Heroku's and 3rd-party addons are available exactly for that.
  • Yes, using a remote Memcached is the common way to go with a web app that needs to scale to multiple dynos. Despite not being colocated on the same server, you'll still get the responses from you Memcached in <1 msec.
  • Choose the addon that gives you most value for your money - not only in terms of RAM per $ but also with regards to robustness and functionality - refer to this comparison for more information.

Upvotes: 6

Related Questions