I’ve heard that HttpRuntime.Cache is not suitable for use with web farms. Why is that? What are some good example workarounds to this problem? I have heard about Memcached and similar offerings but I cannot find any good recent sample showing basic usage.
I’ve heard that HttpRuntime.Cache is not suitable for use with web farms. Why is
Share
One problem with HttpRuntime.Cache in a server farm is that much of the data in the cache will be duplicated across servers.
For example, lets say you have an e-commerce site and you store the product catalog in the cache (since it rarely changes) and that takes 20M of memory. If you have 5 web servers, each of them will have their own copy of the product catalog in their cache, using a total of 100M of memory (20M * 5 servers). Using a shared cache like memcached eliminates that waste by storing only one copy of the cached items instead of 5 copies (one for each server).
Another problem is cache synchronization. Again, let’s say you cache your product catalog for 1 hour after retrieving it from the database. Each server could have a copy of the catalog in its cache that expires at different times. When the cache expires on one server, it could get updated data that the other servers don’t have yet.
This could result in a strange user experience where they might see 50 products in a category, then refresh the page, are sent to a different server, and see 51, refresh again and see 50 again, etc. With a shared cache, you don’t have to worry about that.