Why are you doing that? Local and Production redis data will, of course, will be different than the production ones
Single Redis configuration for both local and production (cluster) environments
How can I configure my Redis configuration in config/database.php to use my local single Redis instance in my local environment and my Redis cluster in production? I feel like I'm missing something obvious.
This configuration works locally:
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
...
And this is the configuration that works with my production cluster:
'redis' => [
'client' => 'predis',
'options' => [
'cluster' => 'redis',
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
],
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
...
I want to combine the two (with the help of environment variables?) so my production deployment uses the cluster config and my local dev environment uses my local config/redis instance.
Not sure it's the best (right) way of achieving this, but I ended up placing the following logic at the top of my database.php file:
<?php
if (env('APP_ENV') == 'production') {
$redis = [
'client' => 'predis',
'options' => [
'cluster' => 'redis',
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
],
];
}
else {
$redis = [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
];
}
return [
....
And using the follwoing for my redis key:
'redis' => $redis
So my production environment uses the Redis cluster configuration and my local uses the local config.
Please or to participate in this conversation.