How To Implement Redis With Episerver Easily

In any high load website, how you deal with your cache is kinda important.  In most website scenarios, you might consider using the out-of-box output cache provider with memory cache and for the majority of projects this is more than adequate; on some higher load projects, however, Redis can work a lot more efficiently.  On a recent project, I was asked to look into implementing a Redis cache solution.  Not knowing anything about Redis the task sounded quite daunting, however, after some reading around the subject implementing Redis is actually surprisingly easy with .NET. In this particular scenario we wanted to cache HTML partial snippets in Redis due to its better scaling with large amounts of data. It's probably worth saying that the reason behind this approach was to use a donut hole cache output provider. What a donut hole cache is outside this tutorial, but if you are interested then I would recommend reading Episerver Caching – Output Caching Strategies Explained…. What Is a Donut Cache?. I've eluded that Redis is better than MemoryCache in certain situations, so it's probably a good idea to cover a few of those reasons briefly.

  • Redis can scale better in a distributed server set-up
  • Redis can run on several nodes
  • Redis can more efficiently retrieve data in large data stores
  • Redis has more advanced features than mem cache, async support, event notifications, various data structure support, advanced eviction policies, blocking queues

What Is Redis and How Can I Use It?

Simply put, Redis as an advanced and really quick key/value store.  The reason why you may consider Redis over, say a 'traditional' database, is because Redis stores and retrieves data from memory and not disk.  As Redis has a powerful feature set, when performance is critical in your application then Redis will give you a lot more customization ability than Memory Cache. I'm assuming that are you're workign with Episerver, your running a Windows box.  To get started with Redis you'll need to install it somewhere.  You can download Redis for Windows msi from,  You will also want to download the Redis Desktop client from here, so you can check that data is being added into Redis.  Think of Redis Desktop Manager as a similiar type of program that Sql Manager is to MS-SQL.

How To Get .NET Code To Talk To Redis

After you have a Redis server set-up, you'll need your code to talk to.  Asides from a conenction string to your Redis database, you'll also need .NET code that can manage this communication. Instead of having the write all this communication code yourself, you can use one of two third-party Nuget packages instead, these are: As most websites nowaday have a heavy angular or node presence on the front-end, async calls are more common practice in most of the builds I work with, so my recommendation would be to use the StackOverFlow version, but, please feel free to do your own research.  You can find good documentation about StackExchange.Redis here and there's a much better write up than I could do here as well (from this point onwards I'm assumign your using  the StackExchange version).

How To Use Redis In My Code?

Now we know which Redis client to use and what it does, the next step is using it.  First we need to create a connection to the Redis database, this can be done with the following code:
public IDatabase SetRedisConnection()
     var connectionString = string.Format("{0}:{1}", "", 6379);
     var connectionMultiplexer = ConnectionMultiplexer.Connect(connectionString);
     var cache = connectionMultiplexer.GetDatabase();
     return cache ;
In the code above, we set the connection string to Redis, specifying the host and port.  We created a connection to the database and return an IDatabase instance. IN Redis you can have more than one database, so in the GetDatabase() call you can additionally specify a database ID, like so:
     var cache = connectionMultiplexer.GetDatabase(1);
With Stackoverflow.Redis if you want to use async calls then you need to create an async ConnectionMultiplexer, in the SetRedisConnection method above you swap out the Connect method with the ConnectAsync method.
     connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(connectionString);
After you have a connection to Redis the CRUD operations are very similar to MemCache code:
    using StackExchange.Redis;

    public class RedisCacher
        private static IDatabase cache;

        public RedisCacher()
             cache = SetRedisConnection()

        public object GetValue(string key)
            return cache.StringGet(key);

        public bool Add(string key, object value, TimeSpan? absExpiration)
            return cache.StringSet(key, value.ToString(), absExpiration);
In the code above, we can now use the IDatabase cache object, to do a task like Add, Get, Delete. I won't go over this code as it's pretty self-explanatory.


In today's guide, I've had a brief discussion about what Redis is and why it's a better caching solution when you're working with very large amount of os cached data compared to standard Memory Cache. Redis comes with two popular.NET flavor, ServiceStack, and StackExchange. I would recommend Stackoverflow flow as it's open source and has better async support. Integrating Redis with Episerver is exactly the same as you would implement a normal MVC site. You install a Redis database on a server, you create a connection to the server and then you use the Stackoverflow.Redis API to perform tasks against it.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top