Git Product home page Git Product logo

laravel-redis-sentinel's Introduction

Laravel Redis Sentinel

Build Status Scrutinizer Code Quality

This provides a Sentinel aware driver for Laravel. A Redis cluster with Sentinels supports a high availability Master/Slave architecture that provides automatic failover should a node stop working.

It's simple code and merely allows you to configure Sentinels correctly by changing some assumptions Laravel makes about how you're using Redis.

Compatibility

Version Supported Laravel Version
^0.0 ^5.2
^1.0 ^5.3

Installation

Add the Service provider to your config/app.php, you should also comment out (or remove) the default illuminate Redis driver:

'providers' => [

    // Illuminate\Redis\RedisServiceProvider::class,

    ...

    RedisSentinel\Laravel\RedisSentinelServiceProvider::class,
]

Point your Redis database at a set of Redis Sentinels. Change the redis part of your config/database.php to something like:

'redis' => [

    'cluster' => false,

    'default' => [
        [
            'host' => env('REDIS_SENTINEL_1'),
            'port' => 26379
        ],
        [
            'host' => env('REDIS_SENTINEL_2'),
            'port' => 26379
        ],
        [
            'host' => env('REDIS_SENTINEL_3'),
            'port' => 26379
        ],
        'options' => [
            'replication' => 'sentinel',
            'service' => 'mymaster',
            'parameters' => [
                'database' => 0,
                'password' => env('REDIS_PASSWORD', null)
            ]
        ]
    ],

    // optional configuration for a separate Redis 'database' for just a cache
    'cache' => [
        [
            'host' => env('REDIS_SENTINEL_1'),
            'port' => 26379
        ],
        [
            'host' => env('REDIS_SENTINEL_2'),
            'port' => 26379
        ],
        [
            'host' => env('REDIS_SENTINEL_3'),
            'port' => 26379
        ],
        'options' => [
            'replication' => 'sentinel',
            'service' => 'mymaster',
            'parameters' => [
                'database' => 1, // note the differing 'database' number
                'password' => env('REDIS_PASSWORD', null)
            ]
        ]
    ],

    'options' => [
    ]

],

Optionally you can add a configuration option that causes Predis to interrogate a given Sentinel for a complete list of Sentinels. If you do this then you only need to provide a single Sentinel in the configuration. Predis will ensure that the Sentinel list is kept up to date on subsequent queries.

'default' => [
    [
        'host' => env('REDIS_SENTINEL'),
        'port' => 26379
    ],
    'options' => [
        'replication' => 'sentinel',
        'service' => 'mymaster',
        'update_sentinels' => true,
        'parameters' => [
            'database' => 0,
            'password' => env('REDIS_PASSWORD', null)
        ]
    ]
],

Queue

Add a connection to your config/queue.php file:

'connections' => [

    ...

    'sentinel' => [
        'driver' => 'sentinel-redis',
        'connection' => 'default', // or any other named 'database' you define in database.php
        'queue' => 'default',
        'retry_after' => 90,
    ],
],

Configure your env file to use the new driver:

QUEUE_DRIVER=sentinel

Cache

Laravel will quite happily use Redis as a cache location. What they don't tell you is that clearing your cache does a simplistic FLUSHDB command. Something you don't want to use if you're also using queues in Redis. "Oh no, all my queued jobs have disappeared".

To fix this setup a cache database configuration as shown in the example config/database.php snippet above, ensuring that you use a different database number and change the Redis section of config/cache.php to read:

'redis' => [
    'driver' => 'redis',
    'connection' => 'cache', \\ make sure this matches the name you gave your 'database'
],

laravel-redis-sentinel's People

Contributors

cooperaj avatar scrutinizer-auto-fixer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravel-redis-sentinel's Issues

Service provider not loaded when running queue:listen

Hi,

I've had a problem with your code, namely the service provider does not boot/register when I run php artisan queue:listen sentinel. Instead, I get the error "queue driver sentinel-redis not found" (because of course the boot method has not been called).

The only solution I have found was to set $defer to false. Do you have an idea what the cause could be?

Best regards,
Tom

Daemon notification

First of all, thanks for creating this. I just deployed it to my production laravel stack and it's working great so far.

In my HA setup I normally run supervisor with laravel queues in daemon mode to keep things running at a lower CPU level. Unfortunately it seems like daemon based queues aren't able to renegotiate the redis connection when the sentinel master goes down. When I run the queues in non daemon mode, it appears to renegotiate the connection successfully.

I'm totally cool running my queues in non daemon mode, and if things started to affect performance I'll just spin up a dedicated queue box. But if you run your queues in daemon mode I would love to hear about how you got it working.

Thanks!

No sentinel server available for autodiscovery

Hi there,

I have been trying to connect with the sentinel but getting this error "No sentinel server available for autodiscovery", tried multiple options but no avail using laravel 5.3.

Any help would be appreciated.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.