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,
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,
],
],
Related
I want to connect redis cluster from Laravel but I am getting No connections available in the pool. My databaase.php looks like
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'clusters' => [
'default' =>
[
[
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
]],
'options' => [
'cluster' => 'redis', // This tells Redis Client lib to follow redirects (from cluster)
]
],
]
But with this settings, I am getting No connections available in the pool. But if I just connect redis cluster using cli, I can successfully connect. So I believe there is something wrong with my laravel configuration that I am still unable to solve. Any help?
Solution
Hi guys, it was a configuration issue. I was using wrong port. The default port is 6379 and I was mistakenly using another port number.
Moving the options to the parent array should solve your problem.
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => 'redis'
]
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
]
]
]
]
What about adding scheme and moving options ?
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => 'redis'
],
'clusters' => [
'default' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
]
]
]
]
Setup Laravel 8.x cache with AWS Elasticache Redis cluster:
i have tried many configure that i found. but none of them work.
i tried connecting Elasticache Redis cluster from redis-cli, it works with Ping pong.
But not working with laravel: (Error: No connections available in the pool)
And this is my config:
'redis' => [
'cluster' => true,
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_database_'),
// 'ssl' => ['verify_peer' => false], => have tried, it not working
/*
'parameters' => [ => have tried, it not working
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'timeout' => 15,
],
*/
],
'clusters' => [
'default' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 0),
'read_write_timeout' => 30,
],
],
'cache' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
]
],
'options' =>[
'cluster' =>'redis',
]
],
],
I have tried connecting Elasticache with Redis cli in Ec2, and it can connect:
And this is Elasticache Redis information:
This is my .env:
CACHE_DRIVER=redis
REDIS_HOST=xxxxx.xxxxx.clustercfg.xxxxx.cache.amazonaws.com
REDIS_PASSWORD=null
REDIS_PORT=6379
By the way, i connect with Configuration Endpoint (not Primary Endpoint)
and i have install "predis/predis" in composer.json.
I changed a lot of configurations that I found on the Google, but it doesn't work. Please help!!
Thanks for reading.
Solution:
Changed with using **phpredis ** it works:
Install php-redis in server:
sudo yum -y install php-redis
sudo systemctl restart php-fpm.service
Change config:
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
....
]
Not sure why it doesn't work with predis. Even though the configuration is the same.
I have a Redis 6.x instance on AWS Elasticache. It is clustered, and has in-transit encryption. I also have a RBAC set up (User with a password in AWS). I am able to connect using the redis-cli and then authorize using AUTH <password> and it works great. (It uses Redis 6 ACL feature)
However when I add the password to my Laravel configuration I get the error:
Couldn't map cluster keyspace using any provided seed
If I remove the password from the User in Elasticache, Laravel connects fine and uses Redis perfectly.
Here is my configuration. The password is correct in my env file and because of clustering, is added both in default connection and in the options['parameters'] key.
'redis' => [
'client' => 'phpredis',
'cluster' => true,
'clusters' => [
'default' => [
[
'scheme' => 'tls',
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
'options' => [
'cluster' => 'redis',
],
],
'options' => [
'parameters' => [
'scheme' => 'tls',
'password' => env('REDIS_PASSWORD', null),
],
'ssl' => ['verify_peer' => false],
'context' => [],
],
],
I've seen this error before but it seems like it occurs whenever something bad happens, rather than giving me specifics. For example, if I remove the password, and restrict some of the access permissions like SET or READ/WRITE commands, then I also get the same error, and so it is very hard to decipher what the error is.
Are you using Laravel 5 or 6?
Our setup is similar to yours, but this solution only seems to work with Laravel 6, but when I use this same configuration, I get the same issue as you in Laravel 5.
'redis' => [
'client' => 'phpredis',
'cluster' => true,
'options' => [
'cluster' => 'redis',
'ssl' => ['verify_peer' => false],
'context' => [],
'password' => env('REDIS_PASSWORD', null),
'parameters' => [
'scheme' => env('REDIS_SCHEME', 'tls'),
],
],
'clusters' => [
'default' =>
[
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
'persistent' => true,
],
],
'options' => [
'cluster' => 'redis',
]
],
],
Now to figure out how to make my Laravel 5 app work, (Or I'll just have to upgrade my Laravel Apps).
I have the error "No connections available in the pool" under laravel 7, php7.4 and redis server on AWZ elasticache.
Here my .env :
REDIS_CLIENT=predis
REDIS_SCHEME=tls
REDIS_HOST=clustercfg.xxx.xxx.xxx.cache.amazonaws.com
REDIS_PASSWORD=xxx
REDIS_PORT=6379
my config/database.php :
'redis' => [
'cluster' => true,
'client' => 'predis',
'options' => [
'cluster' => 'redis',
'parameters' => [
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'timeout' => 15,
],
],
'clusters' => [
'default' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'timeout' => 15,
]
],
'cache' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
]
],
],
],
clustercfg.xxx.xxx.xxx.cache.amazonaws.com is my configuration endpoint and REDIS_PASSWORD my AUTH token
I have basically copied the solution of Predis with laravel 5.5 "No connections available in the pool in Aggregate/RedisCluster.php:337 "
but problem is not resolved.
I have looked at the vpc security group in amazon and basically all ports are opened :
This question already has answers here:
Using Redis for Queues for Multiple Laravel Applications on a Single Server
(4 answers)
Closed 4 years ago.
I have two project on same server (Ubuntu 16.04) with different database, username and also different user_password.
But if A project's queue failed, it may insert into B project's failed-job table.
Yes, it means sometimes it insert the failed record into the right place.
I checked the Laravel config all with default setting.
I use the supervisor keep walker.
So, anybody has the same problem and a solution for that?
I open an issue on github here. https://github.com/laravel/framework/issues/14403
By author's reply. I'v solved the problem by this two steps:
1.change the config/cache.php
'prefix' => 'myProjectName', //the default value is laravel.
2.change the config/database.php
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 2, //the default is 0
],
],
You should also pay attention to config/queue.php, if your queue may have more than 60 seconds to finish the job.
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'default',
'expire' => 120, //the default is 60, and would be your like.
],
If you want to resolve the conflict by specifying a prefix for the redis connection rather than by using different databases you can do so with the following config.
For predis (the default client)
'redis' => [
'client' => 'predis',
'cluster' => false,
'options'=>[
'prefix' => 'YOUR_PREFIX_HERE'
],
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
For phpredis client (available since Laravel 5.3):
'redis' => [
'cluster' => false,
'client' => 'phpredis',
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'prefix' => 'YOUR_PREFIX_HERE:',
],
],