Elasticache PHP Redis AWS Moved Exception in Laravel - php

I am using laravel 5.2 and having an architecture of multi-server autoscaling. I want to have session and cache in a centralized location. I want to use AWS elasticache for the same.
I have setup an elasticache Redis cluster having following options :
Parameter group : default.redis.3.2.cluster.on
Shards : 3
Total Nodes : 9
In laravel session configuration I have set redis as the session driver.
In database.php where redis configuration is set, i have used following :
REDIS_HOST=my_aws_elasticache_configuration_endpoint
REDIS_PASSWORD=null
REDIS_PORT=6379
.
When I try to use it, I get following error :
MOVED 13841 some_ip_address_of_aws:6379
I tried using local redis and it worked, so predis is working properly. I tried to check the solutions online, but not able to get the solution. I believe that the configuration endpoint is trying to redirect the redis connection to an available node url out of the 9 nodes I have. However, I have anticipated that AWS should do it internally and not throw exception. Can anyone help me?

I finally got an answer, we need to get the cluster mode on in database settings and set new cluster option to redis. This is how the redis config in database.php looks like :
'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,
],
],
],
],
If you have password then you can replace options array with this :
'options' => [
'cluster' => 'redis',
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
],

Related

Yii2 memcached not working but php simple connection working

Trying to use Yii2 memcached but it's not working. Here is a connection part in config/web.php file.
When trying to check cache in controller it's not working instead working simple php memcached connection so all required settings are correct.
Result is
Is anyone faced this problem and what can be a reason of this issue ?
My port was wrong, should be 11211
'cache' => [
'class' => 'yii\caching\MemCache',
'useMemcached' => true,
'servers' => [
[
'host' => 'memcached',
'port' => 11212,
'weight'=>60
]
],
],

Connecting to remote REDIS server in laravel

I have made an instagram contender that needs to be able to process video conversions(FFMPEG) on a different server because of CPU reasons.
Both of my servers are connected by the same database.
Currently i have a local redis server working so this way i can push tasks to the background using the default redis config/queue config/database.
All of it is working so far, now i want to go to the next step and set-up a bridge to pass off the intensive tasks to a dedicated server just for doing video processing tasks.
To sum up what i want
1.Dispatch job from SERVER1 and send it to SERVER2 REDIS QUEUE to be processed
SERVER1(default) connection (config/queue) for redis
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],
SERVER1(default) queue (config/database)
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
What i have attempted
SERVER2 Connection
'videoProcessingRedis' => [
'driver' => 'redis',
'connection' => 'videoRedisQueue',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],
SERVER 2 Queue
'videoRedisQueue' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', 'https://website.ca/'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
Jobs/ConvertVideoForHLS
public function __construct(video $video)
{
$this->onConnection('videoProcessingRedis');
$this->onQueue('videoRedisQueue');
$this->video = $video;
}
With the above attempt set i do not get any errors, however i can not view the job or even see that it has been created. even using the php artisan queue:listen and php artisan queue:listen videoProcessingRedis i can not see anything, nothing in the failed_jobs table and nothing in my supervisor.log so im assuming its got something to do with it just bouncing off the host?
Idea 1
Set SERVER2 redis server to be public facing
1.set the bind option on the /etc/redis/redis.conf file
before
bind 127.0.0.1
after
bind 0.0.0.0
after bind has been set(allowing all access) i would add a port restriction on 6379 with only the SERVER1 IP to be able to connect
When working with multiple servers on a Redis queue you need to make sure that the APP_NAME variable of both environments have the same value. If those are different then the worker will not see the jobs.
To check if your connection is working you could run redis-cli -h {ip-of-server-2} from server 1.

Configuring Redis as Cache interface in a Laravel 5 Application

