Redis common data types + detailed explanation of the construction of three major cluster modes of redis

1. Other data types of Redis

1.1 hash data type

Its value is a hash type, and the structure key value form of the hash type. Generally used to store object data.

hset key field value [field value]: Set the value of the field field in the hash table key to value hget key field: Get the value of the specified field stored in the hash table.
hgetall key: Get all fields and values ​​of the specified key in the hash table
hkeys key: Get all the fields in the hash table
hvals key: Get all the values ​​in the hash table
hdel key field: Delete one or more hash table fields

1.2 List<List> data type

Its value is a List data type, and the value can be multiple, ordered, and repeatable.

lpush key element [element...]: Add one or more values ​​​​to the list
Lindex key index: Get the element with the specified index in the list.
lrange key start end: Get a certain range of elements. The first is 0 and the last is -1
lpop key: remove the first element on the left
lset key index element: replace the element content at the specified position 

1.3 Set data type

It is similar to the list type, except that its values ​​are not allowed to be repeated and are unordered.

sadd key element[element....]: Add one or more values ​​​​to the collection
smembers key: Get all elements in the collection.
sinter key1 key2: Returns the intersection of all given sets
sdiff key1 key2: Returns all given sets

1.4 sort set data type

It is similar to set. When adding elements, it specifies the score value and sorts it according to the score. Ranking list.

zadd key score element [score element ...]: Add an ordered set element
 zrange key start end [withscopes]: Get the elements in the set from small to large
 zrevrange key start end [withscopes]: Get them from large to small Elements in the collection
 zrem k1 element [element]: Remove one or more elements from the collection

2. Application scenarios of actual development of redis

1. Caching of hot data: Reduce the frequency of access to the database and reduce the pressure on the database.
2. Application of limited-time services: flash sale, storage of user information of logged-in users, storage of SMS verification codes
3. Counter-related issues: number of likes, number of favorites, number of plays.
4. Questions related to rankings: sort set 
5. Distributed lock: ---Synchronization lock: 
6. Limited flash sale: ---decr key:

3. Persistence of redis

Persist data in memory to disk to prevent data loss. ---When the redis server is started, the data in the disk will be loaded into the memory for calculation.

Improved two persistence methods:

RDB and AOF.

3.1 RDB persistence

It stores snapshots every once in a while. --It is a binary file, and the content inside cannot be understood after opening it.

Trigger mechanism:

[1]save---manual trigger

[2]bgsave--manual trigger

[3]Through configuration file--automatic triggering

(1) save trigger

This command will block the current Redis server. During the execution of the save command, Redis cannot process other commands until the RDB process is completed. The specific process is as follows:

bgsave:

When executing this command, Redis will perform snapshot operations asynchronously in the background, and the snapshot can also respond to client requests. The specific process is as follows:

When bgsave executes this command, it will fork a new thread to perform RDB persistence operations independently without affecting other customers' operations on the redis service.  

When bgsave executes this command, it will fork a new thread to perform RDB persistence operations independently without affecting other customers' operations on the redis service.

RDB snapshot persistence advantages and disadvantages:

Advantages: ----Fast data recovery speed.

Disadvantages: ----poor data integrity--the last period of data will be lost.

3.2 AOF persistence

Log append persistence. When we perform a write operation, a function write will be triggered to append the command for the write operation to a log file appendfile. When the server starts, the commands in appendfile will be executed again. Not enabled by default.

Manually enable aof mode.

AOF advantages and disadvantages:

  1. Advantages: High data integrity---the last instruction is lost.

  2. Disadvantages: Data recovery is slow.

If both rdb and aof are used, which file will be loaded when the server restarts?

               First load the AOF file [it focuses on data integrity].

4. redis cluster mode

Three cluster modes are provided.

[1]Master-slave mode

[2] Sentry mode

[3]Cluster mode

Why use cluster mode:

[1] Solve the problem of single machine failure [2] Solve the problem of single machine pressure

4.1 Master-slave mode

Worthy and never worthy of the Lord: very simple.

Preparation: A linux service. Open three redis services----modify port----6380 [master] 6381 6382 [slave].

修改: 
1.port端口号
2.rdb文件名dump638X.rbd
3.关闭aof  

Start the above three redis services

redis-server redis6380.conf
redis-server redis6381.conf
redis-server redis6382.conf

 Enter the corresponding client

View the roles of each redis service: 

It is found that they are all master roles. How to assign the master-slave relationship:

Equipped with never equipped with master: slaveof master node ip master node port  

When the master node modifies data, the master node's data will be synchronized to all slave nodes.

The slave node can only perform read operations and cannot perform write operations.

If the master node goes down, will the slave node usurp the position? --- It will not automatically come up.

4.2 Sentry Mode

Due to the above master-slave mode, after the master node goes down, the slave node will not automatically take over, and write operations will not be possible during this period.

To solve the above problems:----Sentinel mode---->[1]Monitoring function[2]Fault recovery[3]Elect a master node

4.2.1 Monitoring function

4.2.2 Master node election

4.2.3 Starting Sentinel

 Modify the sentinel configuration:

Start sentinel mode: redis-sentinel sentinel.conf  

Test: Use shutdown to make the master node go down and observe the sentinel console.

If the original master comes back, follow the current master.  

5. Cluster mode

Regardless of master-slave mode or sentinel mode, there is always only one master node. If the write operation concurrency is high, it will inevitably lead to excessive pressure on the master node.

Real cluster:

 Storage principle:

There are 16384 hash slots built into the redis cluster. When a key-value needs to be placed in the redis cluster, redis first uses the crc16 algorithm to calculate an integer result for the key, and then calculates the remainder of the result to 16384, so that each key will correspond A hash slot numbered between 0-16383, redis will map the hash slot to different nodes roughly equally according to the number of nodes.

When you add a Key to Redis Cluster, it will be calculated based on crc16(key) mod 16384 which hash slot the key should be distributed to. There will be many keys and values ​​in a hash slot. You can understand it as table partition. When using redis on a single node, there is only one table, and all keys are placed in this table. After switching to Redis Cluster, 16384 partition tables will be automatically generated for you. When you insert data, it will be based on The above simple algorithm determines which partition your key should exist in. There are many keys in each partition.

Build three masters and three slaves:
7001~7006: 6 redis services.
Create a folder cluster to store 6 redis configuration files

Modify 6 redis configuration files - the redis service must be empty

(1)Port number

(2) bind---any

(2) The name of the dump file

(3) AOF mode must be turned on and the file name must be modified

(4) Turn on cluster mode

Start the above 6 redis services

redis-server redisXXX.conf

 Slot, and set the master-slave relationship.

redis-cli --cluster create --cluster-replicas 1 192.168.223.147:7001 192.168.223.147:7002 192.168.223.147:7003 192.168.223.147:7004 192.168.223.147:70 05 192.168.223.147:7006 ------- ---- Set your own IP

access:

redis-cli -c -h 192.168.223.155 -p 7001

Guess you like

Origin blog.csdn.net/yhl15736773842/article/details/131502282
Recommended