Configuring highly available Redis cluster in Laravel

10,937

Solution 1

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.

Solution 2

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

Share:
10,937

Related videos on Youtube

Ben
Author by

Ben

Updated on June 04, 2022

Comments

  • Ben
    Ben almost 2 years

    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?

  • AndrewMcLagan
    AndrewMcLagan over 7 years
    This is no longer the case since predis now supports sentinel replication
  • Ben
    Ben over 7 years
    @AndrewMcLagan Can you reference some documentation or anything to support this so I can update this answer?
  • AndrewMcLagan
    AndrewMcLagan over 7 years
    Please see my answer. I chased my tail on this for a while. So I feel its important to contribute to the community!
  • mr_carrera
    mr_carrera over 6 years
    +1 thanks for all the useful links! The extra context is helpful. Have you used the Laravel Redis Sentinel Drivers package in any large projects? We have to choose libraries with unit tests.
  • Jeremy Anderson
    Jeremy Anderson over 6 years
    This answer saved me so much time and headache, I would upvote it twice, if I could. Ultimately a quotidian task, but oh so useful an answer.