Monday, 8 July 2013

Notes from Build 2013 Scaling the real time web with ASP.NET SignalR

ASP.NET and SignalR have some improvements. For example SignalR is now being used in the Visual Studio development experience making it possible for edits to be propagated across browsers without referh. Here are some notes from Damian Edwards presentation :
3-502 Scaling the real time web with ASP.NET SignalR
See for getting started
·    Scaling real-time traffic shares many considerations with traditional web traffic eg CPU, bandwidth, memory
·    Application scenarios have huge impact on scaling patterns.
·    Big difference is in concurrency, supporting many long running idle and active connections vs short requests
·    Different SignalR transports have different overheads
General things to watch out for:
Blocking calls eg block I/O
·    Never ever block a Hub method, it jams up pipes
·    Use 4.5 async where possible
Sending large messages
·    Memory leaks caused by misunderstanding SignalR object lifetime eg Hub instances
·    Session - don't use it from SignalR. Instead use Hub state, cookies, browser storage, database etc. instead
Remember the secret of scale "Have your app do as little as possible. If you do nothing, you can scale infinitely" - Scott Hanselman
SignalR core architecture: Pub/Sub
1. Publisher
Message serialized and saved to cache associated with Signal, topic is marked for delivery
2. Message Cache
3. Worker
Worker is scheduled for signal, selects a waiting subscriber, retrieves message from cache
Worker sends message to client as bytes over transport
4. Client
Pattern 1 Sever broadcast
Low rate, message to all clients
Low rate broadcast of the same payload to clients
One message buss sends maps to many users (fan out)
More clients don't increase bus traffic
eg application wide alerts
Pattern 2 Server Push
Low rate, message to unique clients)
Low rate broadcast of the unique payload to each client
One message bus sends maps to one user (no fan out)
More clients means more message bus traffic
eg Job monitor
Pattern 3 User event driven
Broadcast on client action
Broadcast on client actions
One message bus send maps too many users (fan out)
More clients means more message traffic
eg Chat
Pattern 4 High frequency real-time
Fixed high rate, unique message
Fixed high rate broadcast from servers and clients (don't go above 25Hz
One message bus sends maps to one user (no fan out)
More clients means more message traffic
eg Gaming
There are command line utilities for Signal R in Microsoft.AspNt.SignalR.Utils. These include things like
signalR [args]
·    ipc Installs SignalR performance counters
·    upc Uninstalls SignalR performance counters
·    ghp Generates HubProxy JavaScript files for server Hub classes
To generate load use crank.exe /url:http://localhost:29573/TestConnection /Clients:100 /BatchSize:200
For Isolated scenarios in-proc use Stress (Microsoft.AspNet.SignalR.Stress\bin\Debug)
Adjust IIS Settings for concurrency limits
There was something said about Max concurrent requests per CPU
appcmd.exe set config /section:system.webserver/serverRuntime /appConcurrentRequestLimit:100000
Scale-out Issues
·    Client transience: How many messages one server gets to the other servers in my web farm
·    Client transience: When does a client disconnect from my App
·    Client transience: How do I avoid duplicate and missed messages as I move from one server to another
·    Client distribution: What happens if one function on one server is called many more times than others
To solve these issues use SQL Server, Redis & Windows Azure Service Bus (from NuGet). This works well for Server Broadcast pattern but limited for others because every message goes to every server, therefor as traffic increases you are limited by how fast any web server can pull messages off the backplane.
Back planes are much slower than single server
public static void Start()
GlobalHost.DependencyResolver.UseRedis("localhost", 6379, "", "build");
Custom scale-out
·    Common Server
·    Specific Server
·    Filtered message bus
·    Server transition
·    Hybrid
2.0 Scale out improvements
·    Support for pre serialized messages
·    Support for single serialization when sending multiple messages