Skip to content


Repository files navigation

Dalli ElastiCache Gem Version Build Status Code Climate

Use AWS ElastiCache AutoDiscovery or Google Cloud MemoryStore Auto Discovery to automatically configure your Dalli memcached client with all the nodes in your cluster.


Install the gem:

# in your Gemfile
gem 'dalli-elasticache'

Using Dalli Elasticache in Rails

Note that the list of memcached servers used by Rails will be refreshed each time an app server process starts. If the list of nodes in your cluster changes, this configuration will not be reflected in the Rails configuraiton without such a server process restart.

Configuring a Cache Store

The most common use of Dalli in Rails is to support a cache store. To set up your cache store with a cluster, you'll need to generate the list of servers with Dalli ElastiCache and pass them to the cache_store configuration. This needs to be done in your config/environments/RAILS_ENV.rb file for each Rails environment where you want to use a cluster.

# in config/environments/production.rb
endpoint    = ""
elasticache =

config.cache_store = :mem_cache_store, elasticache.servers, { expires_in: }

Configuring a Session Store

Another use of Dalli in Rails is to support a Rails session store. Dalli ElastiCache can also be used in this case. The usage is very similar - first use Dalli ElastiCache to generate the list of servers, and then pass that result to the Rails configuration. In config/application.rb you would write:

# in config/environments/production.rb
endpoint    = ""
elasticache =

config.session_store = :mem_cache_store, memcache_server: elasticache.servers, pool_size: 10, pool_timeout: 5, expire_after:

Dalli Considerations

Please see here for more information on configuring Dalli and Rails.

Using Dalli ElastiCache with a Dalli Client

To initialize a Dalli Client for all the nodes of a cluster, one simply needs to pass the configuration endpoint and any options for the Dalli Client into the Dalli::ElastiCache initializer. Then one can use the methods on the Dalli::ElastiCache object to generate an appropriately configured Dalli::Clientor to get information about the cluster.

config_endpoint = ""

# Options for configuring the Dalli::Client
dalli_options = {
  expires_in: 24 * 60 * 60,
  namespace: "my_app"

elasticache =, dalli_options)

Fetch information about the Memcached nodes:

# Dalli::Client with configuration from the AutoDiscovery endpoint
# => #<Dalli::Client ... @servers=["", ...]>

# Node addresses
# => ["", ""]

# Number of times the cluster configuration has changed
# => 12

# Memcached version of the cluster
# => "1.4.14"

# Refresh data from the endpoint

# Refresh and get client with new configuration


Copyright (2017-2022) Aaron Suggs, Peter M. Goldstein. See LICENSE for details.


A wrapper for Dalli with support for AWS ElastiCache







No packages published