Laravel framework implements redis cluster method analysis, laravelredis

Source: Internet
Author: User
Tags crc32 redis cluster

Laravel framework implements redis cluster method analysis, laravelredis

This article describes how to implement a redis cluster using the Laravel framework. We will share this with you for your reference. The details are as follows:

Configure app/config/database. php as follows:

'redis' => array(    'cluster' => true,    'default' => array(      'host'   => '172.21.107.247',      'port'   => 6379,    ),   'redis1' => array(      'host'   => '172.21.107.248',      'port'   => 6379,    ),

Select "true" as the cluster, and then use it as the cluster;

If you set the session driver to redis, you can use the cluster function:

Let's take a look at the implementation of the session. When we write in the code like this:

Session::put('test', 124);

The actual execution process is as follows:

Illuminate \ Support \ Facades \ SessionIlluminate \ Support \ Facades \ FacadeIlluminate \ Session \ Facade: app ['session']-> putIlluminate \ session \ Facade :: app ['session'] Is Illuminate \ session \ SessionManagerIlluminate \ Support \ Manager :__ call

The Session creates a driver Based on the returned result.

$this->app['config']['session.driver']

Which is configured in the configuration file. Here we configure it as redis

Illuminate\Session\SessionManager::Illuminate\Session\SessionManager

Eventually, Illuminate \ Session \ Store is responsible for the put call.

The Store class stores Illuminate \ Session \ CacheBasedSessionHandler.

The latter forwards the request$this->app['cache']->driver($driver)
......
After a series of code tracing, the storage class is Predis \ Client \ Database. Check its constructor:

public function __construct(array $servers = array()){    if (isset($servers['cluster']) && $servers['cluster'])    {      $this->clients = $this->createAggregateClient($servers);    }    else    {      $this->clients = $this->createSingleClients($servers);    }}

If it is set to a cluster, the createAggregateClient method is called.

protected function createAggregateClient(array $servers){    $servers = array_except($servers, array('cluster'));    return array('default' => new Client(array_values($servers)));}

All servers are placed in the default group.

The actual data storage class is Predis \ Client. Here is the code for creating a server based on the configuration. You can check it for yourself;

The Predis \ Cluster \ PredisClusterHashStrategy class is responsible for calculating the key hash. key functions:

GetHash

GetKeyFromFirstArgument

Predis \ Cluster \ Distribution \ HashRing is responsible for maintaining the server ring and key functions.

AddNodeToRing

Get

Hash

The general principle is as follows: execute the following redis command

get ok

Uses OK as the crc32 operation to get a hash value.

All servers are put into an array with a default length of 128 according to a certain algorithm. The number of items that each server occupies is determined by the following:

Weight/total weight * Total number of servers * 128, referPredis\Cluster\Distribution\HashRing::addNodeToRingMethod

The hash value of each item is calculated by the server ip Address: PORT format for crc32.

protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio){    $nodeObject = $node['object'];    $nodeHash = $this->getNodeHash($nodeObject);    $replicas = (int) round($weightRatio * $totalNodes * $replicas);    for ($i = 0; $i < $replicas; $i++) {      $key = crc32("$nodeHash:$i");      $ring[$key] = $nodeObject;    }}

The hash value of the key is also available, and the server ring is well calculated. The rest is the search. The binary method can quickly find the corresponding server node.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.