skip to content

In ASP.NET Core SignalR, why does Clients.All reach only some clients once the hub runs on several servers, and what fixes it?

level: juniorimportance: should knowfreq 42%

answer

  1. connections live in one process's memory
  2. HubLifetimeManager<THub>, one singleton per process
  3. DefaultHubLifetimeManager knows local connections only
  4. AddStackExchangeRedis or AddAzureSignalR
  5. IHubContext sends hit the same wall

basics

~20 s

Each server's SignalR tracks only its own connections in DefaultHubLifetimeManager, so Clients.All sent on one server skips clients on the others. A Redis backplane (AddStackExchangeRedis) or Azure SignalR Service (AddAzureSignalR) carries every send to every server.

solid answer

~40 s

`AddSignalR()` registers `DefaultHubLifetimeManager<THub>` as the singleton `HubLifetimeManager<THub>`, and it keeps the hub's connections, groups and users in that process's memory. `Clients.All`, `Clients.Group`, `Clients.User` and every `IHubContext<THub>` send are resolved against that local list, so with a commentary hub on six servers a goal pushed from server 3 reaches only the fans whose connections landed on server 3, and nothing reports an error. The fix is to swap the lifetime manager: `AddStackExchangeRedis` registers `RedisHubLifetimeManager<THub>`, which publishes each send to Redis channels every server subscribes to, while `AddAzureSignalR` registers the Azure SignalR SDK's lifetime manager and lets the service hold the client connections. Hub code does not change. A Redis backplane still needs sticky sessions and does not buffer messages.

code

csharp · 14 lines
csharp
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSignalR()
    .AddStackExchangeRedis(builder.Configuration.GetConnectionString("backplane")!);

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

public class CommentaryHub : Hub
{
    public Task PostLine(string matchId, string line) =>
        Clients.Group(matchId).SendAsync("Commentary", line);
}

go deeper

for a junior

Remember that SignalR keeps connections in the memory of the server process that accepted them, so Clients.All reaches only that process's clients until a backplane or Azure SignalR Service is added.

for a middle

Explain that every send goes through HubLifetimeManager<THub>, that AddSignalR registers the in-memory DefaultHubLifetimeManager, and that AddStackExchangeRedis or AddAzureSignalR swaps it without touching hub code.

for a senior

Point out that the failure is silent, that IHubContext sends behave the same way, and that a Redis backplane still needs sticky sessions and drops sends while Redis is down.

for a principal

Weigh a self-hosted Redis backplane, which keeps connections on your servers, against a managed service that takes them away, in terms of latency, data-center placement and how the fleet must scale.

## One process, one list of connections Every send from a SignalR **hub** goes through a `HubLifetimeManager<THub>`: the service that knows which connections exist, which groups they joined and which user each belongs to. `builder.Services.AddSignalR()` registers `DefaultHubLifetimeManager<THub>` for it with `TryAddSingleton`, so there is exactly one per hub type **per process**, and it keeps everything in memory. On one server that is all you need. Every send API ends up in that manager: - `Clients.All`, `Clients.Others` and `Clients.AllExcept(...)` walk the local connections. - `Clients.Group("match-42")` reads the local group table. - `Clients.User(userId)` looks for local connections whose user identifier matches. - `IHubContext<CommentaryHub>`, used by a background score feed or a controller, calls the same manager, so it has exactly the same reach. ## What goes wrong on six servers Picture a live sports-commentary hub behind a load balancer with six servers. Each fan's connection lands on one of them. The commentator's "Goal!" message is handled by server 3, whose hub method calls `Clients.Group("match-42").SendAsync("Commentary", line)`. | Send | Who receives it without a backplane | |---|---| | `Clients.Caller` | the caller, who is on server 3 by definition | | `Clients.All` / `Clients.Others` | only connections on server 3 | | `Clients.Group("match-42")` | only group members connected to server 3 | | `Clients.User(id)` | only that user's tabs connected to server 3 | | `IHubContext` send from a feed on server 5 | only connections on server 5 | Nothing throws. The sends complete normally and five sixths of the audience never hears the goal. Because the failure is silent, it usually shows up in production rather than in a single-machine test. ## The fix: replace the lifetime manager Scale-out in ASP.NET Core SignalR means registering a different `HubLifetimeManager<>`: 1. **Redis backplane**: the `Microsoft.AspNetCore.SignalR.StackExchangeRedis` package and `AddSignalR().AddStackExchangeRedis(connectionString)`, which registers `RedisHubLifetimeManager<THub>`. Each server subscribes to Redis channels for "all", for every group and user it currently holds, and for each local connection. A send is published once, and each server writes it to its own matching connections. 2. **Azure SignalR Service**: the `Microsoft.Azure.SignalR` package and `AddSignalR().AddAzureSignalR()`. Clients are redirected to the service, which holds their connections, and the app servers send through a few connections of their own to the service. 3. **Other providers**: the docs list several non-Microsoft backplanes built on the same extension point. Hub classes, `MapHub<CommentaryHub>("/hubs/commentary")`, `IHubContext<T>` callers and client code stay as they are; only the service registration changes. ## The same goal, with Redis With the Redis backplane registered, the goal travels like this: 1. A fan on server 2 joins group `match-42`; server 2 subscribes to that group's Redis channel because it now holds a member. 2. The commentator's call reaches server 3, whose hub method calls `Clients.Group("match-42").SendAsync(...)`. 3. Server 3's `RedisHubLifetimeManager` serializes the message once and publishes it on the group's channel. 4. Every server subscribed to that channel, server 2 among them, receives it and writes it to its own members of the group. The commentator's server never needs to know where the fans are; Redis carries the message to the servers that asked for it. ## What a backplane does not do - **It does not move connections.** A Redis backplane forwards messages between servers, but each connection's state still lives in one process, so the load balancer still needs sticky sessions. - **It does not store messages.** Sends made while Redis is unreachable are lost and nothing replays them later. - **It adds a hop.** With Redis, a broadcast goes through Redis even for clients on the sending server, which is why the docs recommend running Redis in the same data center as the app. A single server process never needs a backplane, and local development does not either.

  • Does a background score feed that sends through IHubContext<CommentaryHub> avoid the problem?
    No. `IHubContext<THub>` uses the same `HubLifetimeManager<THub>` as the hub, so without a backplane it reaches only the connections of the server it runs on. Once a backplane is added, the opposite trap appears: if the feed runs on all six servers, each instance publishes the same update to everyone and fans see it six times, so make sure each update is produced once.
  • Why does Clients.Caller still work on a farm with no backplane?
    The caller is the connection whose message is being handled, so it is on the current server by definition. `Clients.Caller` sends to that connection id and the local lifetime manager finds it. The Redis manager even short-circuits sends to a single local connection id and writes to it directly instead of publishing to Redis.

Six radio studios each broadcast on their own short-range transmitter: an announcement read in studio 3 reaches only studio 3's listeners. A backplane is a shared feed line patched into every studio, so whatever one studio reads goes out on all six transmitters.

  • SignalR shares connections between servers automatically once a load balancer sits in front.
  • Clients.All throws when some of the hub's clients are connected to another server.
  • A Redis backplane removes the need for sticky sessions.
  • Only IHubContext needs a backplane; sends from inside a hub method reach everyone.
  • The backplane stores messages and replays them to clients that missed them.