Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - Switch between different Redis backplanes

Let's assume we have 2 Redis Server Backplanes, one as Master and the other as Slave.

Each web application is using SignalR in order to push content to the connected clients as it happens and in order to connect them to the backplane I am using in Application_Start

GlobalHost.DependencyResolver.UseRedis(host, port, "", new[] {"signalr.key"});
RouteTable.Routes.MapHubs();

Now in case Master Redis Backplane fails, I would like to promote the Slave Redis server to Master and switch all existing connections from web servers to the new Master Redis Server.

In order to promote the Slave Server to Master I am using the following code

using (var conn = new RedisConnection(host, port, allowAdmin: true))
{
    if (conn.ServerType != ServerType.Master)
    {
        conn.Open();
        var makeMaster = conn.Server.MakeMaster();
        var info = conn.Wait(conn.GetInfo());
        conn.Wait(makeMaster);
    }
}

that seems to do the work.

Can you please help me on how I can inform my web application that the backplane has changed how to connect to the new one, in order to sustain communication between my connected clients?

like image 323
ppolyzos Avatar asked Feb 25 '13 13:02

ppolyzos


1 Answers

We don't use SignalR specifically, but we have something pretty similar in the way we use redis, especially when switching between nodes. Specifically, we use redis pub/sub to subscribe to a channel, and we broadcast to that channel when changing master.

Our configuration is a little different, because we use the delimited configuration version based around ConnectionUtils.Connect(...). This means we can specify multiple nodes, with ConnectionUtils handling the concerns of figuring out which is the current master. But in your case you could perhaps publish the new master information as part of the pub/sub. I should also note that much of the code to handle switching masters (with notification) is wrapped up behind ConnectionUtils.SwitchMaster. This includes a broadcast of the change, which you can subscribe to via ConnectionUtils.SubscribeToMasterSwitch. As a minor implementation detail, the channel it uses for this is "__Booksleeve_MasterChanged" - but that is opaque if you just use the public methods.

like image 73
Marc Gravell Avatar answered Sep 22 '22 11:09

Marc Gravell