10 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|---|
Redis backplane for ASP.NET Core SignalR scale-out | bradygaster | Learn how to set up a Redis backplane to enable scale-out for an ASP.NET Core SignalR app. | >= aspnetcore-2.1 | bradyg | mvc | 11/12/2019 | signalr/redis-backplane |
Set up a Redis backplane for ASP.NET Core SignalR scale-out
By Andrew Stanton-Nurse, Brady Gaster, and Tom Dykstra,
This article explains SignalR-specific aspects of setting up a Redis server to use for scaling out an ASP.NET Core SignalR app.
Set up a Redis backplane
-
Deploy a Redis server.
[!IMPORTANT] For production use, a Redis backplane is recommended only when it runs in the same data center as the SignalR app. Otherwise, network latency degrades performance. If your SignalR app is running in the Azure cloud, we recommend Azure SignalR Service instead of a Redis backplane.
For more information, see the following resources:
:::moniker range="= aspnetcore-2.1"
-
In the SignalR app, install the
Microsoft.AspNetCore.SignalR.Redis
NuGet package. -
In the
Startup.ConfigureServices
method, callAddRedis
afterAddSignalR
:services.AddSignalR().AddRedis("<your_Redis_connection_string>");
-
Configure options as needed:
Most options can be set in the connection string or in the ConfigurationOptions object. Options specified in
ConfigurationOptions
override the ones set in the connection string.The following example shows how to set options in the
ConfigurationOptions
object. This example adds a channel prefix so that multiple apps can share the same Redis instance, as explained in the following step.services.AddSignalR() .AddRedis(connectionString, options => { options.Configuration.ChannelPrefix = "MyApp"; });
In the preceding code,
options.Configuration
is initialized with whatever was specified in the connection string.
:::moniker-end
:::moniker range="= aspnetcore-2.2"
-
In the SignalR app, install one of the following NuGet packages:
Microsoft.AspNetCore.SignalR.StackExchangeRedis
- Depends on StackExchange.Redis 2.X.X. This is the recommended package for ASP.NET Core 2.2 and later.Microsoft.AspNetCore.SignalR.Redis
- Depends on StackExchange.Redis 1.X.X. This package isn't included in ASP.NET Core 3.0 and later.
-
In the
Startup.ConfigureServices
method, call xref:Microsoft.Extensions.DependencyInjection.StackExchangeRedisDependencyInjectionExtensions.AddStackExchangeRedis*:services.AddSignalR().AddStackExchangeRedis("<your_Redis_connection_string>");
When using Microsoft.AspNetCore.SignalR.Redis
, call xref:Microsoft.Extensions.DependencyInjection.RedisDependencyInjectionExtensions.AddRedis*.
-
Configure options as needed:
Most options can be set in the connection string or in the ConfigurationOptions object. Options specified in
ConfigurationOptions
override the ones set in the connection string.The following example shows how to set options in the
ConfigurationOptions
object. This example adds a channel prefix so that multiple apps can share the same Redis instance, as explained in the following step.services.AddSignalR() .AddStackExchangeRedis(connectionString, options => { options.Configuration.ChannelPrefix = "MyApp"; });
When using Microsoft.AspNetCore.SignalR.Redis
, call xref:Microsoft.Extensions.DependencyInjection.RedisDependencyInjectionExtensions.AddRedis*.
In the preceding code, options.Configuration
is initialized with whatever was specified in the connection string.
For information about Redis options, see the StackExchange Redis documentation.
:::moniker-end
:::moniker range=">= aspnetcore-3.0"
-
In the SignalR app, install the following NuGet package:
Microsoft.AspNetCore.SignalR.StackExchangeRedis
-
In the
Startup.ConfigureServices
method, call xref:Microsoft.Extensions.DependencyInjection.StackExchangeRedisDependencyInjectionExtensions.AddStackExchangeRedis*:services.AddSignalR().AddStackExchangeRedis("<your_Redis_connection_string>");
-
Configure options as needed:
Most options can be set in the connection string or in the
ConfigurationOptions
object. Options specified inConfigurationOptions
override the ones set in the connection string.The following example shows how to set options in the
ConfigurationOptions
object. This example adds a channel prefix so that multiple apps can share the same Redis instance, as explained in the following step.services.AddSignalR() .AddStackExchangeRedis(connectionString, options => { options.Configuration.ChannelPrefix = "MyApp"; });
In the preceding code,
options.Configuration
is initialized with whatever was specified in the connection string.For information about Redis options, see the StackExchange Redis documentation.
:::moniker-end
-
If you're using one Redis server for multiple SignalR apps, use a different channel prefix for each SignalR app.
Setting a channel prefix isolates one SignalR app from others that use different channel prefixes. If you don't assign different prefixes, a message sent from one app to all of its own clients will go to all clients of all apps that use the Redis server as a backplane.
-
Configure your server farm load balancing software for sticky sessions. Here are some examples of documentation on how to do that:
Redis server errors
When a Redis server goes down, SignalR throws exceptions that indicate messages won't be delivered. Some typical exception messages:
- Failed writing message
- Failed to invoke hub method 'MethodName'
- Connection to Redis failed
SignalR doesn't buffer messages to send them when the server comes back up. Any messages sent while the Redis server is down are lost.
SignalR automatically reconnects when the Redis server is available again.
Custom behavior for connection failures
Here's an example that shows how to handle Redis connection failure events.
:::moniker range="= aspnetcore-2.1"
services.AddSignalR()
.AddRedis(o =>
{
o.ConnectionFactory = async writer =>
{
var config = new ConfigurationOptions
{
AbortOnConnectFail = false
};
config.EndPoints.Add(IPAddress.Loopback, 0);
config.SetDefaultPorts();
var connection = await ConnectionMultiplexer.ConnectAsync(config, writer);
connection.ConnectionFailed += (_, e) =>
{
Console.WriteLine("Connection to Redis failed.");
};
if (!connection.IsConnected)
{
Console.WriteLine("Did not connect to Redis.");
}
return connection;
};
});
:::moniker-end
:::moniker range="> aspnetcore-2.1"
services.AddSignalR()
.AddMessagePackProtocol()
.AddStackExchangeRedis(o =>
{
o.ConnectionFactory = async writer =>
{
var config = new ConfigurationOptions
{
AbortOnConnectFail = false
};
config.EndPoints.Add(IPAddress.Loopback, 0);
config.SetDefaultPorts();
var connection = await ConnectionMultiplexer.ConnectAsync(config, writer);
connection.ConnectionFailed += (_, e) =>
{
Console.WriteLine("Connection to Redis failed.");
};
if (!connection.IsConnected)
{
Console.WriteLine("Did not connect to Redis.");
}
return connection;
};
});
:::moniker-end
Redis Cluster
Redis Cluster utilizes multiple simultaneously active Redis servers to achieve high availability. When Redis Cluster is used as the backplane for SignalR, messages are delivered to all of the nodes of the cluster without code modifications to the app.
There's a tradeoff between the number of nodes in the cluster and the throughput of the backplane. Increasing the number of nodes increases the availability of the cluster but decreases the throughput because messages must be transmitted to all of the nodes in the cluster.
In the SignalR app, include all of the possible Redis nodes using either of the following approaches:
- List the nodes in the connection string delimited with commas.
- If using custom behavior for connection failures, add the nodes to
ConfigurationOptions.Endpoints
.
Next steps
For more information, see the following resources: