Amazon Elasticache Autodiscovery is absolutely terrible. It is almost impossible to establish what is crazy, because it should be very simple.
I wrote a simple function in PHP to create the URL of an elastic node page, given the number of nodes you started. Yes, you need to update your code if you change the number of nodes (or perhaps put this value in env var).
It maps the same keys to the same nodes:
function get_elasticache_node_url( $key, $config_url, $num_nodes ) { $node = hexdec( substr( md5($key), 0, 15) ) % $num_nodes + 1; $nodestr = str_pad($node, 4, "0", STR_PAD_LEFT); return str_replace('.cfg.','.'.$nodestr.'.',$config_url); } $num_nodes = 10; $config_url = 'cluster-name.xyzxyz.cfg.use1.cache.amazonaws.com'; echo get_elasticache_node_url("key1", $config_url, $num_nodes ); echo get_elasticache_node_url("key2", $config_url, $num_nodes );
Output:
cluster-name.xyzxyz.0001.use1.cache.amazonaws.com cluster-name.xyzxyz.0004.use1.cache.amazonaws.com
Nate
source share