skip to content

For an ASP.NET Core SignalR app, when would you choose Azure SignalR Service with AddAzureSignalR over a Redis backplane, and what changes on your servers?

level: seniorimportance: must knowfreq 45%

answer

  1. who holds the persistent connections
  2. scale by connections vs by messages
  3. Azure:SignalR:ConnectionString
  4. InitialHubServerConnectionCount, default 5
  5. no affinity on the app servers

basics

~20 s

With a Redis backplane your servers hold every client connection, so you scale by connection count and need sticky sessions. AddAzureSignalR moves the connections to the service: each server keeps a few connections per hub, scales with message load and needs no affinity.

solid answer

~50 s

A SignalR connection is persistent: it holds a TCP connection and memory on a server for as long as the client stays, idle or not. With a Redis backplane the six servers of a commentary hub hold every fan's connection, so capacity is set by connection count, the farm needs sticky sessions, and SignalR competes for sockets with anything else on the host; the docs advise running it apart from other web apps. `AddSignalR().AddAzureSignalR()` (package `Microsoft.Azure.SignalR`, connection string from `Azure:SignalR:ConnectionString`) replaces the lifetime manager: negotiate redirects clients to the service with a URL and an access token, the service holds their connections, and each server opens a small pool per hub, `InitialHubServerConnectionCount` defaulting to 5. Servers then scale with message volume and need no affinity. Redis stays the choice for self-hosted or on-premises apps where latency to an Azure region matters; the docs recommend the service for SignalR apps hosted on Azure.

code

csharp · 15 lines
csharp
using Microsoft.Azure.SignalR;

var builder = WebApplication.CreateBuilder(args);

// Reads Azure:SignalR:ConnectionString from configuration, then applies the callback.
builder.Services.AddSignalR()
    .AddAzureSignalR(options =>
    {
        options.InitialHubServerConnectionCount = 5; // the default, shown for clarity
        options.GracefulShutdown.Mode = GracefulShutdownMode.WaitForClientsClose;
    });

var app = builder.Build();
app.MapHub<CommentaryHub>("/hubs/commentary");
app.Run();

go deeper

for a junior

Recall that a SignalR client keeps its connection open the whole time, and that Azure SignalR Service holds those connections instead of your servers.

for a middle

Explain what AddAzureSignalR changes: negotiate redirects clients to the service, each server keeps a few connections per hub, and the connection string comes from Azure:SignalR:ConnectionString.

for a senior

Size a self-hosted farm by concurrent connections and memory, set Kestrel connection caps on purpose, and argue Redis against the service on latency, hosting location and deployment behaviour.

for a principal

Own the trade between running connection capacity yourself and depending on a managed service, including data-center placement, behaviour during deployments and who answers when either side fails.

## What a persistent connection costs a server Ordinary HTTP clients use connections that can close when idle. A SignalR connection stays open the whole time the client is on the page, so the server pays for it continuously: - **A TCP connection.** A server can hold only so many. When it runs out, the docs describe random socket errors and connection resets, for example "An attempt was made to access a socket in a way forbidden by its access permissions". - **Memory.** Each connection carries buffers and bookkeeping, idle or not. - **Neighbours.** If SignalR holds the last free connections, other web apps on the same server have none left, so the docs advise running SignalR on servers of its own. - **Kestrel caps.** A WebSocket connection counts against `KestrelServerLimits.MaxConcurrentUpgradedConnections`, not `MaxConcurrentConnections`, and both default to `null`, meaning no limit. Setting caps deliberately turns overload into rejected connections instead of a server that degrades for everyone. Capping only upgraded connections can push clients down to Server-Sent Events or long polling, which count against `MaxConcurrentConnections` instead, so think about both. - **IIS on a Windows client OS** allows 10 concurrent connections, so it is unsuitable even for testing SignalR under load. ## Scaling with a Redis backplane With `AddStackExchangeRedis`, your servers still hold every client: - You scale out by **number of clients**, even when few messages flow. - The load balancer needs **sticky sessions**. - Redis should sit **in the same data center** as the app. For a six-server sports-commentary hub, that means sizing each server for its share of concurrent fans at kick-off, not for the message rate. A workable sizing routine: 1. Load-test one server to find the memory and socket cost per idle connection, and the point where latency degrades. 2. Estimate peak concurrent connections, which arrive in a burst at kick-off, not spread over the day. 3. Leave headroom for one server failing: its fans reconnect and land on the other five. 4. Set Kestrel's connection caps below the measured breaking point, so an overloaded server rejects newcomers instead of slowing every fan already connected. ## Scaling with Azure SignalR Service The `Microsoft.Azure.SignalR` package adds `AddAzureSignalR()` to `ISignalRServerBuilder`. It replaces the lifetime manager with the SDK's own and changes the connection flow: 1. The client calls `negotiate` on your app as before. 2. The app answers with the service's URL and an access token carrying the user's claims, instead of creating a local connection. 3. The client connects to the service, which holds the connection. 4. The service forwards hub calls to your servers over **server connections** that each server opens per hub. | `ServiceOptions` member | Default | |---|---| | `ConnectionString` | read from `Azure:SignalR:ConnectionString` | | `InitialHubServerConnectionCount` | 5 per hub (replaces the obsolete `ConnectionCount`) | | `MaxHubServerConnectionCount` | `null`, no cap | | `ServerStickyMode` | `Disabled` | | `GracefulShutdown.Mode` | `Off` | | `AccessTokenLifetime` | 1 hour | Hub classes, `MapHub`, groups and `IHubContext` stay the same. ## Choosing between them | Concern | Redis backplane | Azure SignalR Service | |---|---|---| | Who holds client connections | your servers | the service | | Sticky sessions on the app | required | not required | | What drives app scale-out | connection count | message load | | Network placement | Redis next to the app | an Azure region | The docs recommend the service for SignalR apps hosted on Azure: App Service, virtual machines and containers. They recommend Redis for apps on your own infrastructure, especially when latency between your data center and Azure would hurt low-latency or high-throughput traffic. ## What does not change Neither option turns pushes into a durable queue for your app. Keep the authoritative match state in your own store, and let clients resync after a reconnect.

  • What does ServerStickyMode do in the Azure SignalR SDK, and when would you change it?
    It decides whether the service must route a client to the app server that handled its negotiate. The default, `Disabled`, lets the SDK pick a server connection, preferring local and less-loaded ones. `Required` insists on the negotiating server and fails if that server is not connected. Change it only when negotiate stores per-client state in that server's memory that the hub later needs.
  • What happens to clients when an app server behind Azure SignalR Service restarts for a deployment?
    With the default `GracefulShutdown.Mode` of `Off`, the server stops at once and its clients' connections drop. `WaitForClientsClose` removes the server from the service so it gets no new clients, then waits for existing ones to close, up to `GracefulShutdown.Timeout` (30 seconds by default). `MigrateClients` tries to move connections to another server at message boundaries, which suits hubs that keep no per-connection state in memory.

  • With Azure SignalR Service, each app server still holds one socket per client.
  • A Redis backplane lets the farm scale by message volume rather than by connection count.
  • Idle SignalR clients cost the server nothing because they send no requests.
  • AddAzureSignalR requires replacing MapHub with UseAzureSignalR route registration.
  • Kestrel caps WebSocket connections at a fixed default number per server.

Assumes ASP.NET Core SignalR 10 with Microsoft.Azure.SignalR 1.33, where InitialHubServerConnectionCount replaces the obsolete ConnectionCount option.