What is Redis and Should I Be Using It?

When we start to think about caching, one of the first things that you will need to think about is where will the cached data live? 

First, if you're 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, like server performances can be affected, what happens in load balanced situations, what happens to the cache if you do an IIS reset etc...  

Second, if you're 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 for their cache store, this is where Redis comes in.

What Is Redis?

Redis is an open source in-memory data structure store which also can be used as a database as well as caching. It supports almost all types of data structures such as strings, hashes, lists, sets, sorted sets with range queries, 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 Memcached. From some simple Googling, you can find that most people will recommend using Redis over MemCached as it’s faster and most configurable. With Redis you can either self-host, or you can use a Redis cloud provider. The website would be configured to use a Redis provider in config and the site's whole session caching would be moved.

What Is A .NET Redis Client?

Redis is not written within .NET and 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 clients that have been written that will do this for you, so you don't have to write any code yourself.   There are two main Redis providers for .NET:

ServiceStack.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

StackExchange.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

Redis Technology Stack Recommendation

If you're starting a new project in the cloud then Redis is your best option.  The StackOverflow.Redis C# implementation works really well and it's free so I'd go with that.  If you're using traditional hosting then single instances with low usage can get away with using session.  If you're using a load balancer, then Redis will work quicker that the alternative SQL provider.  

Redis, as the newer and more versatile persistant storage, has almost always come out as superior choice for caching whenever I've done a technology stack investigation.  In terms of which Redis client to use, then StackExchange.Redis is the more feature rich version, is more popular, open source and free and I'd recommend using that.  Another cool tool to be aware of is the free Redis Desktop Manager to manage the cache.

How To Implement Redis in Episerver? Using StackExchange.Redis

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:

public IDatabase SetRedisConnection()
{
    var connectionString = string.Format("{0}:{1}", "127.0.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.  We can now add and get items like this:
var database = SetRedisConnection()

var key = "hello";
var valueAsString = database.StringGet(key);

var value = "world";
database.StringSet(key, value);

Redis Takeaway

If you need to cache in-memory/temporary data and you're considering cache providers, then Redis woudl be my 
recommendation at the time of writing (2017). In this tutorial I've only sctrached the basics of what Redis
can do. Redis ia a powerful platform and can provide a lot of benefits for regular in-memory session
provided by .NET.

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