If you need to implement caching within your application, one of the first things that you will need to think about is where will the cached data live? If you are using a traditional infrastructure platform, you'll have your own web servers and you could consider using in-memory storage for your cache. In-memory caching is pretty simple, however, there are a number of downsides to this approach. If the cache gets too large, server performance can be affected. What happens to the cached data in load-balanced situations? What happens to the cache if the server restarts?

If you are hosting your website in the cloud then in-memory storage won't work correctly, as you're running your site within web apps where you don't have access to IIS itself. These limitations have meant that a lot of companies have started to look at more advanced options to deal with cached data, this is where Redis comes in.

What Is Redis?

Redis is an open-source in-memory data structure store that can be used for caching things. It supports almost all types of data structures such as strings, hashes, lists, sets, sorted sets with range queries, and bitmaps. Redis also can be used for messaging systems used as pub/sub. Redis is a newer technology and was built based on the learnings from a technology called Memcached. If you do some Google research, you can find that most people will recommend using Redis over Memcached as it’s faster and more configurable. With Redis, you can either self-host, or you can use a Redis cloud provider. To use Redis in your application, you will need to use a Redis provider and do some configuration tweaks 😱😫☕

What Is A .NET Redis Client?

Redis is not a programming language specific tool and it can be used with anything, like .NET or JavaScript. In order to talk to a Redis server, you will need a way to allow your C# code to get/add/delete things into it. Luckily, there are two popular Redis providers for .NET, so you don't have to write any code yourself. There are two main clients are:

  • StackExchange.Redis- ServiceStack.RedisLet us take a deeper dive into each one:

StackExchange.Redis

PRO

  • Better object caching support built-in

CON

  • Commercial product and required a license.- Doesn’t support async- Not as popular with only half a million downloads

Github Link: StackExchange.Redis:

ServiceStack.Redis

PRO

  • Free provider- Used on Stackoverflow, so proven- Open-source- Async support- Most popular with over a million downloads

    CON

  • Less feature-rich for object caching

Github Link: ServiceStack.Redis

Redis Technology Stack Recommendation

If you are building a cloud application, Redis is your best option. The 'StackOverflow.Redis' package works really well and it's free so for anyone new to Redis I recommend using that. StackExchange.Redis is the most feature-rich package, it is the most used .NET Redis package, it's open-source and free, what is not to love 🔥🔥🔥

If you're using traditional hosting, using a single on-prem instance you will likely get away with using in-memory session cache. If you're using a load balancer, Redis will work quicker than the alternative SQL provider approach.

Redis, as the newer and more versatile persistent storage, has almost always come out as a superior choice for caching whenever I've done a technology stack investigation. Another cool tool to be aware of is the free Redis Desktop Manager to manage the cache.

How To Implement Redis in Episerver?

I'm now assuming you've picked Redis and StackExchange Redis, the next step is to cover the code you'll need to use in order to start using it. First, we need to create a connection to the Redis database, this can be done with the following code:

In the code above, a connection string is set and pointed to Redis. The connection needs the host and port. A connection to the database, return an IDatabase instance. Using this object, you can now access the code like this:

If you need to introduce an HTML fragment cache or a form of data, Redis would be my recommendation at the time of writing (2017). In this tutorial, I've only scratched the surface of what Redis can do. Redis is a powerful platform and can provide a lot of benefits over regular in-memory sessions state. Happy Coding 🤘