Analysis of Redis Principle

1. Introduction to Redis

Redis is an open source, network-based, high-performance key-value database that makes up for the shortcomings of key-value storage such as memcached. In some cases, it can play a very good supplementary role to the relational database to meet real-time high concurrency. need.
Redis is similar to memcached, but data can be persisted and supports a wide range of data types. It supports calculating the union, intersection and complement (difference) of sets on the server side, and also supports a variety of sorting functions.

Note: The network data transmission between the Redis client and the server is not encrypted. It is recommended not to use Redis to access sensitive data, otherwise there may be security risks.

2. Redis structure

Redis includes Redis Server and Redis-WS, as shown in the figure below
Insert image description here

  • Redis Server: The core module of the Redis component, responsible for data reading and writing, data persistence, master-slave replication, and cluster functions of the Redis protocol.
  • Redis-WS: Redis WebService management module, mainly responsible for the creation, expansion, reduction, query, deletion and other operations of Redis cluster. The cluster management information is stored in the DB database.

3. Redis principle

Redis persistence

Redis provides multiple different levels of persistence methods such as RDB and AOF.

  • RDB persistence
    can generate point-in-time snapshots of the data set within a specified time interval.

  • AOF persistence
    records all write operation commands executed by the server and restores the data set by re-executing these commands when the server starts. All commands in the AOF file are saved in the Redis protocol format, and new commands will be appended to the end of the file. Redis can also rewrite the AOF file in the background so that the size of the AOF file does not exceed the actual size required to save the data set state.

Redis can use AOF persistence and RDB persistence at the same time. In this case, when Redis restarts, it will give priority to using the AOF file to restore the data set, because the data set saved by the AOF file is usually more complete than the data set saved by the RDB file. Users can also turn off the persistence function, so that the data only exists when the server is running.

Redis running mode

Redis instances can be deployed on one or more nodes, and one or more Redis instances can also be deployed on a node (the number of Redis instances on each node is calculated by the software based on the node hardware resources).

The latest version of Redis supports the cluster function, which can combine multiple Redis instances into a Redis cluster to provide a distributed key-value database to the outside world. The cluster shares data through sharding and provides replication and failover functions.

  • Single instance mode
    The logical deployment method of single instance mode is as shown in the figure below:
    Insert image description here
    Description:

  • A master instance (master) can correspond to multiple slave instances (slave), and the slave instance itself can also be connected to slave instances.

  • Command requests sent to the master instance will be synchronized by the master instance to the slave instance for processing in real time.

  • If the master instance is down, the slave instance will not be promoted to master automatically.

  • The slave instance is read-only by default. When "slave-read-only" is configured as no, the slave instance can also be written. However, after the slave instance is restarted, the data will be synchronized from the master instance, and the data previously written to the slave instance will be lost.

  • Compared with the structure in which all slave instances are directly connected to the master instance, the multi-level slave instance structure reduces the number of slave instances that need to be directly synchronized by the master instance, which can improve the business processing performance of the master instance to a certain extent.

  • Cluster mode
    The logical deployment method of cluster mode is shown in the figure below:
    Insert image description here
    Description:

  • Multiple Redis instances are combined into a Redis cluster, with a total of 16384 slots evenly allocated to each master instance.

  • Each instance in the cluster records the mapping relationship between slots and instances, and the client also records the mapping between slots and instances. The client performs hash calculation according to the key, and then performs a modulo operation with 16384 to obtain the slot value. According to the slot-instance mapping, the message is directly sent to the corresponding instance for processing.

  • By default, the slave instance cannot read or write. Executing the readonly command online can make the slave instance readable.

  • If the master instance fails, the remaining master instances in the cluster will elect a slave instance to be promoted to the master. More than half of the master instances must be in "good" running status to be elected.

  • The cluster-require-full-coverage configuration item indicates whether the cluster requires full coverage. If it is configured to yes, then when one of the masters and slaves fails, the cluster status is faulty and the entire cluster cannot process commands; if it is configured to no, more than half of the masters and slaves fail. The instance running status is "Good" and the cluster status is "Good".

  • The Redis cluster can be expanded and reduced (new instances are added to the cluster or Redis instances are removed from the cluster), and slot migration is performed.

四、Redis-Data-Sync

Redis-Data-Sync is a data synchronization tool between primary and secondary cluster Redis services. This tool synchronizes data in the logical cluster from the primary cluster to the backup cluster in real time, and provides a data copy of the Redis service in the primary cluster on the backup cluster.

Guess you like

Origin blog.csdn.net/weixin_43114209/article/details/132401778