One of our clients’ database systems feeds content into third-party websites. Our client has recently signed on a very high traffic website, and a five-minute test yesterday revealed that we have some optimizing to do be able to handle that website’s traffic.
I made some code changes, added an index to a database table which should up in the slow query log during the test, and was surprised to realize MySQL query caching wasn’t turned on for that server, so it is now. And for another test this afternoon I’ve put the code on a faster server.
But the most exciting change I made was implementing and incorporating memcached, a “Free & open source, high-performance, distributed memory object caching system.” I had known about it for years — Fabrizio Parrella at The Queensboro Shirt Company set it up as one of the first things he did when he got there — but I hadn’t ever messed with it, and our servers’ loads were such that it wasn’t a real need. (Business database applications tend to have much fewer page views than high-profile public websites.)
But last night I had a real need, and found memcached to be an amazing tool.
Here’s how I explained it in an email to my programming teammates this morning:
The basic idea with memcached is that you can store data (text, arrays, etc.) in RAM for quick retrieval instead of having to pull it out of the db each time (which involves hard drive reads among other things), and this cache is session/user/page view independent.
In a nutshell here is the magic:
In the past I just had:
———————–
$offer_info = maj_get_offer_info($offer_id); // how we get info out of the db into our script———————–
Now I have:
———————–
$seconds_to_live_in_cache = 120; $key = "offer_info_{$offer_id}"; $offer_info = memcache_get($memcache_instance, $key); if (!is_array($offer_info)) { $offer_info = maj_get_offer_info($offer_id); memcache_add($memcache_instance, $key, $offer_info, false, $seconds_to_live_in_cache); }———————–
The key to appreciating this is that if John Doe loads up a page that uses the database to get $offer_info, with that then getting put into memcache, and then 90 seconds later Susan Smith accesses the same page with the same offer, Susan’s page is going to pull the offer_info from super-fast RAM instead of making a database call (or actually a set of several database calls).
And for higher traffic sites, the memcached server can be a different server than the web or db servers, or you can have multiple memcached servers.
My example puts an array into the cache, but you could also use it for example to put a block of html into it, such as the left nav html content on Port City Deals (featured deals, search by category/location, price ranges). All of that is info that is very rarely changed, so the whole block can be built and then cached for a long period of time, saving MANY database calls with each and every page view.
In posting this online I’m sure some are going to say “so where have you been for the past several years?”, but I’m excited about my newly discovered tool. My next step is to build it into our code generator from the ground level up.
box. (Compared to our “filters” search capability that allows you to drill down with precision, this global search capability allows you to cast a wide net. Both complement each other very well.)
