Purely out of necessity, I’ve become a system administrator/architect for digitalpoint.com servers… and a few people have been asking me for general admin tips to make things stable and scalable, so here’s a good one for you…
Use memcached… no really, use it.
memcached is a distributed memory caching system that allows multiple servers to access the same shared memory (you can use it just for single local server too of course). You can cache pretty much anything… SQL query results (especially ones that can’t hit indexes), dynamically generated web pages, dynamic RSS feeds, etc.
If you compile the memcache() functions/class directly into PHP, you have a an easy (yet powerful) way to incorporate it.
For example, let’s say I have some existing code that makes a call to some sort of API (I use memcached for the keyword suggestion tool in this way)… you can add a couple lines of code to cache the results.
[code=php]$data = @file_get_contents (“//some_api_call_url?keywords=$keywords”);[/code]
could become this:
[code=php]$memcache = new Memcache;
$memcache->addServer(‘192.168.1.1’);
$memcache->addServer(‘192.168.1.2’);
if (!$data = $memcache->get($keywords)) {
$data = @file_get_contents (“//some_api_call_url?keywords=$keywords”);
$memcache->set($keywords, $data, false, 86400); // 24 hour cache
}
$memcache->close();[/code]
Now we are only hitting the API for the same keywords once every 24 hours… that setup utilizes 2 memcached servers working as a single entity for pooling and failover.
I use memcache for all sorts of things (this blog’s pages are cached for 60 seconds as another example because WordPress sucks, and protects against a massive influx of traffic [front-page digg for example]).
Here’s some things I personally use it for…
- Caching this blog’s pages (for 60 seconds)
- Caching Keyword Suggestion Tool queries (for 7 days)
- Caching AdSense Sandbox queries (for 24 hours)
- Temporary counters in Keyword Tracker (using decrement() for displaying remaining keywords on a “Check All”)
- Recent forum topics that show on all the webmaster tools
- vBulletin forum datastore
Any high-traffic site that is dynamically generated (especially if it includes SQL queries) could benefit from it… you could manage which memcache items need to be updated when you insert/update to the database, or an easy way to do it is just check the cache for the pages that potentially have high traffic… if they aren’t in the cache, put them in the cache with a certain expiration date. Then the page is dynamically generated no more often than the cache expiration time (in the case of this blog, the pages are generated no more than once every 60 seconds).
P.S. – Make sure you block outside access to your memcached port since there’s no authentication used!