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, dalli-elasticache
.
Using the gem for configuration happens all inside of your config/environment files. Here is an example setup for production in production.rb
.
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.
The 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.
That's all
Hopefully this saved you some time. To learn more, take a look at the dalli-elasticache gem.