Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring highly available Redis cluster in Laravel

Tags:

php

redis

laravel

I'm trying to update my Laravel application so that Queue::push() pushes jobs to a redis queue cluster. The guy setting that up is communicating to me that our application needs to be configured with connection details for the primary master and several slaves. If this is the correct way to set this up I'm struggling to figure out how to configure this.

Out of the box the redis config looks something like...

'redis' => array(
    'cluster' => true,
    'default' => array(
            'host' => '127.0.0.1',
            'port' => 6379,
            'database' => 0,
        ),
    ),
);

I've been digging through the Laravel driver trying to figure out how to configure masters and slaves within Laravel and haven't been able to figure it out. How can I add slaves here?

Or is this the wrong direction?

like image 762
Webnet Avatar asked Dec 24 '22 23:12

Webnet


2 Answers

As of nrk/predis v1.1.0 there is built in support for the Redis Sentinel API.

There are slight configuration changes you will need to apply to Laravel in order to utilise this capability. The easiest way is to do this is use one of the redis sentinel packages for Laravel. These are simply wrappers for the Laravel driver and take care of the configuration. One that I personally use in a large project:

cooperaj/laravel-redis-sentinel

What is Redis Sentinel?

Redis Sentinel is a seperate service that works with a Redis Cluster, monitoring its health for a highly available cluster with automatic failover.

How do you use it?

We run our entire application in Kubernetes, that includes a Laravel 5.2 API and React/Node websites running from that API. We have a Redis Sentinel cluster configured similar to this. We also run an Elasticsearch cluster similar to this.

What is your hardware?

Our entire operations / systems layer is built on a Google Container Engine cluster.

Why is this important?

If you rely on Redis for your Laravel caching or queues your application will fail if Redis fails for any reason.

like image 143
AndrewMcLagan Avatar answered Dec 27 '22 13:12

AndrewMcLagan


Laravel's Redis driver only supports Redis Cluster, which is sharded. If you need an HA Redis system you'll need to use Sentinels which means you can use https://github.com/Indatus/laravel-PSRedis

like image 40
Webnet Avatar answered Dec 27 '22 13:12

Webnet