1. Redis installation
1.1. Redis is installed
Redis is c language development.
Install redis need c compiler environment language. If you do not need to install gcc online. yum install gcc-c ++
installation steps:
The first step: redis source package uploaded to the linux system.
Step two: Unpack redis.
Step Three: compiler. Redis into the source directory. make
Step four: Install. make install PREFIX = / usr / local / redis
PREFIX parameter specifies the installation directory of redis. General software is installed to / usr directory
1.2 Connecting redis
. 1.2.1 redis start:
Distal Start: start redis-server installation directory directly under the redis
[root@localhost bin]# ./redis-server
Background start:
Copy the /root/redis-3.0.0/redis.conf to the next / usr / local / redis / bin directory
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis/bin/
Modify the configuration file:
[root@localhost bin]# ./redis-server redis.conf
View redis process:
[root@localhost bin]# ps aux|grep redis
root 5190 0.1 0.3 33936 1712 ? Ssl 18:23 0:00 ./redis-server *:6379
root 5196 0.0 0.1 4356 728 pts / 0 S + 18:24 0:00 grep Redis
[root@localhost bin]#
1.2.2. Redis-cli
[root@localhost bin]# ./redis-cli
The default connection localhost running redis 6379 port services.
[root@localhost bin]# ./redis-cli -h 192.168.25.153 -p 6379
-h: address of the server connection
-p: Port number services
关闭redis:[root@localhost bin]# ./redis-cli shutdown
1.3. Redis five data types
String: key-value (do caching)
Redis All data are strings. Commands are not case sensitive, key is case-sensitive. Redis is single-threaded. Redis is not suitable for large content stored data.
get、set、
incr: plus one (generated id)
Decr: minus one
Hash: key-fields-values (do caching)
Corresponds to a key for a map, map the key-value as well as
Use the hash key to classify.
Hset: Add content to the hash
Hget: take content from the hash
List: sequential repeat
192.168.25.153:6379> lpush list1 a b c d
(integer) 4
192.168.25.153:6379> lrange list1 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
192.168.25.153:6379> rpush list1 1 2 3 4
(integer) 8
192.168.25.153:6379> lrange list1 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
5) "1"
6) "2"
7) "3"
8) "4"
192.168.25.153:6379>
192.168.25.153:6379> lpop list1
"d"
192.168.25.153:6379> lrange list1 0 -1
1) "c"
2) "b"
3) "a"
4) "1"
5) "2"
6) "3"
7) "4"
192.168.25.153:6379> rpop list1
"4"
192.168.25.153:6379> lrange list1 0 -1
1) "c"
2) "b"
3) "a"
4) "1"
5) "2"
6) "3"
192.168.25.153:6379>
Set: elements without order, can not be repeated
192.168.25.153:6379> sadd set1 a b c c c d
(integer) 4
192.168.25.153:6379> smembers set1
1) "b"
2) "c"
3) "d"
4) "a"
192.168.25.153:6379> srem set1 a
(integer) 1
192.168.25.153:6379> smembers set1
1) "b"
2) "c"
3) "d"
192.168.25.153:6379>
There are set operations command, self-study.
SortedSet (zset): sequential, can not be repeated
192.168.25.153:6379> zadd zset1 2 a 5 b 1 c 6 d
(integer) 4
192.168.25.153:6379> zrange zset1 0 -1
1) "c"
2) "a"
3) "b"
4) "d"
192.168.25.153:6379> zrem the zset1
(integer) 1
192.168.25.153:6379> zrange zset1 0 -1
1) "c"
2) "b"
3) "d"
192.168.25.153:6379> zrevrange zset1 0 -1
1) "d"
2) "b"
3) "c"
192.168.25.153:6379> zrange zset1 0 -1 withscores
1) "c"
2) "1"
3) "b"
4) "5"
5) "d"
6) "6"
192.168.25.153:6379> zrevrange zset1 0 -1 withscores
1) "d"
2) "6"
3) "b"
4) "5"
5) "c"
6) "1"
192.168.25.153:6379>
1.4. Key command
Set the expiration time of the key.
Expire key second: set the expiration time of key
Ttl key: View key valid
Persist key: remove the key expiration time. Key persistence.
192.168.25.153:6379> expire Hello 100
(integer) 1
192.168.25.153:6379> ttl Hello
(integer) 77
2. Redis persistence scheme
All Redis data are saved to memory.
Rdb: snapshot form, on a regular basis to save the current time data in memory to disk. Redis supported by default persistence solution.
aof form: append only file. Redis command of all database operations, additions and deletions to change the order of operations. Saved to a file. When all the database recovery command to perform it again.
Redis.conf configuration in the configuration file.
Rdb:
Aof configuration:
Two kinds of persistence solution while opening them aof file to restore the database.
3. Redis cluster structures
3.1. Redis-cluster architecture diagram
redis-cluster voting: Fault Tolerance
Architectural details:
(1) All of the nodes are interconnected and redis (PING-PONG mechanism), the internal transmission speed using the binary protocol and bandwidth optimization.
fail (2) is to take effect when the node cluster by more than half of the nodes detect the failure.
(3) redis client node is connected, without an intermediate layer proxy. The client need not be connected to all cluster nodes in the cluster connected to an available node
(4) redis-cluster mapping all of the physical node to [0-16383] the slot, cluster maintains node <-> slot <-> value
16384 clusters built Redis hash slot, when a key-value to be placed in the cluster Redis, Redis crc16 algorithm key using the first calculated result, and the results of the remainder number of 16384, such that each key will correspond to a No. 0-16383 hash between the grooves, redis will be substantially equal to the hash slot mapped to different nodes according to the number of nodes
3.2. Redis cluster structures
Redis cluster should be at least three nodes. To ensure high availability cluster, each node needs to have a backup machine.
Redis cluster requires at least 6 servers.
Build a pseudo-distributed. You can use a virtual machine running six redis instance. Redis need to modify the port number 7001-7006
3.2.1. Cluster Setup environment
1, using ruby script to build clusters. Need ruby operating environment.
Install ruby
yum install ruby
yum install rubygems
2, the installation package ruby script to use.
[root@localhost ~]# gem install redis-3.0.0.gem
Successfully installed redis-3.0.0
1 gem installed
Installing ri documentation for redis-3.0.0...
Installing RDoc documentation for redis-3.0.0...
[root@localhost ~]#
[root@localhost ~]# cd redis-3.0.0/src
[root@localhost src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr 1 2015 redis-trib.rb
3.2.2 build step
Redis need six servers. Build a pseudo-distributed.
Redis need six instances.
You need to run at different ports 7001-7006
Step 1: Create 6 redis instances, each running on a different port. Redis.conf need to modify the configuration file. Configuration file also need to comment before the cluster-enabled yes removed.
Step Two: Start each redis instance.
The third step: Use ruby script to build clusters.
./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006
Create a script to close the cluster:
[root@localhost redis-cluster]# vim shutdow-all.sh
redis01/redis-cli -p 7001 shutdown
redis01/redis-cli -p 7002 shutdown
redis01/redis-cli -p 7003 shutdown
redis01/redis-cli -p 7004 shutdown
redis01/redis-cli -p 7005 shutdown
redis01/redis-cli -p 7006 shutdown
[root@localhost redis-cluster]# chmod u+x shutdow-all.sh
[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006
>>> Creating cluster
Connecting to node 192.168.25.153:7001: OK
Connecting to node 192.168.25.153:7002: OK
Connecting to node 192.168.25.153:7003: OK
Connecting to node 192.168.25.153:7004: OK
Connecting to node 192.168.25.153:7005: OK
Connecting to node 192.168.25.153:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.25.153:7001
192.168.25.153:7002
192.168.25.153:7003
Adding replica 192.168.25.153:7004 to 192.168.25.153:7001
Adding replica 192.168.25.153:7005 to 192.168.25.153:7002
Adding replica 192.168.25.153:7006 to 192.168.25.153:7003
M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001
slots:0-5460 (5461 slots) master
M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002
slots:5461-10922 (5462 slots) master
M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003
slots:10923-16383 (5461 slots) master
S: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004
replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3
S: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005
replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01
S: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006
replicates 2935007902d83f20b1253d7f43dae32aab9744e6
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 192.168.25.153:7001)
M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001
slots:0-5460 (5461 slots) master
M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002
slots:5461-10922 (5462 slots) master
M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003
slots:10923-16383 (5461 slots) master
M: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004
slots: (0 slots) master
replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3
M: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005
slots: (0 slots) master
replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01
M: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006
slots: (0 slots) master
replicates 2935007902d83f20b1253d7f43dae32aab9744e6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-cluster]#
3.3. Using cluster
Redis-cli connected clusters.
[root@localhost redis-cluster]# redis01/redis-cli -p 7002 -c
-c: connection on behalf of the cluster is redis