Recently as we spent a lot of time trying to sort out performance issues on our web site, we spent a lot of time going over our caching system and working to improve it. Our old caching system was originally written to use the file system, which actually worked really well when the site used to run under Linux. But it was beginning to show it’s age and was really expensive to maintain, so we started looking for alternative. I looked at a number of different options, and had read a lot about memcached, but alas I could not find any good memcached implementations for Windows which I needed for our development and testing. That is until I came across Couchbase while I was researching the option of using NoSQL for parts of our web site for the future. Not only is Couchbase a super fast NoSQL database, it is also entirely memcached compatible so makes is a great commercial memcached implementation. And it runs on Windows!
File Systems are SLOW!
One of the biggest problems we had with the file system cache was simply that it was too expensive to actually get rid of items that had expired from the cache. So our simple cache system would use the file timestamp to determine if a file was old and had expired and hence needed to be refreshed. But nothing would ever come back to clean up old ‘expired’ entries that had not been looked at for a while so the file system would end up with hundreds of thousands of tiny cache files on it over time. Thankfully that problem is neatly solved with Couchbase caching as it is entirely memory based. Nothing is stored on disk, and if you use more memory than you have allocated, old stuff starts to get tossed out automatically (and efficiently).
But another major problem we had was that the file system was really, REALLY slow when it comes to deleting files and dealing with directories full of LOTS of files. And this is an area where Windows is just plain bad compared to Linux and other UNIX file systems. Having a really slow file system leads to the problem of taking a long time to delete cache files from disk, if we ever needed to purge the cache. Purging the cache which was commonly needed after a major site upgrade, and it would literally take 30 minutes or more to delete the files. We ended up changing our cache purging code to rename the old cache directory so a new one would be created to take it’s place, and then deleting the old files in the background. We even went so far as to put the cache files into a Windows FAT32 ramdisk, but it still took forever to delete the files. And once again that problem is neatly solved by Couchbase because it is all memory based. A couple of mouse clicks is all that is needed to purge the cache, or a single call to the client library can also purge it instantly.
Couchbase to the Rescue
So we recently went live with our new site caching system using Couchbase, and the results are excellent. The overhead of getting a cache entry from Couchbase over the network from an entirely separate machine is actually less than the overhead we had in managing all the little files on disk. But more importantly we now have a lot of flexibility to automatically expire items and purge the entire cache quickly if we need to.