skip to content

Your six-server SignalR commentary hub uses the Redis backplane, and Redis is unreachable for a minute mid-match. Which messages are lost, and what do callers see?

level: seniorimportance: should knowfreq 32%

answer

  1. no buffering, no replay
  2. Clients.All is published even for local fans
  3. a single local connection is written directly
  4. "An unexpected error occurred invoking"
  5. StackExchange.Redis reconnects and resubscribes

basics

~20 s

Sends that travel through Redis during the outage are lost: SignalR neither buffers nor replays them. Once the Redis client's few-second retry runs out the publish throws, so hub methods fail and IHubContext callers get exceptions; even local fans miss Clients.All.

solid answer

~50 s

`RedisHubLifetimeManager<THub>` turns `Clients.All`, `Others`, `Group`, `User` and sends to a connection on another server into a Redis publish, and every server, the sender included, delivers only what its subscription receives. StackExchange.Redis holds a publish in its backlog only for its command timeout (5 seconds by default); in a minute-long outage the publish then throws, so no fan gets the line, not even those on the sending server. A hub method that sends therefore fails: the server logs "Failed to invoke hub method" and the caller's `invoke` rejects with "An unexpected error occurred invoking '...' on the server." An `IHubContext` send from a score feed throws into the feed's code. Only `Clients.Caller` or `Clients.Client(id)` for a connection on the same server is written directly. Existing client connections stay open. When Redis returns, StackExchange.Redis reconnects and resubscribes, and nothing sent in between is replayed, so clients need a way to detect the gap and resync.

code

csharp · 19 lines
csharp
public sealed class ScoreFeed(IHubContext<CommentaryHub> hub, ILogger<ScoreFeed> logger)
{
    private long _sequence;

    public async Task PublishAsync(string matchId, string line, CancellationToken ct)
    {
        var seq = Interlocked.Increment(ref _sequence);
        try
        {
            await hub.Clients.Group(matchId).SendAsync("Commentary", seq, line, ct);
        }
        catch (Exception ex)
        {
            // Nothing is buffered: this line is gone for every fan.
            // Clients see the gap in seq and ask for the missing lines.
            logger.LogWarning(ex, "Backplane send {Sequence} for {Match} failed", seq, matchId);
        }
    }
}

go deeper

for a junior

Remember the rule from the docs: SignalR does not buffer messages while the Redis backplane is down, so anything sent during the outage is lost.

for a middle

Explain that the Redis lifetime manager publishes broadcasts, group and user sends to Redis, and that each server, including the sender, delivers only what its subscription receives.

for a senior

Describe what callers see, from failed invocations to exceptions in IHubContext feeds and new connections that may fail, and design sequence numbers plus a resync path instead of assuming delivery.

for a principal

Decide whether best-effort push is acceptable for the product and where authoritative state lives, so a backplane outage costs freshness rather than correctness.

## How a send travels through the Redis backplane With `AddStackExchangeRedis`, every hub send goes through `RedisHubLifetimeManager<THub>`. Most sends do **not** touch local connections directly: | Send | Path | |---|---| | `Clients.All`, `Others`, `AllExcept` | published on the hub's `:all` channel | | `Clients.Group(name)` | published on the group's channel | | `Clients.User(id)` | published on the user's channel | | `Clients.Client(id)`, connection on this server | written straight to the connection | | `Clients.Client(id)`, connection elsewhere | published on that connection's channel | | `Clients.Clients(ids)` | published once per connection id | Each server, **including the one that sent**, writes a broadcast to its own connections only when its Redis subscription receives it. A broadcast that never reaches Redis reaches nobody. ## What happens while Redis is unreachable The ASP.NET Core docs are explicit: SignalR does not buffer messages to send them when the server comes back up, and any messages sent while the Redis server is down are lost. In detail: 1. **Publishes fail.** Since 2.5, StackExchange.Redis queues commands issued while it is reconnecting, but only for its configured command timeout, 5 seconds by default. A blip shorter than that can pass unnoticed; in a minute-long outage the publish fails with an exception that surfaces from `SendAsync`. 2. **Hub methods fail.** A `PostLine` hub method that awaits `Clients.Group(...).SendAsync(...)` throws. The server logs "Failed to invoke hub method 'PostLine'.", and the commentator's `invoke` call rejects with "An unexpected error occurred invoking 'PostLine' on the server." unless detailed errors are enabled. 3. **`IHubContext` callers get exceptions.** A background score feed sees the exception in its own loop; unhandled, it can stop the feed. 4. **Existing client connections stay open.** They live on the app servers, and keep-alive pings between client and server do not involve Redis. 5. **New connections can fail.** Connecting a client subscribes channels for that connection and its user, which needs Redis. 6. **Cross-server group changes stall.** `AddToGroupAsync` for a connection on another server publishes a command and waits for that server's acknowledgement, giving up after about 30 seconds. ## When Redis comes back StackExchange.Redis reconnects and restores its subscriptions, and SignalR carries on; the docs say it reconnects automatically. Nothing is replayed. A goal that was posted during the outage is simply missing for every fan. ## Recognising it in the logs The backplane logs under the `RedisHubLifetimeManager` category, and an outage leaves a recognisable trail: - "Connection to Redis failed." (event `ConnectionFailed`) when the subscription connection drops; - "Failed to invoke hub method '...'." from the hub dispatcher for each hub method whose send threw; - "Connection to Redis restored." (event `ConnectionRestored`) when it comes back. The gap between the first and last line is the window in which every backplane send was lost. Alerting on the failure event turns a silent hole in the commentary into an incident someone sees. ## Designing for the gap Treat the backplane as a **best-effort notification channel** and keep the authoritative state somewhere else: - Put a **sequence number** in each pushed message so clients can notice a gap. - Give clients a **resync** path, such as a hub method or an HTTP endpoint that returns the lines after a given sequence number, and call it on a gap or after a reconnect. - **Catch and log** send failures in feeds that use `IHubContext`, so one outage does not kill the loop. - Hook `ConnectionFailed` and `ConnectionRestored` through `RedisOptions.ConnectionFactory` to alert on backplane outages. - Keep Redis in the **same data center** as the app and make it highly available, so outages are rare and short. Stateful reconnect does not cover this case. It replays messages a server had already written to a client's connection when the client's own link dropped; a line that never reached Redis was never written to any connection.

  • Why do fans connected to the very server that called Clients.All also miss the message?
    The Redis lifetime manager does not deliver broadcasts locally first. `SendAllAsync` only publishes to the hub's `:all` channel, and every server, the sender included, writes to its own connections when its subscription receives the message. If the publish fails, no server receives anything.
  • Does stateful reconnect recover commentary lines lost during the outage?
    No. Stateful reconnect replays messages that the server had already written to a client's connection when that client's own link dropped. A line whose publish to Redis failed never reached any connection, so there is nothing to replay; the app must detect the gap and resync.
  • What happens if Redis is down when the app starts?
    `RedisHubLifetimeManager` starts connecting in its constructor, and each new client connection waits for that connection. With `AbortOnConnectFail = false`, the default in `RedisOptions`, the multiplexer is created and keeps retrying in the background. The connection-string overloads replace those options with `ConfigurationOptions.Parse`, where StackExchange.Redis's own default applies, so add `abortConnect=false` to the string for the same behaviour.

  • SignalR keeps backplane messages until Redis is back, however long the outage lasts.
  • Clients.All still reaches fans on the sending server while Redis is down.
  • A Redis outage immediately disconnects every SignalR client connection.
  • IHubContext sends fail silently during an outage, so feeds need no error handling.
  • Stateful reconnect replays the messages that failed to reach Redis.