Phpredis Redis Array Redis Arrays

Source: Internet
Author: User
Tags rehash

Official Url:https://github.com/phpredis/phpredis/blob/master/arrays.markdown#readme

Redis Arrays

A Redis Array is an isolated namespace in which keys is related in some manner. Keys is distributed across a number of Redis instances, using consistent hashing. A hash function is used-spread the keys across the array in order to keep a uniform distribution. This feature is added as the result of a generous sponsorship by A+e Networks.

An array is composed of the following:

    • A List of Redis hosts.
    • A key extraction function, used to hash part of the key in order to distribute related keys on the same node (optional). This was set by the "function" option.
    • A list of nodes previously in the ring, only present after a node have been added or removed. When a read command was sent to the array (e.g. GET, lrange ...), the key is first queryied in the main ring, and then in th E Secondary ring If it is not found in the main one. Optionally, the keys can migrated automatically when this happens. Write commands'll always go to the main ring. This was set by the "previous" option.
    • An optional index in the form of a Redis set per node, used to migrate keys when nodes is added or removed; Set by the "index" option.
    • An option to rehash the array automatically as nodes is added or removed, set by the "autorehash" option.
Creating an array

There is several ways of creating Redis arrays; They can pre-defined in Redis.ini using new RedisArray(string $name); , or created dynamically usingnew RedisArray(array $hosts, array $options);

