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:

  • Is a 3rd party add-on necessary, or can Memcached be implemented directly on a dyno within the formation?
  • Is using Memcached via an outside service even practical? If the whole point of Memcached is high-performance in-memory access, doesn't the network latency of an external service negate that?
  • If using add-ons, is there a reason to choose MemCachier vs Memcached Cloud?

Answers


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.

(@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

Need Your Help

How to design a circular reference to a single database table with an added relationship?

sql database database-design data-modeling

I'm not sure how best to phrase the question, but essentially I have a table of contacts, and instead of doing the typical -- a contact has a reference to a table with spouse information, and a table

put string and character in ofstream

c++ file makefile

I want to put string and character together in ofstream() but I get error I want to create files in different names but in same path for example like this E://string.txt but string is variable