Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Using memcached with ASP.NET

Using memcached with ASP.NET

Instead of ASP.Net's built-in caching, some .NET developers are turning to memcached, is a distributed memory caching system originally by Danga Interactive for LiveJournal.

A fundamental problem with caching is stale data. When running a single web server, one can easily clear a cache when data is known to have been changed. Unfortunately, ASP.NET doesn't have a good way to scale this up multiple servers. Each server's cache is blissfully unaware of changes to other caches.

ASP.NET does allow triggers based on changes to the file system or a database table to invalidate a cache. However these have problems, such as the expensive polling that database triggers cause and the tedious wiring of the triggers themselves. There are other options however.

Unlike ASP.NET's built-in caching, memcached is a distributed cache. Any web server in can update or delete a cache entry and all the other servers automatically see the change next time they access the cache. This is done by storing the entries on one or more cache servers. Each entry assigned to a server based on a hash of its key.

Superficially, the ASP.NET API for memcached looks almost identical to the built-in API. This allows switching to memcached to be as easy as a single pass with search and replace.

Moving beyond just getting it running however, there are some questions as its proper use in larger web farms. Richard Jones writes

As we add more nodes, the usefulness of get_multi decreases - it's possible for a single page to hit almost all of the memcached instances. I read somewhere that facebook partition their memcached cluster to improve get_multi performance (eg, all user data on a subset of mc nodes). Can anyone comment on the effectiveness of this?

One proposed solution is to generate hash keys separately from the cache entire's key. This would allow a developer to ensure that all entries needed by a given page are more likely to be on the same server. Unfortunately, generating hash keys based on where one wants the data stored rather than from the cache key itself is likely to be error prone and will take careful implementation.

You can download memcached under a BSD license. Client APIs for C#, as well as Perl, Python, PHP, Java, and a host of other languages have to installed separately. Finally, there is a Win32 port for those not wanting to run Linux machines.


Rate this Article