High Performance Website Architecture design cache Chapter (5)-Redis Cluster (TOP)

Source: Internet
Author: User
Tags node server redis cluster install redis

Cluster technology is an important means of building high-performance Web site architecture, Imagine that the site under high concurrency access pressure, but also need to query from the massive data to meet the conditions of the data, and rapid response, we must think of the data is sliced, the data according to some rules into a number of different server nodes, To reduce the pressure on a single-node server.

In the previous article we talked about Redis's master-slave replication technology, when the implementation of Multi-node master-slave, we can also call it a cluster, but the cluster we are talking about today is mainly the use of slicing technology to build Clusters.

The purpose of the cluster is to put different keys scattered to different redis nodes, here we need a rule or algorithm, the common practice is to get the key hash value, and then based on the number of nodes to determine the model, but this approach has its obvious disadvantage, when we need to increase or decrease a node, will cause a lot of Key cannot be hit, this ratio is quite high, so someone has proposed the concept of a consistent Hash.

There are four important characteristics of a consistent hash:

Equalization: It is also defined as balance, which means that the result of the hash can be distributed to all nodes as much as possible, thus effectively utilizing the resources on each Node.

Monotonicity: There are many translations for monotonicity that make me very puzzled, and what I want is that when the number of nodes changes, the result of the hash should be as far as possible to protect the allocated content from being reassigned to the new Node.

Dispersion and load: These two actually mean the same thing, which requires that the consistency hash algorithm should avoid duplication as much as possible with the key Hash.

But the consistency hash is not the point we're going to introduce today because Redis introduces the concept of another hash slot (hash slot).

There are 16,384 hash slots built into the Redis cluster, and when a key-value is required to be placed in a Redis cluster, Redis first calculates a result using the CRC16 algorithm for key and then the result to 16384 for the remainder so that each key corresponds to a number A hash slot between 0-16383, Redis maps a hash slot to a different node based on a roughly equal number of nodes.

The advantage of using hash slots is that you can easily add or remove Nodes.

When the node needs to be increased, it is only necessary to move some hash slots of the other nodes to the new node;

When you need to remove a node, just move the hash slot on the removal node to the other Node.

Internal mechanism, What do I have to do with it, for us, when adding or removing nodes, don't let us stop all Redis services. I'm thankful, that's it.

Let's start by building a Redis cluster to Experience.

Since we are starting multiple Redis instances, Although we can start directly from the command line, it is not always convenient, so let's start with a new three instance directory, 9001,9002,9003, which is the port number of the Redis instance.

I have built the directory here, and then we have previously compiled and modified redis-server, redis.conf These two files are copied into these three directories, after the copy is like This:

We open the redis.conf file, and for the sake of simplicity, we keep only the following configuration Items:

Daemonize Yes
Port 9001
cluster-enabled Yes
Cluster-config-file nodes.conf
Cluster-node-timeout 5000
AppendOnly Yes

Note: port is to be modified to the name of the corresponding directory, that is, each instance has a different port.

Let's start with each of the three examples Below:

zhaoguihuadediannao:~ zhaogh$ CD Applications/dev/redis-cluster

Zhaoguihuadediannao:redis-cluster zhaogh$ CD 9001

zhaoguihuadediannao:9001 zhaogh$./redis-server./redis.conf

zhaoguihuadediannao:9003 zhaogh$ CD. /9002

zhaoguihuadediannao:9002 zhaogh$./redis-server./redis.conf

zhaoguihuadediannao:9002 zhaogh$ CD. /9003

zhaoguihuadediannao:9003 zhaogh$./redis-server./redis.conf

zhaoguihuadediannao:9003 zhaogh$

Next we create the cluster and let three instances communicate with one another:

ZHAOGUIHUADEDIANNAO:SRC zhaogh$./redis-trib.rb create--replicas 0 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003

>>> Creating Cluster

Connecting to Node 127.0.0.1:9001:ok

Connecting to Node 127.0.0.1:9002:ok

Connecting to Node 127.0.0.1:9003:ok

>>> performing hash slots allocation on 3 nodes ...

Using 3 Masters:

127.0.0.1:9001

127.0.0.1:9002

127.0.0.1:9003

m:92c9912cb1ccf657c886ecd839dd32c66efd8762 127.0.0.1:9001

slots:0-5460 (5461 Slots) Master

m:b6d46fcb8b0e6ee373b09a4f2cbcec744d1a259b 127.0.0.1:9002

slots:5461-10922 (5462 Slots) Master

M:44AB30C7C589FFB15B9B04DD827C72CFAEEDACB2 127.0.0.1:9003

slots:10923-16383 (5461 Slots) Master

Can I Set the above configuration? (type ' yes ' to accept): Yes

>>> Nodes Configuration Updated

>>> Assign a different config epoch to each node

>>> sending CLUSTER MEET messages to join the CLUSTER

Waiting for the cluster to Join:

>>> performing Cluster Check (using node 127.0.0.1:9001)

m:92c9912cb1ccf657c886ecd839dd32c66efd8762 127.0.0.1:9001

slots:0-5460 (5461 Slots) Master

m:b6d46fcb8b0e6ee373b09a4f2cbcec744d1a259b 127.0.0.1:9002

slots:5461-10922 (5462 Slots) Master

M:44AB30C7C589FFB15B9B04DD827C72CFAEEDACB2 127.0.0.1:9003

slots:10923-16383 (5461 Slots) Master

[OK] All nodes agree about slots Configuration.

>>> Check for open Slots ...

>>> Check Slots Coverage ...

[OK] All 16384 Slots Covered.

ZHAOGUIHUADEDIANNAO:SRC zhaogh$

It is important to note that the Execute REDIS-TRIB.RB command requires Ruby support, and if you do not install it you can download it first to Https://rubygems.org/gems/redis and then install it OFFLINE.

sudo gem install redis-3.0.7.gem--local

Below we test with Redis's own client:

ZHAOGUIHUADEDIANNAO:SRC ZHAOGH$/REDIS-CLI-C-P 9001

127.0.0.1:9001> Get testkey001

-redirected to Slots [12786] located at 127.0.0.1:9003

(nil)

127.0.0.1:9003> Set testkey002 testvalue002

-redirected to Slots [401] located at 127.0.0.1:9001

Ok

127.0.0.1:9001> Get testkey002

"testvalue002"

127.0.0.1:9001> Set testkey003 testvalue003

Ok

127.0.0.1:9001>

As you can see, although the first time we connected is Port 9001, when we went to get testkey001, Redis cluster automatically redirected us to 9003.

When we set testkey002 in 9003, redis cluster is redirected to 9001.

In general, Redis cluster deployment is very convenient, unfortunately, there are almost no C # clients can well support Redis cluster, It is very sad, we look forward to their updates.

Next time, We continue to discuss Redis clusters, such as how to add nodes, remove nodes, re-slice, and so On.

Reprint To: http://www.cnblogs.com/zhaoguihua/p/redis-005.html

High Performance Website Architecture design cache Chapter (5)-Redis Cluster (TOP)

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.