Declaring a new array with a list of nodes
$ra = new Redisarray (Array ("Host1", "host2:63792", "host2:6380"));
Declaring a new array with a list of nodes and a function to extract a part of the key
function Extract_key_part ($k) {    return substr ($k, 0, 3);//hash only on first 3 characters.} $ra = new Redisarray (Array ("Host1", "host2:63792", "host2:6380"), Array ("function" = "extract_key_part"));
Defining a "previous" array when nodes is added or removed.

When a new node was added to an array, Phpredis needs to know about it. The old list of nodes becomes the ' previous ' array, and the new list of nodes is used as a main ring. Right after a node have been added, some read commands would point to the wrong nodes and would need to look up the keys in T He previous ring.

Adding Host3 to a ring containing host1 and host2.  Read commands the previous ring if the data is a not found in the main ring. $ra = new Redisarray (Array ("Host1", "Host2", "Host3"), Array ("previous" = = Array ("Host1", "Host2")));
Specifying the "retry_interval" parameter

The retry_interval is used to specify a delay in milliseconds between reconnection attempts in case the client loses Conne Ction with a server

$ra = new Redisarray (Array ("Host1", "host2:63792", "host2:6380"), Array ("retry_timeout" = 100));
Specifying the "lazy_connect" parameter

This option was useful when a cluster had many shards but not of them was necessarily used at one time.

$ra = new Redisarray (Array ("Host1", "host2:63792", "host2:6380"), Array ("lazy_connect" = true));
Specifying the "connect_timeout" parameter

The Connect_timeout value is a double and was used to specify a timeout in number of seconds when creating redis socket con Nections used in the Redisarray.

$ra = new Redisarray (Array ("Host1", "host2:63792", "host2:6380"), Array ("connect_timeout" = 0.5));
Specifying the "read_timeout" parameter

The Read_timeout value is a double and was used to specify a timeout in number of seconds when waiting response from the SE RVer.

$ra = new Redisarray (Array ("Host1", "host2:63792", "host2:6380"), Array ("read_timeout" = 0.5));
Defining Arrays in Redis.ini

Because php.ini parameters must is pre-defined, Redis Arrays must all share the same. ini settings.

List available Redis arraysini_set (' redis.array.names ', ' users,friends ');//Set host names for each array.ini_set (' Red Is.arrays.hosts ', ' users[]=localhost:6379&users[]=localhost:6380&users[]=localhost:6381&users[]= Localhost:6382&friends[]=localhost ');//Set Functionsini_set (' Redis.arrays.functions ', ' users=user_hash ');// Use index only for Usersini_set (' Redis.arrays.index ', ' users=1,friends=0 ');
Usage

Redis arrays can be used just as Redis objects:

$ra = new Redisarray ("Users"), $ra->set ("User1:name", "Joe"), $ra->set ("User2:name", "Mike");
Key hashing

By default and on order to being compatible with other libraries, Phpredis would try to find a substring enclosed in curly bra Ces within the key name, and use it to distribute the data.

For instance, the keys "{User:1}:name" and "{user:1}:email" would be stored on the same server as only "user:1" would be have Hed. You can provide a custom function name in your Redis array with the "function" option; This function is called every time a key needs to be hashed. It should take a string and return a string.

Custom Key Distribution function

In order to control the distribution of keys by hand, you can provide a custom function or closure that returns the server Number, which is the "index" in the array of servers so created the Redisarray object with.

For instance, instanciate a Redisarray object with and new RedisArray(array("us-host", "uk-host", "de-host"), array("distributor" => "dist")); write a function called ' dist ' that'll return for all 2 The keys is should end up on the "De-host" server.

Example
$ra = new Redisarray (Array ("Host1", "Host2", "Host3", "Host4", "Host5", "Host6", "Host7", "Host8"), Array ("distributor" =& Gt Array (2, 2));

This declares, we started with 2 shards and moved to 4 then 8 shards. The number of initial shards is 2 and the resharding level (or number of iterations) is 2.

migrating keys

When a node was added or removed from a ring, Redisarray instances must was instanciated with a "previous" List of nodes. A single call to causes all the keys to being $ra->_rehash() redistributed according to the new list of nodes. Passing a callback function to _rehash() makes it possible to track the progress of this operation:the function is called wit H a node name and a number of keys that would be examined, e.g. _rehash(function ($host, $count){ ... });

It is possible to automate this process, by setting in the ‘autorehash‘ => TRUE constructor options. This would cause keys to being migrated when they need to is read from the previous array.

In order to migrate keys, they must all is examined and rehashed. If the "index" option is set, a single key per node lists all keys present there. Otherwise, the KEYS command is used to list them. If a "Previous" List of servers is provided, it'll be used as a backup ring when keys can isn't be found in the current RI Ng. Writes go to the new ring, whilst reads would go to the new ring first, and to the second ring as a backup.

Adding and/or removing several instances is supported.

Example
$ra = new Redisarray ("users"); Load up a new config from Redis.ini, using the ". Previous" listing. $ra->_rehash ();

Running This code would:

    • Create a new ring with the updated list of nodes.
    • Server by server, look up all the keys in the previous list of nodes.
    • Rehash each key and possibly move it to another server.
    • Update the array object with the new list of nodes.
Multi/exec

Multi/exec is still available, but must was run on a single node:

$host = $ra->_target ("{users}:user1:name");//Find host First$ra->multi ($host)//Then run transaction on that host.   ->del ("{users}:user1:name")   ->srem ("{Users}:index", "user1")   ->exec ();
Limitations

Key Arrays offer no guarantee if using REDIS commands that span multiple keys. Except for the use of MGET, MSET, and DEL, a single connection would be used and all the keys read or written there. Running KEYS () on a Redisarray object would execute the command on each node and return an associative array of KEYS, index Ed by host name.

Array Info

Redisarray objects provide several methods to help understand the state of the cluster. These methods start with an underscore.

    • $ra->_hosts()→returns a list of hosts for the selected array.
    • $ra->_function()→returns the name of the function used to extract key parts during consistent hashing.
    • $ra->_target($key)→returns the host to is used for a certain key.
    • $ra->_instance($host)→returns a Redis instance connected to a specific node; Use with to _target get a single Redis object.
Running The Unit tests
$ cd tests$./mkring.sh start$ php array-tests.php

Phpredis Redis Array Redis Arrays

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.