pyurin/yii2-redis-ha

Basic redis-ha client

Installs: 183 444

Dependents: 0

Suggesters: 0

Security: 0

Stars: 17

Watchers: 3

Forks: 11

Open Issues: 0

Type:yii2-extension

1.2 2017-11-14 10:34 UTC

This package is not auto-updated.

Last update: 2024-12-21 17:53:51 UTC


README

This Yii2 component is based on core yii2-redis component https://github.com/yiisoft/yii2-redis but gives ability to work with master-slave redis clusters with sentiels.
It's rather simple - gets master server address from given sentinels and connects to it, then operates just like \yii\redis\Connection.
It works with sentinels only, does not connect to redis hosts without sentinels.

Workflow of connection process

Loop over given sentinels and search for an alive one that will give a master host address.
If a sentinel does not respond or it's respond is empty, then we'll try to check the next one.
If no successfull reply was received, then connection will fail.
After we've found a redis master, we'll connect to it.
If connection to the master server fails, we won't try anything else, we'll fail.
If we sucessfully connect to the master server, we'll then check with role command if it really is master (http://redis.io/topics/sentinel-clients).
If it's not - we'll fail, if it really is - we're sucessfully connected.

Adviced configuration

Redis HA cluster must be done with minimum 2 redis servers and 3 sentinels.
I suppose that in termes of performance, considering this implementation it's better to have a sentinel server for each app locally, to avoid unnecessary overheads.
That would not be important if we cached previously givem master server, but we dont. We ask sentinel each time before connecting to redis.
That's how I see a simple architecture:

    -------                    -------
   |       |                  |       |
   | Redis |                  | Redis |
   |       |                  |       |
    ------- \                 /-------
       |     \               /   |
       |      \             /    |
       |       \-----------/     |
       |       |\         /|     |
       |       |  Sentinel |     |
       |       |   .    .  |     |
       |        --.------.-      |
       |         .        .      |
       |        .          .     |
  -----|-------.---        -.----|----------     
 |     |      .    |      |  .   |          |    
 |    Sentinel. . .| . . .|. . Sentinel     |    
 |         |       |      |         |       |    
 |  (localhost)    |      |  (localhost)    |    
 |         |       |      |         |       |    
 |         |       |      |         |       |    
 |        APP      |      |        APP      |    
 |                 |      |                 |    
  -----------------        -----------------     
   

Usage

Basic example:

  			'session' => [
  					'class' => '\yii\redis\Session',
  					'redis' => [
  						'class' => '\pyurin\yii\redisHa\Connection',
  						'masterName' => 'mymaster',
  						'sentinels' => [
  								'localhost'
  						]
  					]
  			],

Sentinel servers are queried in the same order as exist in array.

Installation

Available with composer:

  "require" : {
    "pyurin/yii2-redis-ha":"*"
  },