I have a Laravel application that I am integrating a Redis Server into to handle the application caching. However I am having issues with the Redis configuration, as I am not able to access the Redis interface using the Laravel cache facade.
I set up a redis-server and comfirmed that it is working on the server:
> redis-cli
127.0.0.1:6379 > ping
PONG
I then followed the integration documentation for Redis/Laravel from here:
https://laravel.com/docs/5.7/redis
I installed the composer predis/predis package...
I set it up in Laravel to use the default redis config:
app/config/cache.php:
'default' => env('CACHE_DRIVER', 'redis'),
app/config/database.php:
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
]
.env:
BROADCAST_DRIVER=log
CACHE_DRIVER=redis
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
Now when I test I can access Redis via the Redis Facade...
use Illuminate\Support\Facades\Redis;
...but not from the Cache Facade.
use Illuminate\Support\Facades\Cache;
// ************* this works
Redis::set('testFromRedisSet', 'RedisSet');
// ************* none of these work...
Cache::store('redis')->put('testFromStoreCachePut', 'CacheStorePut', 600);
Cache::put('testFromCachePut', 'CachePut', 600);
Cache::remember('testFromCacheRemember', 60, function() {
return "CacheRemember";
});
Cache::rememberForever('testFromCacheRememberForever', function() {
return "CacheRememberForever";
});
127.0.0.1:6379> KEYS '*'
1) "testFromRedisSet"
127.0.0.1:6379>
Interestingly enough the Cache Facade still appears to be functioning properly. If I monitor the cache requests in Telescope, the 4 cache requests in the example that are not showing up in the Redis Server are still being cached somewhere, and using the Cache facade to retrieve these 4 values works fine.
...all these retrieve the correct values:
Cache::get('testFromCacheStoreRedisPut');
Cache::get('testFromCachePut');
Cache::get('testFromCacheRemember');
Cache::get('testFromCacheRememberForever');
So what is going on here?
It looks like Laravel is utilizing the [redis] cache driver correctly, because when I shut down the Redis Server and retest the app the entire applications throws a Predis Connection Exception:
In AbstractConnection.php line 155: Connection refused [tcp://127.0.0.1:6379]
Where are the cache keys being stored and why can't I view the stored keys in the terminal using "redis-cli"?
redis-cli KEYS '*'
Monitoring the Redis Server shows me that Redis is storing the data and pushing event notifications, but all data that I can't see in the termimal is prefixed with laravel_cache. However there is no data or hash key with that name present in the interface.
1561596707.950397 [1 127.0.0.1:42058] "SETEX" "laravel_cache:testFromCacheStoreRedisPut" "36000" "s:18:\"CacheStoreRedisPut\";"
1561596707.950898 [1 127.0.0.1:42058] "SETEX" "laravel_cache:testFromCachePut" "36000" "s:8:\"CachePut\";"
1561596707.951521 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheRemember"
1561596707.952110 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheRememberForever"
1561596707.952718 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheStoreRedisPut"
1561596707.953236 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCachePut"
1561596707.953745 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheRemember"
1561596707.954191 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheRememberForever"
1561596709.251036 [0 127.0.0.1:42064] "SELECT" "1"
1561596709.251200 [1 127.0.0.1:42064] "GET" "laravel_cache:telescope:dump-watcher"
1561596709.263678 [1 127.0.0.1:42064] "GET" "laravel_cache:telescope:pause-recording"
How can i accesss the data being stored in the laravel_cache namespace? Even running the same GET commands shown in the server monitor does not fetch any data directly.
Redis cache store uses the cache database connection by default. If you look at config/database.php it switches to database 1 for caching by default. To view the keys in the terminal, you have to select which database to use first.
127.0.0.1:6379> SELECT 1
127.0.0.1:6379[1]> KEYS *
Using different databases allows you to separate your cache and session store, it can be handy when you need to delete all cache keys (flushdb) but keep the user session. You can even add a separate connection for queues. Note that it's not recommended to use multiple databases, use multiple Redis instance instead.
As mentioned in the comments, Correctly setting the default would be a better solution than explicitly defining the store
You need to specify the store:
Cache::store('redis')->put('bar', 'baz', 600); // 10 Minutes
https://laravel.com/docs/5.8/cache#cache-usage
Simple Solution!
In config\database.php UPDATE the value of default & cache with 1
'redis' => [
'default' => [
// Other settings...
'database' => env('REDIS_DB', 1),
],
'cache' => [
// Other settings...
'database' => env('REDIS_CACHE_DB', 1),
],
],

Call to undefined method Illuminate\Support\Facades\Redis::connect()

Running Laravel 5.7 via Homestead.
This is happening in:
/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php
"line":66
"function":"establishConnection"
We are using PHP Redis and in config/database.php:
'redis' => [
'client' => 'phpredis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 6,
],
],
I've seen other answers on here and none of the steps work. There is no redis cluster in use. PhpRedis extension is installed on VM and confirmed with php -m. Redis server is running at 127.0.0.1 and can be connected to outside of the code.
I'm unable to change the code as it works for others. It must be a configuration issue on my VM but I can't see what it could be. If anyone has had a similar issue I'd much appreciate any advice.
The problem you are having is because of class naming conflict: PhpRedisConnector creates new \Redis client, but it seems that you have alias for Illuminate\Support\Facades\Redis facade set up somewhere in your app. PhpRedisConnector creates new Redis and gets facade object instead.
Try removing this line in your config/app.php:
'aliases' => [
...
'Redis' => Illuminate\Support\Facades\Redis::class, // remove this line
...
]

Laravel 5.2 Mongo MonogDB Failed to parse MongoDB URI

I am getting this error:
MongoDB\Driver\Exception\InvalidArgumentException: Failed to parse MongoDB URI: 'mongodb://mongo:tcp://172.17.0.3:27017/mydatabase' in /var/www/laravel/vendor/mongodb/mongodb/src/Client.php:81
My enviroment variables look like this:
MONGO_DATABASE="mydatabase"
MONGO_HOST="mongo"
MONGO_PASSWORD=""
MONGO_PORT="27017"
MONGO_USERNAME=""
and my database.php looks like this
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_HOST', 'localhost'),
'port' => env('MONGO_PORT', 27017),
'database' => env('MONGO_DATABASE', 'mydatabase'),
'username' => env('MONGO_USERNAME', ''),
'password' => env('MONGO_PASSWORD', ''),
'options' => [
'database' => 'admin' // sets the authentication database required by mongo 3
]
I can correctly ping mongo from the container and it resolves to 172.17.0.3 as per the connection string. It seems as if the rest of the connection string is not getting generated correct?
I am running Laravel 5.2 and my package.json has this entry
"mongodb/mongodb": "^1.0.0",
"jenssegers/mongodb": "3.0.*",
As per https://github.com/jenssegers/laravel-mongodb 3.0.X is highest compatibility for Laravel 5.2.
Many thanks in advance
After further investigation my link from Amazon ECS was passing another MONGO_PORT which was overriding the env set in my .env file.

Categories