18

As explained in the StackExchange.Redis Basics documentation, you can connect to multiple Redis servers, and StackExchange.Redis will automatically determine the master/slave setup. Quoting the relevant part:

A more complicated scenario might involve a master/slave setup; for this usage, simply specify all the desired nodes that make up that logical redis tier (it will automatically identify the master):

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("server1:6379,server2:6379");

I performed a test in which I triggered a failover, such that the master would go down for a bit, causing the old slave to become the new master, and the old master to become the new slave. I noticed that in spite of this change, StackExchange.Redis keeps sending commands to the old master, causing write operations to fail.

Questions on the above:

  1. How does StackExchange.Redis decide which endpoint to use?
  2. How should multiple endpoints (as in the above example) be used?

I also noticed that for each connect, StackExchange.Redis opens two physical connections, one of which is some sort of subscription. What is this used for exactly? Is it used by Sentinel instances?

Darren Lewis
  • 7,998
  • 1
  • 31
  • 55
Gigi
  • 24,295
  • 20
  • 85
  • 170

1 Answers1

5

What should happen there is that it uses a number of things (in particular the defined replication configuration) to determine which is the master, and direct traffic at the appropriate server (respecting the "server" parameter, which defaults to "prefer master", but which always sends write operations to a master).

If a "cannot write to a readonly slave" (I can't remember the exact text) error is received, it will try to re-establish the configuration, and should switch automatically to respect this. Unfortunately, redis does not broadcast configuration changes, so the library can't detect this ahead of time.

Note that if you use the library methods to change master, it can exploit pub/sub to detect that change immediately and automatically.

Re the second connection: that would be for pub/sub; it spins this up ahead of time, as by default it attempts to listen for the library-specific configuration broadcasts.

Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • 1
    Thanks for the response. I'm not sure I follow... what do you mean by library methods to change master / library-specific configuration broadcasts? Also, when I caused the master/slave failover, I actually waited for a few seconds and tried sending a SET again, several times, and it always kept sending it to the old master. – Gigi Mar 26 '15 at 07:15
  • 1
    @Gigi I will have to investigate the `SET` issue; on the other: `IServer.MakeMaster(...)` – Marc Gravell Mar 26 '15 at 09:27
  • @MarcGravell what is the 'server' parameter that you mentioned? I couldn't find it at ConfigurationOptions type. – Jean Lourenço Oct 21 '16 at 10:59
  • @JeanLourenço I mean the command-flags on individual operations – Marc Gravell Oct 21 '16 at 11:06