1

Context: I'm developing a system similar to Uber's, specifically, the server mediates the flow of data between two types of users (a driver and a client for example), both through their apps by connecting to the server's SignalR hub. Tracking their connection state to the server is mandatory and the process should be as instant as possible.

Their connection state is managed through a HashList<>() for each type of user (2 Lists). An entry is added to the list whenever a user is connected (OnConnected() is called), and that entry is removed from the list whenever the user gets disconnected (OnDisconnected() is called).

These HashLists are static variables, following the league of this StackOverflow answer.

While debugging locally on my machine, the HashLists gets modified instantly, but when published on a cloud service (in my case: Azure), the lists take time (about 10 ~ 15 seconds) until they are updated. However, acquiring a valid connection with the server just takes about 5 seconds.

Now, I know that connecting to the server can't be instant and it depends on several factors most of them are out of my reach, but detecting disconnections instantly (or as instant as it can get) would solve my problem, what should I do? Is there a different approach I should take?

PS: Detecting disconnections instantly can be done client-side, but this won't help in my case because I need the connected user data in the server.

bfahm
  • 188
  • 5
  • 14

1 Answers1

3

I believe you're deploying your own SignalR service. Only storing content in static variables is not enough, as there's no guarantee your requests will be forwarded to the same server. To solve this, you need to implement your own backplane pattern and persist the state somewhere else.(e.g. Azure Redis Cache)

More info:

https://learn.microsoft.com/en-us/aspnet/signalr/overview/performance/scaleout-in-signalr

https://learn.microsoft.com/en-us/aspnet/core/signalr/redis-backplane?view=aspnetcore-3.1

PS: I strongly recommend you use Azure SignalR Service rather than hosting your own.

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • I second this answer. Scale-out using a Redis backplane only goes so far before just start destroying the Redis instance. And unfortunately with Azure Redis, there is no way to even see the load that pubsub puts on a Redis instance, so you don't know things are going off of the rails until you start to see serious lags in SignalR performance. – Rob Reagan Apr 02 '20 at 17:34
  • Regarding Redis: I understand that this would help in case users get connected to different servers. But in my case (development phase, number of users max out at 2 or 3), assuming they all get connected to the same server, why do the variables take a long time to get updated? Is it the problem of the static variables or is the bottleneck from `OnDisconnected` function taking time to be called? Regarding SignalR Service, I will give it a try for sure, but again, the app is still in the development phase, I'll see if the free plan works out for us. – bfahm Apr 03 '20 at 13:32