title: How to have both clustered and non-clustered redis connections in laravel
body: <h1>Background</h1>
  
  <p>In the past I was able to use a non-clustered redis just fine in my config like so:</p>
  
  <pre><code>'redis' =&gt; [
  
      'default' =&gt; [
          'host' =&gt; env('REDIS_HOST', '127.0.0.1'),
          'password' =&gt; env('REDIS_PASSWORD', null),
          'port'     =&gt; 6379,
          'database' =&gt; 0,
          'cluster' =&gt; true,
      ]
  ],
  </code></pre>
  
  <p>However due to the load on our redis servers, I have to cluster my redis, This config works fine when the <em>only</em> redis connection I have is clustered (figured it out after a lot of <a href="https://stackoverflow.com/a/56510512/766570">work</a>):</p>
  
  <pre><code>'redis' =&gt; [
      'client' =&gt; 'predis',
      'cluster' =&gt; true,
      'options' =&gt; [
          'cluster' =&gt; 'redis',
          'parameters' =&gt; [
              'host' =&gt; env('REDIS_SHARD_1_HOST', '127.0.01'),
              'password' =&gt; env('REDIS_PASSWORD', null),
              'port' =&gt; env('REDIS_DEFAULT_PORT', 6379),
              'database' =&gt; 0,
              ],
          ],
      'clusters' =&gt; [
          'default' =&gt; [
              'host' =&gt; env('REDIS_SHARD_1_HOST', '127.0.01'),
              'password' =&gt; env('REDIS_PASSWORD', null),
              'port' =&gt; env('REDIS_SHARD_1_PORT', 6379),
              'database' =&gt; 0,
          ],
          'shard2' =&gt; [
              'host' =&gt; env('REDIS_SHARD_2_HOST', '127.0.01'),
              'password' =&gt; env('REDIS_PASSWORD', null),
              'port' =&gt; env('REDIS_SHARD_2_PORT', 6379),
              'database' =&gt; 0,
          ],
          'shard3' =&gt; [
              'host' =&gt; env('REDIS_SHARD_3_HOST', '127.0.01'),
              'password' =&gt; env('REDIS_PASSWORD', null),
              'port' =&gt; env('REDIS_SHARD_3_PORT', 6379),
              'database' =&gt; 0,
          ],
          'options' =&gt; [
              'cluster' =&gt; 'redis'
          ],
      ]
  ]
  </code></pre>
  
  <p>any my env file looks like this (For my localhost, anyway):</p>
  
  <pre><code>QUEUE_DRIVER=redis      // cluster compatible
  BROADCAST_DRIVER=redis  // cluster compatible
  CACHE_CONNECTION=redis  // cluster incompatible 
  
  REDIS_CLUSTER=true
  REDIS_HOST=localhost
  
  REDIS_DEFAULT_PORT=7000
  
  REDIS_SHARD_1_HOST=localhost
  REDIS_SHARD_2_HOST=localhost
  REDIS_SHARD_3_HOST=localhost
  
  REDIS_SHARD_1_PORT=7000
  REDIS_SHARD_2_PORT=7001
  REDIS_SHARD_3_PORT=7002
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>The fact is that currently, we use the non-clustered redis for the following:</p>
  
  <ul>
  <li>Cache: supports redis clustering</li>
  <li>Queue/Jobs: supports redis clustering</li>
  <li>Broadcast (ie websockets): does <em>not</em> support redis clustering</li>
  </ul>
  
  <p>That's why we need to have both redis connections simultaneously, so that we can use the clustered connection for caching/queues, and non-clustered connection for websockets.</p>
  
  <p>But this isn't working:</p>
  
  <pre><code>'redis' =&gt; [
      'default' =&gt; [
          'host' =&gt; env('REDIS_HOST', '127.0.0.1'),
          'password' =&gt; env('REDIS_PASSWORD', null),
          'port'     =&gt; 6379,
          'database' =&gt; 0,
          'cluster' =&gt; true,
      ],
      'clustered' =&gt; [
          'client' =&gt; 'predis',
          'cluster' =&gt; true,
          'options' =&gt; [
              'cluster' =&gt; 'redis',
              'parameters' =&gt; [
                  'host' =&gt; env('REDIS_SHARD_1_HOST', '127.0.01'),
                  'password' =&gt; env('REDIS_PASSWORD', null),
                  'port' =&gt; env('REDIS_DEFAULT_PORT', 6379),
                  'database' =&gt; 0,
                  ],
              ],
          'clusters' =&gt; [
              'default' =&gt; [
                  'host' =&gt; env('REDIS_SHARD_1_HOST', '127.0.01'),
                  'password' =&gt; env('REDIS_PASSWORD', null),
                  'port' =&gt; env('REDIS_SHARD_1_PORT', 6379),
                  'database' =&gt; 0,
              ],
              'shard2' =&gt; [
                  'host' =&gt; env('REDIS_SHARD_2_HOST', '127.0.01'),
                  'password' =&gt; env('REDIS_PASSWORD', null),
                  'port' =&gt; env('REDIS_SHARD_2_PORT', 6379),
                  'database' =&gt; 0,
              ],
              'shard3' =&gt; [
                  'host' =&gt; env('REDIS_SHARD_3_HOST', '127.0.01'),
                  'password' =&gt; env('REDIS_PASSWORD', null),
                  'port' =&gt; env('REDIS_SHARD_3_PORT', 6379),
                  'database' =&gt; 0,
              ],
              'options' =&gt; [
                  'cluster' =&gt; 'redis'
              ],
          ]
  </code></pre>
  
  <p>further, some users <a href="https://github.com/nrk/predis/issues/480#issuecomment-399978155" rel="nofollow noreferrer">state</a> that such a task is simply impossible for redis. Is that true?</p>
  
  <h1>update</h1>
  
  <p>I tried this</p>
  
  <p>in <strong>Cache.php</strong></p>
  
  <pre><code>    'redis' =&gt; [
          'driver' =&gt; 'redis',
          'connection' =&gt; 'clustered',
      ],
  </code></pre>
  
  <p>note: in the above connection i simply couldn't just copy/paste the cluster options, since it would crash if i didn't put a driver option</p>
  
  <p>In <strong>database.php</strong> I was inspired by this <a href="https://stackoverflow.com/a/45354052/766570">answer</a> and simply put different connections under the redis key: (ie `database.redis.connection-1, database.redis.connection-2 etc)</p>
  
  <pre><code>'redis' =&gt; [
      'clustered' =&gt; [
           // clustered settings copied from above
          ],
      ], 
  
      'default' =&gt; [
           // non clustered settings
      ],
  ]
  </code></pre>
  
  <p>To test, I ran the following tinker</p>
  
  <pre><code>&gt;&gt;&gt; use Illuminate\Support\Facades\Cache;
  &gt;&gt;&gt; Cache::put('foo','bar',1);
  Predis/Response/ServerException with message 'MOVED 7837 127.0.0.1:7001'
  </code></pre>
  
  <p>The move error is a known one, it's simply <a href="https://stackoverflow.com/a/53000256/766570">saying</a> that i'm dealing with a non-clustered redis connection.</p>
  
  <p>thoughts?</p>