<p>I am learning StackExchange.Redis, and Kubernetes, so I made a simple .net core app that reads a key/value from a Redis master+2slaves deployed on kubernetes. (so, everything, Redis and my app, run inside containers)</p>

<p>To connect to redis I use the syntax suggested in the doc: </p>

<pre><code>ConnectionMultiplexer.Connect("server1:6379,server2:6379,server3:6379");
</code></pre>

<p>However, if I monitor the 3 containers with redis-cli MONITOR, the requests are processed always from the master, the 2 slaves do nothing, so there is no load balancing. </p>

<p>I have tried also to connect to a Kubernetes load balancer service which exposes the 3 Redis containers endpoints, the result is that when I start the .net app the request is processed randomly by one of the 3 Redis nodes, but then always on the same node. I have to restart the .net app and it will query another node but subsequent queries go always on that node.</p>

<p>What is the proper way to read key/values in a load balanced way using StackExchange.Redis with a master/slave Redis setup?</p>

<p>Thank you</p>

<p>SE.Redis has a <code>CommandFlags</code> parameter that is optional on every command. There are some useful and relevant options here:</p>
<ul>
<li><code>DemandPrimary</code></li>
<li><code>PreferPrimary</code></li>
<li><code>DemandReplica</code></li>
<li><code>PreferReplica</code></li>
</ul>
<p>The default behaviour is <code>PreferPrimary</code>; write operations bump that to <code>DemandPrimary</code>, and there are a <em>very few</em> commands that actively prefer replicas (keyspace iteration, etc).</p>
<p>So: if you aren't specifying <code>CommandFlags</code>, then right now you're probably using the default: <code>PreferPrimary</code>. Assuming a primary exists and is reachable, then: it will use the primary. And there can only be one primary, so: it'll use one server.</p>
<p>A cheap option for today would be to add <code>PreferReplica</code> as a <code>CommandFlags</code> option on your high-volume read operations. This will push the work to the replicas if they can be resolved - or if no replicas can be found: the primary. Since there can be multiple replicas, it applies a basic rotation-based load-balancing scheme, and you should start to see load on multiple replicas.</p>
<p>If you want to spread load over all nodes including primaries and replicas... then I'll need to add new code for that. So if you want that, please log it as an issue on the github repo.</p>
