Why does ASP.NET Core SignalR need sticky sessions on a server farm, and in which cases can you run without them?
answer
- connection state lives in one process
- connectionToken sent back as ?id=
- 404 "No Connection with that ID"
- a Redis backplane does not remove it
- three documented exceptions
basics
~20 sA SignalR connection lives only in the process that created it, so all its later requests must reach that process. Sticky sessions are required, Redis backplane included, except on one process, with Azure SignalR Service, or when every client uses WebSockets only and skips negotiation.
solid answer
~50 sA client normally starts with `POST /hubs/commentary/negotiate`; the server creates the connection in its in-memory connection manager and returns a `connectionToken`, which the client then sends as `?id=` on every transport request. WebSockets use one long-lived request after that, but Server-Sent Events add a `POST` for every client-to-server message and long polling is a stream of separate polls and posts. If any of them reaches another server, it answers `404` with "No Connection with that ID" and the connection fails. A Redis backplane forwards messages between servers; it does not share connection state, so it still needs affinity. The docs name three exceptions: a single server process; Azure SignalR Service, where the service rather than your app is sticky; and clients restricted to WebSockets that also skip negotiation, because the whole connection is then one request that the receiving server creates on arrival.
code
http · 15 linesPOST /hubs/commentary/negotiate?negotiateVersion=1 HTTP/1.1
Host: live.example.com
HTTP/1.1 200 OK
Content-Type: application/json
{"negotiateVersion":1,"connectionId":"x1Yq0b","connectionToken":"Hd9kQ2","availableTransports":[]}
GET /hubs/commentary?id=Hd9kQ2 HTTP/1.1
Host: live.example.com
HTTP/1.1 404 Not Found
Content-Type: text/plain
No Connection with that IDgo deeper
Recall that a SignalR connection belongs to the server that created it, so the load balancer must keep sending that client back to the same server.
Walk through negotiate, the connection token sent as ?id=, and the extra requests of Server-Sent Events and long polling, and explain the 404 'No Connection with that ID' a misrouted request gets.
Name the three exceptions precisely, explain why a Redis backplane is not one of them, and say how you would spot missing affinity in access logs.
Treat affinity as a deployment contract: it shapes how servers are drained and rolled, and it is one of the costs that separates a self-hosted backplane from a managed service.
## Where a SignalR connection lives Every SignalR connection is an `HttpConnectionContext` held in the memory of the server process that created it, in that process's connection manager, together with the hub's per-connection state. Nothing about it is written to a shared store, and adding a Redis backplane does not change that. ## The requests one connection makes A browser client normally goes through two phases: 1. **Negotiate.** `POST /hubs/commentary/negotiate` creates the connection on whichever server receives it and returns a `connectionId`, a `connectionToken` and the transports on offer. 2. **Transport.** The client then connects with `?id=<connectionToken>` in the query string. How many requests follow depends on the transport: | Transport | Requests after negotiate | |---|---| | WebSockets | one upgrade request that stays open | | Server-Sent Events | one long `GET` for server-to-client messages, plus a `POST` per client-to-server message | | Long polling | a new `GET` for each poll, plus a `POST` per client-to-server message | Each of those requests looks the token up in the local connection manager. On a server that did not create the connection the lookup fails, and the dispatcher answers **404 Not Found** with the plain-text body "No Connection with that ID". The client's transport fails and the connection closes. Even WebSockets need affinity after a negotiate: the negotiate `POST` and the upgrade are two separate requests, and a load balancer is free to send them to different servers. ## Sticky sessions **Sticky sessions** (also called session affinity or client affinity) make the load balancer send every request from one client to the same server, usually with an affinity cookie or a hash of the client address. Common switches: - Azure App Service: the **Session affinity** (ARR affinity) setting, which the ASP.NET Core docs say to leave on for SignalR. - IIS Application Request Routing: client affinity. - Nginx: `ip_hash` in the upstream block. For a six-server sports-commentary hub, turning affinity on is part of the deployment, not an optimisation. ## Why the Redis backplane does not remove it `AddStackExchangeRedis` makes **messages** reach every server: a `Clients.Group` send is published to Redis and each server writes it to its own members. It does not make **connections** portable; a token created on server 2 still means nothing to server 4. The docs state plainly that the Redis backplane requires sticky sessions unless the WebSockets-only exception below applies. ## The three exceptions 1. **One server process.** There is nowhere else for a request to go. 2. **Azure SignalR Service.** After negotiate the client is redirected to the service and keeps its connection there. The service routes traffic to app servers over its own connections, so the app's load balancer needs no affinity. 3. **WebSockets only, negotiation skipped.** When every client is restricted to WebSockets and skips negotiate, the connection is a single upgrade request with no `id`. Whichever server receives it creates the connection on the spot, and nothing ever has to come back to it. One client still on long polling, or one that negotiates first, brings the requirement back. ## Spotting missing affinity Typical signs: - connections that work with one instance and fail at random with two or more; - 404 responses on `/hubs/...?id=` requests in the access logs; - long-polling clients that connect and drop within seconds. The fix is load-balancer configuration, not hub code. ## Affinity and deployments Affinity pins a client to one server for the life of its connection, which has consequences when servers come and go: - Taking a server out of rotation ends its connections; clients that reconnect negotiate again and land on another server with a **new connection id**. - Anything tied to the old connection id, such as group membership, has to be re-established after that reconnect. - Adding a server does not rebalance fans already connected; only new connections land on it, so scaling out at half-time relieves the busy servers slowly. So a rolling deployment of the commentary hub drains servers gradually and expects fans to reconnect, rather than assuming connections survive the move.
- Why can a client restricted to WebSockets still fail without affinity if it does not skip negotiation?Negotiate and the WebSocket upgrade are two separate HTTP requests. Negotiate creates the connection on the server that receives it, and the upgrade carries that connection's token in `?id=`. If the load balancer sends the upgrade elsewhere, the lookup fails with 404 and the connection never opens. Skipping negotiation removes the first request, so the upgrade creates the connection wherever it lands.
- What does Azure App Service need for a SignalR app that does not use Azure SignalR Service?The ASP.NET Core docs list two settings: Web sockets turned on, so clients are not pushed down to slower transports, and Session affinity (ARR affinity) left on, so each client's requests return to the same instance. With Azure SignalR Service neither applies, because clients connect to the service rather than to the app.
- With a Redis backplane, any server can serve any request of a SignalR connection.
- Sticky sessions are only needed for long polling, never when WebSockets are available.
- AddStackExchangeRedis stores SignalR connection state in Redis so servers can share it.
- A 404 on a hub request carrying ?id= always means the MapHub path is wrong.
- Azure SignalR Service still needs ARR affinity on the app servers.