preface


Redis is the cache middleware that we currently use on a large scale. Due to its powerful, efficient and convenient functions, Redis has been widely used. Single node Redis has achieved high performance, and to improve availability we can use Redis clusters. And of course, now there are a lot of cluster, the tunnel model (and more advanced virtual tunnel mode), planetary model, intelligent routing, P2P mode, all kinds of, each have each benefits, also have official gave a cluster tool, but no official in this article, this is because of his official on a server with… A machine died, directly all crash, a little unnecessary, this paper mainly talks about the master-slave mode of Redis.

Introduction of redis


  • Distributed –

    1. The so-called distributed is not running alone on a server, no matter how much money the company spends on a server, it also has its limit, 1000T memory is also the limit, so the risk of a single server is relatively large, in case it dies, the whole company completely paralyzed is not appropriate.
    2. So distributed to say let me a service that is deployed in one server, so there is a server was paralyzed for various reasons, and it doesn’t matter, continue to run, but the whole system performance will decline, and then a period of time and so on these nodes restored it can access the distributed system.
  • Memory data store – noSQL

    1. We know that everything we do with the database needs to be done in SQL language, structured query language.
    2. It’s sometimes called noSQL, because the database that we have, frankly it’s very strong, all sorts of relationships, all sorts of messy stuff, but the database, it’s better for complex queries, it’s not as good as noSQL for simple queries, Because noSQL is optimized for simple queries like key value pairs. So its performance is actually higher in this scenario.

Principle of distribution


Requests, data, and calculations are distributed

For example, if there are ten people, one person is responsible for one kind of data, now there is a pile of data waiting, the data end is 1 to find the first person, the data end is 2 to find the second person… It is very simple, but how to disperse this, and dispersed after someone hung up backup how to prepare, how to find, backup machine also hung up how to do, learn a lot of text.

Install and configure Redis


This article needs to use at least three servers, preferably four, because a separate machine to control the convenience of viewing, you please configure your own firewall gateway and other configurations and clone, if you encounter problems can leave a message

  1. Download – wget download. Redis. IO/releases/re…
  2. Decompress -tar -zxf redis-5.0.5.tar.gz
  3. Install base dependencies
  4. yum install tcl gcc make -y

Now open the newly unzipped Redis file in the server:

CD redis - 5.0.5Copy the code

Then you can ls look inside the directory, there is a deps folder, also open it:

cd deps
Copy the code

Then we can make, compile several modules in Redis, there are four modules in redis, and then there is another thing, which is to assemble these compiled modules into programs. A lot of things are like that under Linux, compile and then assemble, a lot of gameplay.

make hiredis jemalloc linenoise lua
Copy the code

The installation

make install
Copy the code

Configure redis

  1. ./install_server.sh
  2. The problem
  3. Please select the redis port for this instance: specifies a port
  4. Please select the redis config file name to specify a configuration file
  5. Please select the redis log file name specifies a log file
  6. Please select the data directory for this instance to specify the disk file location
  7. Please select the redis executable path
  8. Setting random boot

systemctl enable redis_port  

The cluster


Before said the principle of distributed, here to say, cluster and distributed is basically the same meaning, but cluster can represent in addition to the meaning of distributed.

Let’s say I have three services, and I need set ‘name’ ‘AAA’ or get name, and he’ll take your key, which is the name of the example, and compute what’s called a CRC. CRC is the so-called cyclic redundancy check (CRC). CRC is a common method of hashing and hashing data. If the key is converted to a number, the number will be converted to a number. If the key is converted to a number, the number will be converted to a number. If the key is converted to a number, the number will be converted to a number.

And, there is a very important point, is the backup, if one of the failure of the service to find the service on the other machine immediately, and to cross the standby, all put in a same as did not do.

This solution solves roughly 99% of errors, since hardware is not likely to break very often, and 99.999% May require two cross-backup solutions, depending on your needs.

We’re talking about master-slave services. For example, in a server, there are two open, one is the master service, one is the slave service, if the server hangs, the two must die together, if the master service dies, the slave service will immediately follow up, become the master service, after a while, the dead master service recovered and became the slave service, and then he continues to run.

Redis cluster configuration


Again, it’s best to have four virtual machines and static network cards, or you’ll get mad every time you reboot

