Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does StackExchange.Redis use multiple endpoints and connections?

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?

like image 377
Gigi Avatar asked Mar 25 '15 04:03

Gigi


People also ask

Is StackExchange Redis thread-safe?

For this, StackExchange. Redis offers ConnectionMultiplexer , a single thread-safe connection that StackExchange. Redis manages asynchronously.

What is Redis connection multiplexer?

Multiplexing allows for a form of implicit pipelining. Pipelining, in the Redis sense, meaning sending commands to the server without regard for the response being received. If you've ever been through a drive-through window and rattled off your entire order into the speaker, this is like pipelining.

What is StackExchange Redis?

StackExchange. Redis is a high performance general purpose redis client for . NET languages (C#, etc.). It is the logical successor to BookSleeve, and is the client developed-by (and used-by) Stack Exchange for busy sites like Stack Overflow.

What is Redis connection string?

Redis Connection Strings supports multiple URI-like formats, from a simple hostname or IP Address and port pair to a fully-qualified URI with multiple options specified on the QueryString.


1 Answers

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.

like image 172
Marc Gravell Avatar answered Sep 16 '22 13:09

Marc Gravell