Get Started With NCache Backplane For .NET Core SignalR

What is ASP.NET Core SignalR? 

ASP.NET Core SignalR is an open-source Microsoft API that is used to push the content from server-side to clients instantly. It manages the connection automatically and seamlessly. It is used to add any kind of real-time web functionality to your ASP.NET Core application. ASP.NET Core SignalR uses remote procedure calls, shortened to RPC, to call the client from the server. It takes advantage of WebSocket, which is a communication protocol that is used to enable two-way persistent communication channels over TCP connections.

ASP.NET Core SignalR provides the same core concepts and functionalities as SignalR. Hubs are the core connection point between the server and the clients. Clients can trigger the methods on the hub, and the hub can trigger the methods for the clients. The hub will have the complete control over the connections between server and client. 

Use cases for SignalR:

  • Chat room applications
  • Real-time monitoring applications
  • Job progress updates
  • Real-time forms 
  • IoT Device control and so on.  

What is NCache?

NCache is an Open Source in-memory distributed cache for .NET, Node.js., and Java. NCache is very fast and scalable, and it caches application data to reduce database transactions. By using NCache, we can easily overcome performance bottlenecks related to your data storage, and scale your .NET, Java, and Node.js applications to extreme transaction processing.

In real time the .NET or .NET Core SignalR web applications will have more transaction updates from the web server to clients. To handle the high traffic in ASP.NET or ASP.NET Core applications, the SignalR must scale to a multi-server environment. In such case, the SignalR needs to distribute the messages across multiple web servers. The NCache with SignalR backplane will resolve this performance issue. 

In this article, I’m going to explain how to Use NCache Backplane with .NET Core SignalR.

SignalR BackPlane Approach 

SignalR BackPlane is a shared bus/repository. It is a shared resource where all your web servers are connected. These web servers have their own respective clients with load balance in between. The web server, instead of sending the message to their own clients, if you have a WebFarm configured and you can use the backplane. These web servers, instead of sending messages or invoking methods to their connected clients, can actually send a message to the backplane. That would broadcast the message to all the web servers. The backplane has all the web servers connected. Based on whichever webserver is going to invoke a method, it’s going to send a message through signalR to its own clients and then backplane transmit it to all connected clients. 

Bottlenecks with SignalR Backplane

1. Real-time applications need less latency and more Throughput 

  • SignalR Backplane needs low latency and high Throughput
  • Database as SignalR Backplane is slow, Database can not scale out with increasing application load. 

2. SignalR apps need guaranteed delivery of messages 

  • SignalR Backplane must be reliable 
  • Database can choke down under extreme load and is a single point failure sometime 

3. SignalR Backplane must have high availability built into it 

  • SignalR backplane needs to be highly available 
  • Backplane maintenance or unplanned outage can cause service delivery issue 

The solution is to overcome all these bottlenecks is using scalable In-memory Distributed Cache.

What is an In-Memory Distributed Cache? 

1.    Cluster of multiple inexpensive cache servers
             -    Pools their memory and CPU into one logical capacity 
2.    Synchronizes cache updates across all cache servers 
             -    Cache updates are immediately visible from all cache servers 
3.    Linearly scales transaction & memory capacity 
             -    Just add more cache server to grow capacity 
4.    Replicates data for reliability 
            -    Intelligent replication without compromising performance & scalability.

Integrating NCache as a Backplane for ASP.Net Core SignalR Application. 

Click here to check how to install the NCache and how to manage the cache using NCache web monitoring application

I’m going to use my existing ASP.NET Core signalR application for the demo. You can download the source code from github. Please read this article to understand how to create an ASP.NET Core SignalR application. 

Add the below JSON object in appsettings.json file:

  "NCacheConfiguration": {
    "CacheName": "myLocalCache",
    "ApplicationID": "chatApplication"
  },

CacheName - Provide your newly created cache name 

ApplicationID – Give some unique string relevant to your application 

Download and install the package AspNetCore.SignalR.NCache from Nuget package Manager.

Add the below code in Startup.cs file:

services.AddSignalR().AddNCache(ncacheOptions => {
    ncacheOptions.CacheName = Configuration["NCacheConfiguration:CacheName"];
    ncacheOptions.ApplicationID = Configuration["NCacheConfiguration:ApplicationID"];
});

Run the application and try to chart from two user accounts. Once the NCache initiates, you can see the client count in NCache web monitor application, as shown in the below figure: 

Communication between the users:

Client Connection established, now the NCache act as a backplane for our SignalR application:

Summary

We learned to get started with NCache Backplane for ASP.NET Core SignalR application, and how to overcome performance bottlenecks using the NCache distributed cache. We will see more about NCache in my next article.