signalr
  1. signalr-scaling-signalrapplications

Scaling SignalR Applications - Scale-out with SignalR

Syntax

// In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR().AddStackExchangeRedis("<connection string>");
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseSignalR(routes =>
    {
        routes.MapHub<MyHub>("/myhub");
    });
}

Example

Let's say we have a SignalR application with a single instance and we want to scale it out to multiple instances. We can achieve this by using a distributed cache like Redis.

Setting up Redis

  1. Create a Redis instance on a hosting service like Azure or AWS.
  2. Obtain the connection string for the Redis instance.

Scaling SignalR application

  1. Install the Microsoft.AspNetCore.SignalR.StackExchangeRedis NuGet package.
  2. In the ConfigureServices method in the Startup.cs file, add services.AddSignalR().AddStackExchangeRedis("<connection string>");.
  3. In the Configure method in the Startup.cs file, add app.UseSignalR(routes => { routes.MapHub<MyHub>("/myhub"); });.

Output

After implementing scale-out with SignalR, we will have multiple instances of our SignalR application running simultaneously. This results in improved performance and scalability.

Explanation

By default, SignalR uses an in-memory message bus to handle communication between clients and the server. When scaling out to multiple instances, this in-memory message bus does not work as each instance would have its own set of connected clients.

Therefore, we use a distributed cache like Redis to store the message bus and allow all instances to communicate with each other. This is achieved through the use of the AddStackExchangeRedis method in the ConfigureServices method of the Startup class.

It is important to note that in this setup, all connected clients are stored in the Redis cache, which can impact performance and increase latency. Therefore, it is recommended to implement Redis pub/sub mechanism, which allows for reduced Redis interactions and also uses the publish/subscribe model of Redis to deliver messages from one server to another.

Use

Scale-out with SignalR is used in scenarios where a single instance of SignalR is not sufficient to handle the load or when redundancy is required. It allows for improved performance and scalability, and is commonly used in real-time web applications.

Important Points

  • Scale-out with SignalR requires a distributed cache like Redis.
  • Redis pub/sub mechanism is recommended for better performance and latency.
  • Scale-out with SignalR is used in scenarios where more than one instance of SignalR is required.
  • The AddStackExchangeRedis method is used to add Redis support in the application.

Summary

In this article, we discussed scale-out with SignalR. We saw how a distributed cache like Redis is used to handle communication between multiple instances of SignalR and how to implement it in a SignalR application. We also discussed the benefits, use cases, and important points to keep in mind while implementing scale-out with SignalR.

Published on: