0

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

8
  • Did you check redis connection? laravel.com/docs/11.x/redis#using-multiple-redis-connections Commented May 28, 2024 at 8:19
  • Have not personally tried this but it should be the same logic when using Laravel's log channel 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 Commented May 28, 2024 at 8:22
  • In your case maybe Redis::connection('replica1')->get('key'), Redis::connection('replica2')->get('key') for your read while Redis::get('key') for write/master Commented May 28, 2024 at 8:24
  • Yes, when specific the connection it works. I mean is is possible to make laravel auto select the connection. Just like Redis::set(), Redis::get() Commented May 28, 2024 at 8:26
  • 1
    Does this answer your question? Laravel separate read/write operations for Redis? Commented May 28, 2024 at 8:37

1 Answer 1

0

Without any progress by searching or read the laravel doc.

Then I read the source code and debug the redis connection initialization process. I found below code works well for separate read and write operation, read load balancing, and write read sticky.

    'redis' => [
        'client' => 'predis',
        'options' => [
            'cluster' => 'redis',
            'replication' => true,
        ],
        'default' => [
            'tcp://127.0.0.1:6379?alias=master',// this is the master role
            'tcp://127.0.0.1:6379',// I also make the master instance to process the read action, you also can remove it to make the master only respond to the write action.
            'tcp://127.0.0.1:6380'
        ]
    ],

Then in your code you can use it like below:

// There is a little flaw, without connection() it cannot auto complete.
// But this has no relation to the redis config. Maybe it need to resolve to laravel-ide-helper.
Redis::get('test');  // this will select any one of the slave instance
Redis::connection()->set('test',2);  // this will use the master
Redis::connection()->get('test');  // for sticky this will also use the master.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.