Scott Hanselman

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

November 6, '15 Comments [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">
<providers>
<add name="MySessionStateStore"
type="Microsoft.Web.Redis.RedisSessionStateProvider"
host="hanselcache.redis.cache.windows.net"
accessKey="THEKEY"
ssl="true"
port="1234" />
</providers>
</sessionState>

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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Friday, 06 November 2015 10:13:31 UTC
Do the packages you linked support AWS ElastiCache?
Kralizek
Friday, 06 November 2015 11:01:10 UTC
Hello Scott, thanks for promoting Redsmin!
Friday, 06 November 2015 13:47:40 UTC
Yes Kralizek, the stackoverflow libs does support aws elasticache with redis.
Luiz
Friday, 06 November 2015 14:10:20 UTC
I had no idea about the providers! Thankyou! Very useful.
Friday, 06 November 2015 17:57:37 UTC
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 http://www.cachemanager.net 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...
Dennis
Friday, 06 November 2015 18:12:34 UTC
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?
Stephen Brannan
Friday, 06 November 2015 18:16:24 UTC
Nevermind. A new search shows you can issue FLUSHALL. Looks like that's no longer an issue :-)
Stephen Brannan
Friday, 06 November 2015 18:45:05 UTC
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.
Taher
Friday, 06 November 2015 19:37:08 UTC
Another great Redis management tool is Redis-Commander. It is Node based and can be installed easily via npm.
Diego Terzano
Friday, 06 November 2015 20:12:27 UTC
"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.
Greg
Monday, 09 November 2015 16:16:59 UTC
Check out Redis.StackExchange.Extensions for quickly using complex objects with redis. I prefer using Protocol-buffers because of the speed and size benefits.
Rush
Monday, 09 November 2015 22:40:13 UTC
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.

thanks
Tuesday, 10 November 2015 00:15:44 UTC
Redis is cool. Good read. But keeping everything in CLR process is even faster:

http://www.infoq.com/articles/Big-Memory-Part-2

Actually serializing objects in-heap is faster than out-of-process round-trips (that anyway require serialization)
Tuesday, 10 November 2015 05:03:46 UTC
Very helpful. Thank you for sharing Scott
Tuesday, 10 November 2015 05:17:05 UTC
Thank you for sharing the good technique. I forwarded this link to my .Net developers. I hope they will found it helpful.
Tuesday, 10 November 2015 13:59:25 UTC
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 .
Wednesday, 11 November 2015 09:50:41 UTC
Will this work with Core aswell ?
Per B
Friday, 13 November 2015 15:29:24 UTC
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.

http://fiyazhasan.me/asp-net-session-satate-management-with-redis-local-server-farm-testing/
Comments are closed.

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