A list of things to think of before deciding to use a distributed cache

Choosing the right cache software for your web application is not an easy job. The first and most important question that you have to ask yourself is if you actually need a cache in your application. But what exactly is a cache and how an application can profit or not from its use?

A cache is a software component that is used for storing data in memory (RAM) in order to provide faster accesses to this data. Using a cache you can save time and I/O operations, since you do not access the data stored in other slower types of storages such as relational databases or XML files in a hard disk drive.

Most of the cache implementations provide a simple key-value storage, where the key is unique in the cache. The value can be then extracted by using the unique key. The type of the value can be from a simple primitive value (boolean, string, number) to a complex object. In both type-cases you have to serialize the key-value pair and most of the times store it a simple string in the cache (continue reading for more information about serialization and desirialization).

When we talk about large web application, for example the ones that hosted in the cloud and serve thousands of requests pair day, then when we think about a cache software, this software has to be a distributed cache, since the application are also distributed. We can define two main types of caches:

The data stored in such a cache is most of the times data retrieved from a database. New user requests that access common data, do not have to query the database again. The main question you have to ask yourself before deciding for a distributed cache is whether you really have such business data that can be accessed from multiple clients or not. It makes no sense to store data in a cache that are not going to be used multiple times from multiple users and in that case you better have to stick with the traditional server-database architecture.

Well known examples of distributed caches are the AppFabric cache from Microsoft and the open source cache Redis.

In order to get a better understanding about the architecture of a web application that use a client-server structure together with a distributed cache, consider the following simple diagram:

A client-server architecture which uses a distributed cache

Since this article focuses on the distributed caches I am going to provide you with some important features that you have to consider if the cache software your selected, support.

        // cache is the Redis cache instance
        await cache.StringSetAsync("key1", "value1");
        var val = await cache.StringGetAsync("key1");

Choosing a cache software is an important architectural decision that you have to make if your application keeps getting bigger and bigger and you have to deal with thousands of user requests pair day.

Drop me a wire if you have any questions, I would be happy to assist you with questions concerning the AppFabric or the Redis caches.

comments powered by Disqus