2008
Distributing resources with Horde_Support_ConsistentHash
Let's say you've just read Yahoo!'s Best Practices for Speeding Up Your Web Site for the first time, and you want to tackle some easy ones first: serve your static assets (javascripts, css stylesheets, and images) from multiple servers. Alternately, if you've read the Yahoo! rules a bit more closely, you might not be worrying too much about assets due to the empty browser caches most of your users have, so what you really want to do is distribute cached user data across a set of cache servers.
We're going to skip the details of why the simplistic ways to do this are a bad idea, and go right to consistent hashing.
If you're not familiar with it already, you should read up to understand what it solves and how it works. Once you have that covered, here's how you can do it using Horde_Support_ConsistentHash, part of the recently released horde/support package.
For our example, you have three cache servers; the third one is much more powerful so you'd like it to handle the bulk of users. You're going to distribute cached user data across them based on username. Easy does it:
$h = new Horde_Support_ConsistentHash;
$h->add('cache1.example.com', 1);
$h->add('cache2.example.com', 1);
$h->add('cache3.example.com', 4);
$cacheServer = $h->get($username);
This has all kinds of nice properties: you can bring up new servers gradually by increasing their weight (just read the list of nodes from a database or configuration file), and doing so - or taking a bad server out of the pool - won't cause most users on an unaffected server to be switched, reducing cache thrashing and probably saving your butt if you really have as much traffic as you think you do.
Horde_Support_ConsistentHash has no external dependencies (even on other Horde_Support files) and can be installed with an easy:
pear channel-discover pear.horde.org pear install horde/support-beta
Finally, if your cache backend is memcache, the PECL memcache extension can take care of this for you by setting memcache.cache_strategy = "consistent". Obviously the Horde implementation can help you out in far more situations than just memcache, or if you're stuck with an older memcache package.