How can an app on EC2 autodiscover ElastiCache instances?
Say I have a webapp running on some number of load-balanced EC2 servers, storing and retrieving metadata from SimpleDB with larger chunks of data stored on S3 (due to the whole 1 KB limitation of SimpleDB). Since S3 is pretty high latency and I don't want to be making a ton of requests over there anyway, I'll want a caching layer for the info... enter ElastiCache.
Ok so I provision an ElastiCache server with endpoint X so I hardcode X into my app on EC2 and it's running happily until I get a few hundred thousand new users and all of a sudden my cache server is woefully underpowered for the demand. Fortunately I can just start up a few new larger cache servers... but then I realize I've got endpoints X, Y, and Z and my app only knows to try X, so I still have a problem.
So right now I'm just trying to get my head wrapped around the various pieces to this puzzle, and I haven't gotten to the coding part yet, but won't this be an issue? I've read the documentation for ElastiCache and it mentions that it is a cache cluster, but then each server in the cluster seems to have its own endpoint. Is there a way for an app running on EC2 to know about all the cache servers that are running, and more to the point which one contains the data for a particular key? Is it possible to ask the cluster as a whole to store or retrieve a piece of information?
Today Aws announced cache discovery. Your problem is solved. http://aws.typepad.com/aws/2012/11/amazon-elasticache-now-with-auto-discovery.html .
If your app is deployed from versioncontrol (I hope it is), you'd just edit the configuration file and re-deploy the application. I don't see a huge problem with this approach, but maybe I am missing the obvious.
Let me know.
Amazon's Elasticache Autodiscovery is absolutely horrible. It is basically impossible to install, which is crazy because it should be very simple.
I wrote a simple function in PHP to generate an elasticache node URL given the number of nodes you have running. Yes, you have to update your code if you change the number of nodes (or perhaps put this value in an 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
链接地址: http://www.djcxy.com/p/9162.html