Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel separate read/write operations for Redis?

Tags:

redis

laravel

I have a Redis cluster running inside Kubernetes. A Redis cluster can only have 1 master and N slaves. Is it possible to have multiple Redis connections inside Laravel for reading and writing? so I can write to the master and read from a slave. It's possible for 'normal' databases:

'mysql' => [
  'read' => [
     'host' => ['192.168.1.1'],
   ],
  'write' => [
     'host' => ['196.168.1.2'],
   ],
  '....'    => .....
]

Is there anyway to achieve this for Redis?

Thankyou,

like image 924
MevlütÖzdemir Avatar asked Mar 26 '18 14:03

MevlütÖzdemir


1 Answers

You can change which connection is used with the connection method which will return a Redis instance, e.g:

$redis = Redis::connection('read');
$redis->get('example');

You would configure the different connections in the same way you would for MySQL.

'redis' => [

    'client' => 'predis',

    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],

    'read' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6380),
        'database' => 0,
    ],

],
like image 56
sam Avatar answered Oct 14 '22 18:10

sam