Scott Hanselman

Using Redis as a Service in Azure to speed up ASP.NET applications

November 06, 2015 Comment on this post [18] Posted in ASP.NET MVC | Azure
Sponsored By

Microsoft Azure has a Redis Cache as a Service. There's two tiers. Basic is a single cache node, and Standard is as a complete replicated Cache (two nodes, with automatic failover). Microsoft manages automatic replication between the two nodes, and offers a high-availability SLA. The Premium tier can use up to a half-terabyte of RAM and tens of thousands of client connections and be clustered and scaled out to even bigger units. Sure, I could manage your own Redis in my own VM if I wanted to, but this is SAAS (Software as a Service) that I don't have to think about - I just use it and the rest is handled.

I blogged about Redis on Azure last year but wanted to try it in a new scenario now, using it as a cache for ASP.NET web apps. There's also an interesting open source Redis Desktop Manager I wanted to try out. Another great GUI for Redis is Redsmin.

For small apps and sites I can make a Basic Redis Cache and get 250 megs. I made a Redis instance in Azure. It takes a minute or two to create. It's SSL by default. I can talk to it programmatically with something like StackExchange.Redis or ServiceStack.Redis or any of a LOT of other great client libraries.

However, there's now great support for caching and Redis in ASP.NET. There's a library called Microsoft.Web.RedisSessionStateProvider that I can get from NuGet:

Install-Package Microsoft.Web.RedisSessionStateProvider 

It uses the StackExchange library under the covers, but it enables ASP.NET to use the Session object and store the results in Redis, rather than in memory on the web server. Add this to your web.config:

<sessionState mode="Custom" customProvider="FooFoo">
<add name="MySessionStateStore"
port="1234" />

Here's a string from ASP.NET Session stored in Redis as viewed in the Redis Desktop Manager. It's nice to use the provider as you don't need to change ANY code.

ASP.NET Session stored in a Redis Cache

You can turn off SSL and connect to Azure Redis Cache over the open internet but you really should use SSL. There's instructions for using Redis Desktop Manager with SSL and Azure Redis. Note the part where you need a .pem file which is the Azure Redis Cache SSL public key. You can get that SSL key here as of this writing.

Not only can you use Redis for Session State, but you can also use it for a lightning fast Output Cache. That means caching full HTTP responses. Setting it up in ASP.NET 4.x is very similar to the Session State Provider:

Install-Package Microsoft.Web.RedisOutputCacheProvider 

Now when you use [OutputCache] attributes in MVC Controllers or OutputCache directives in Web Forms like <%@ OutputCache Duration="60" VaryByParam="*" %> the responses will be handled by Redis. With a little thought about how your query strings and URLs work, you can quickly take an app like a Product Catalog, for example, and make it 4x or 10x faster with caching. It's LOW effort and HIGH upside. I am consistently surprised even in 2015 how often I see folks going to the database on EVERY HTTP request when the app's data freshness needs just doesn't require the perf hit.

You can work with Redis directly in code, of course. There's docs for .NET, Node.js, Java and Python on Azure. It's a pretty amazing project and having it be fully managed as a service is nice. From the Azure Redis site:

Perhaps you're interested in Redis but you don't want to run it on Azure, or perhaps even on Linux. You can run Redis via MSOpenTech's Redis on Windows fork. You can install it from NuGet, Chocolatey or download it directly from the project github repository. If you do get Redis for Windows (super easy with Chocolatey), you can use the redis-cli.exe at the command line to talk to the Azure Redis Cache as well (of course!).

It's easy to run a local Redis server with redis-server.exe, test it out in development, then change your app's Redis connection string when you deploy to Azure. Check it out. Within 30 min you may be able to configure your app to use a cache (Redis or otherwise) and see some really significant speed-up.

Sponsor: Big thanks to my friends at Octopus Deploy for sponsoring the feed this week. Build servers are great at compiling code and running tests, but not so great at deployment. When you find yourself knee-deep in custom scripts trying to make your build server do something it wasn't meant to, give Octopus Deploy a try.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
November 06, 2015 14:13
Do the packages you linked support AWS ElastiCache?
November 06, 2015 15:01
Hello Scott, thanks for promoting Redsmin!
November 06, 2015 17:47
Yes Kralizek, the stackoverflow libs does support aws elasticache with redis.
November 06, 2015 18:10
I had no idea about the providers! Thankyou! Very useful.
November 06, 2015 21:57
In memory cache read access is still 100 times faster than a distributed cache like Redis. Maybe you should mention that, too, if you write "use Redis to make your website faster" ;)

For object cache and request cache i'm currently using for my azure hosted web apps. Did you ever tried that?

It is great because I can have 2 levels of cache, in Memory + Redis behind it and a lot of configuration options.
One of my apps runs on multiple nodes and this thing just works awesome, in sync etc...
November 06, 2015 22:12
The azure redis cache is one of my favorite features of Azure. I use it for caching and lightweight session state in ServiceStack. Very easy to implement and use.

The only issue I found with Azure redis is how to clear the cache from an administrative perspective without re-creating the whole redis instance in Azure?
November 06, 2015 22:16
Nevermind. A new search shows you can issue FLUSHALL. Looks like that's no longer an issue :-)
November 06, 2015 22:45
We use server farm and its important to maintain sessions across servers. Hence we use redis as a session store. We are using msopentech fork and are happy with it.
November 06, 2015 23:37
Another great Redis management tool is Redis-Commander. It is Node based and can be installed easily via npm.
November 07, 2015 0:12
"It's nice to use the provider as you don't need to change ANY code."

Not exactly correct. In order to store an object in Redis via this provider (or anything other than in-memory sessions), the class must be serializable.

So, unless you have a very simple app, you will need to change code.
November 09, 2015 20:16
Check out Redis.StackExchange.Extensions for quickly using complex objects with redis. I prefer using Protocol-buffers because of the speed and size benefits.
November 10, 2015 2:40
I've always been advised to stay away from session state. I Understand this advice is probably centered around the fact that the default provided is in-memory which falls over when you have more than one server. So is using sesion storage ok if you distribute the backend? Or should I be just using claims based auth and querying the cache given those claims values.

November 10, 2015 4:15
Redis is cool. Good read. But keeping everything in CLR process is even faster:

Actually serializing objects in-heap is faster than out-of-process round-trips (that anyway require serialization)
November 10, 2015 9:03
Very helpful. Thank you for sharing Scott
November 10, 2015 9:17
Thank you for sharing the good technique. I forwarded this link to my .Net developers. I hope they will found it helpful.
November 10, 2015 17:59
I use Redis, and it is good replacement for memcache. For some who ask why not just use in-memory cache, the answer is that you cant share the memory with multiple instances while these cache servers allow you to do that. it also protect the cached data from app crashing and recycling .
November 11, 2015 13:50
Will this work with Core aswell ?
November 13, 2015 19:29
Great article! Microsoft.Web.RedisSessionStateProvider is a cool package (JUST INSTALL AND CONFIGURE IT IN WEB.CONFIG). I used it once to store sessions in REDIS on local testing server farm.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.