Using Multi-Node Elasticache with Rails
When using multi-node Elasticache memcached with Rails, it's critically important to get your setup correct.
If you don't, your application will connect to memcached nodes at random (!!!). Resulting in low cache hit rates.
How to setup
Conveniently, there is a gem for this,
Using the gem for configuration happens all inside of your config/environment files. Here is an example setup for production in
Notice here that the
url is set in our Rails credentials. We only establish the connection if it is present and we are not in a Rails console.
dalli-elasticache gem retrieves the address for each node and passes them to
Dalli. When your application is running, Rails/Dalli will correctly route memcached requests to the same nodes based on the key being set or retrieved.
Hopefully this saved you some time. To learn more, take a look at the dalli-elasticache gem.