Redis cluster I prefer manual configuration because there is an official one-click configuration method, but it has a requirement that the cluster must work on one server, which is… It’s just a word to find something to do. Just start a process and get it over with

Then create a configuration file on one of the machines. Name it whatever you want. I prefer redis

vim redis.conf
Copy the code

redis.conf

No # Cluster-enabled yes # Storage node information profile -- all nodes will synchronize with each other Cluster-config-file nodes.conf # Live time -- no heartbeat after 5s automatically deletes nodes and broadcasts cluster-node-timeout 5000 # appendonly yesCopy the code

Next, we can run directly according to our configuration redis try a, direct

redis-server ./redis.conf
Copy the code

If that’s okay, you’ll see this pageJust now we said that our mode is master slave mode, once we have a master node, we need another slave node, directly

cp redis.conf redis2.conf
Copy the code

Conf file and cluster-config-file with a different name, or you’ll get a startup error

Conf and redis2.conf were then transferred by SCP to the other two clones, which are not covered here. SCP Local file path Remote user@ Server ADDRESS: path of the peer server

Finally, it’s time to start our cluster

By the way, if you haven’t turned on the firewall yet, and haven’t noticed this, you’re going to be in for a real pain in the ass, because that means you’re going to have to delete half the steps above and start over

Just say it to each server

firewall-cmd --add-port=7000/tcp --permanent
firewall-cmd --add-port=17000/tcp --permanent
Copy the code

Why open two ports? Because Redis ordered… One is you specify, say 7000, and the other is your number + 10000 7000 is the data communication 17000 that does command communication, redis has its own protocol

Start the cluster


Redis -cli --cluster create 192.168.181.130:7000 192.168.181.130:7001 192.168.181.131:7000 192.168.181.131:7001 192.168.181.132 192.168.181.132:7000:7001 - cluster - replicas of 1Copy the code

–cluster Cluster configuration create Create –cluster-replicas 1 Cluster redundancy 1

Change these addresses to the addresses of your virtual machine

Redis Weakness – Redis cannot guarantee consistency


  1. The set operation is a success if the primary node succeeds
  2. The primary node automatically desynchronizes data. If the synchronization fails, the primary node hangs up
  3. Fetch returns the previous data

Reason: For performance

The solution

  1. Critical data is not redis
  2. If the set succeeds, get does not consider it successful

Refer to the wechat profile picture changed after occasionally changed after others half a day to refresh

Redis master-slave script


There are many things that are extremely inconvenient, such as the control of Redis. Now there are three servers, and 100 servers will make the turnover rate of the company surprisingly, so it is very convenient to write a script for unified control. Here I uphold the Linux community has always been “use, not roll” such a friendly attitude, put the script out you will use it will not leave a message, anyway I will not read……

#! /usr/bin/env bash

if[!The $1 ]
then
  echo 'command:' $0 '[cmd]'
  exit
fi

cmd=The $1;

if [ $cmd= ='initall' ]
then
  for item in `cat server_list.txt`
  do
    echo init $item;
    scp redis_sh.sh root@$item:/root/;

    ssh root@$item '/root/redis_sh.sh init 7000';
    ssh root@$item '/root/redis_sh.sh init 7001';
  done

elif [ $cmd= ='startall' ]
then
  for item in `cat server_list.txt`
  do
    echo start $item;

    ssh root@$item '/root/redis_sh.sh start 7000';
    ssh root@$item '/root/redis_sh.sh start 7001';
  done

elif [ $cmd= ='ps' ]
then
  for item in `cat server_list.txt`
  do
    echo $item;

    ssh root@$item 'ps -ef | grep [r]edis';
  done


elif [ $cmd= ='stopall' ]
then
  for item in `cat server_list.txt`
  do
    echo stopall $item;

    ssh root@$item '/root/redis_sh.sh stopall';
  done

elif [ $cmd= ='create' ]
then
  servers=' '

  for item in `cat server_list.txt`
  do
    servers+=$item: 7000' ';
    servers+=$item: 7001' ';
  done

  redis-cli --cluster create $servers --cluster-replicas 1

else
  echo no this command: The $1

fi
Copy the code

What are you interested in, or what problems you can directly add my friends, we communicate together

Wechat: Dyy916829411 QQ: 916829411