For programmers, testers and tech geeks

SignalR Scale-out using Redis

Before going in depth with Redis for why and how part first let's build scenario in which I explored this option and used Redis for this purpose. Recently we were implementing a chatting solution for native apps, one option was the fire-base integration. Al-tough this was a good option in terms of availability and infrastructure ( who is best then google ) but we were little bit reluctant in sharing our data with any third party even if we apply data encryption. As the result we came on SignalR option and find it very robust and easy to implement so we build a sample chat application with SignalR as server. Next thing was the performance KPIs. Most common points were like :

  • Max number of connected users
  • Max size of message
  • How to provide availability
  • Connections persistence

And while exploring different options available we came across Redis service. On official site it says " Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. "

On exploring further we found Redis is also recommended by Microsoft officially for this purpose with some other options as well.   This is when we decided to integrate Signalr with Redis.  Redis backplane acts like a middleman between the two clients and notifies all subscribed clients with any changes. 

To implement Redis we have nuget package available i.e. Microsoft.AspNet.SignalR.Redis. Once you have installed this package, you need to update your Startup.cs file like this :

using Owin;
using Microsoft.Owin;
using Microsoft.AspNet.SignalR;
[assembly: OwinStartup(typeof(SignalRwithRedis.Startup))]
 
namespace SignalRwithRedis
{
	public class Startup
	{
		public void Configuration(IAppBuilder app)
		{
			GlobalHost.DependencyResolver.UseRedis("localhost", 6379, string.Empty, "myApp");
			app.MapHubs();
		}
	}
}

In above snippet UseRedis function is taking four parameters where "localhost" is address where redis service is deployed. Will look into detail about how to download and install redis service in other post. "6379" is port number where Redis service is listening. "string.Empty" is password parameter which is by default empty when we install Redis service. And the last parameter is the application name. You can use any suitable one you want. This name helps us to track the message traffic for specific app going through Redis middle layer we will also see this in detail how to manipulate messages between clients but for this post that's it. We have now scaled out our SignalR server. I hope this would help to understand and implement Redis. In case of any query or positive feedback put your comments below.