I use redis replication(one master with port 6379, two slave with port 6380, 6381), I've not found anything in the laravel doc for config redis replication.
I use below config(from gpt's answer), but the replication config not works. When I use Redis::get('key')
, every redis connection is on the master instance.
Is it possible to config redis replication like the mysql database read write configuration. Laravele will select the master instance for write, and load balance read among all the slaves? What's more laravel can stick to the same instance like mysql database, The sticky option is an optional value that can be used to allow the immediate reading of records that have been written to the database during the current request cycle.
// config/database.php
'redis' => [
'client' => 'predis',
'options' => [
'cluster' => 'redis',
'replication' => true,
],
'default' => [
'host' => '127.0.0.1',
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
],
'replicas' => [
[
'host' => '127.0.0.1',
'password' => env('REDIS_PASSWORD', null),
'port' => 6380,
'database' => 0,
],
[
'host' => '127.0.0.1',
'password' => env('REDIS_PASSWORD', null),
'port' => 6381,
'database' => 0,
],
],
]
master redis connection log
[10280] 28 May 15:19:11.426 - Accepted 127.0.0.1:55759
[10280] 28 May 15:19:11.472 - Client closed connection
[10280] 28 May 15:19:13.721 - Accepted 127.0.0.1:55768
[10280] 28 May 15:19:13.754 - Client closed connection
Log::channel('channel-name')->info
then with redis:Redis::connection('replica1')
. Though instead of replica being an array of connections, separate them by replica 1 and 2 – BeckaRedis::connection('replica1')->get('key')
,Redis::connection('replica2')->get('key')
for your read whileRedis::get('key')
for write/master – BeckaRedis::set()
,Redis::get()
– Franfranc