Redis knowledge points summary

One, what is redis

Redis is a storage system that supports multiple data structures such as Key-Value. It can be used in scenarios such as caching, event publishing or subscription, and high-speed queues. The database is written in ANSI C language, supports the network, provides direct access to strings, hashes, lists, queues, and collection structures, based on memory, and can be persisted.

Redis is a non-relational database (not-only-sql or nosql). It stores data in key-value pairs and stores the data in memory. The access speed is fast, but the support for persistence is not good enough, so redis Generally used in conjunction with relational databases, redis can be used as a distributed cache, used in the case of large data volume and high concurrency. Redis operates through many commands, and redis is not suitable for storing large data.

Two, what are the application scenarios of redis

  1. Session cache (most commonly used);
  2. message queue;
  3. Activity leaderboard or count;
  4. Publish, subscribe to news (message notification);
  5. Product list, review list, etc.

Three, redis data type

Redis supports a total of five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set ordered collection).

  1. String (string)

It is the most basic data type of redis. A key corresponds to a value. It should be noted that the maximum storage of a key value is 512MB.
Use scenarios: regular key-value caching applications, generally used for regular counting: Weibo number, fan number

  1. hash

Hash is a collection of key-value pairs, a mapping table of field and value of string type, suitable for storing objects

  1. table

It is a simple string list of redis, which is sorted by insertion order.
Usage scenario: It can easily realize the latest news ranking and other functions. Another application is the message queue

4. Collection

It is an unordered collection of string type and cannot be reused
. The Set data structure provided by Redis can store some collective data. In the Weibo application, all the followers of a user can be stored in a collection, and all its fans can be stored in a collection. Redis also provides operations such as intersection, union, and difference for collections, which can easily implement functions such as common attention, common preference, and second-degree friends. For all the above collection operations, you can also use different command options. Return the result to the client or save it to a new collection

  1. sorted set

Compared with Set, Sorted Set adds a weight parameter score, so that the elements in the set can be ordered according to score and inserted in order, that is, automatic sorting.
Usage scenario: The set value can be the student ID of the classmate, and the score can be the test score, so that when the data is inserted into the set, the natural sorting is already performed, or the Sorted Set is used as a weighted queue, such as ordinary The score of the message is 1, the score of the important message is 2, and then the worker thread can choose to get the work tasks in the reverse order of the score, so that the effect of giving priority to the important tasks is achieved.

Four, redis service related commands

slect#Select the database (database number 0-15)
exit#Exit the connection
information#Get service information and statistics
monitor#Real-time monitoring
config get#Get service configuration
flushdb#Delete the key in the currently selected database
flushall#Delete all the databases key

Five, redis publishing and subscription

Redis publish and subscribe (publish/subscribe) is a message communication mode in which one party sends information and the other receives information.
The picture below shows three clients subscribing to the same channel at the same time

Six, redis publishing and subscription

Redis publish and subscribe (publish/subscribe) is a message communication mode in which one party sends information and the other receives information.
The picture below shows three clients subscribing to the same channel at the same time

Seven, redis persistence

There are two ways of redis persistence: snapshot (snapshot), attached file only (AOF)

Snapshot (RDB)

In the form of snapshots, the data at the current moment in the memory is periodically saved to disk. The persistence scheme supported by Redis by default. Fast speed, but some data will be lost when the server is powered off

  1. Write the data stored in the memory into a binary file in a snapshot mode, such as the default dump.rdb
  2. If more than 1 Key is modified within 900 seconds, the snapshot will be saved
  3. If more than 10 keys are modified within 300 seconds, the snapshot will be saved
  4. If more than 10,000 key points are modified within 60 seconds, the snapshot will be saved

Additional file only (AOF)

append only
file. Add, delete and modify all commands for redis database operations. Save to file. When the database is restored, execute all the commands once. The two persistence schemes simultaneously enable the use of AOF files to restore the database. Data integrity can be guaranteed, but the speed is slow

  1. When using AOF persistence, the service will append each received write command to the file through the write function (appendonly.aof)
  2. AOF persistent storage method parameter description
appendonly yes #开启AOF持久化存储方式 
appendfsync always #收到写命令后就立即写入磁盘,效率最差,效果最好
appendfsync everysec  #每秒写入磁盘一次,效率与效果居中
appendfsync no #完全依赖操作系统,效率最佳,效果没法保证

Why is redis single thread so fast

Redis is divided into client and server. A complete redis request event has multiple stages (client-to-server network connection -> redis read and write events occur -> redis server data processing (single thread) -> data return). The redis single-threaded model usually refers to the data processing of the server. The client and the server use socket communication. The socket server monitor can accept multiple client requests at the same time. That is to say, the redis service faces multiple The redis client connection requests, and the redis service itself runs in a single thread.

The core of redis is that if all my data is in memory, my single-threaded operation is the most efficient. Why? Because the essence of multi-threading is the situation where the CPU simulates multiple threads. There is one such simulated situation. The cost is context switching. For a memory system, it is the most efficient to switch without context. Redis binds a piece of memory data with a single CPU, and then reads and writes the data in this memory multiple times, all of which are done on one CPU, so it is a single thread to handle this matter. In the case of memory, this solution is the best solution. The single-threaded method cannot play the multi-core CPU performance. In order to make full use of the multi-core CPU, multiple instances (ie multiple redis processes) are often started on one server. In order to reduce the overhead of switching, it is necessary to specify the CPU it is running on for each instance (redis process) and because redis is single-threaded, there is no need to consider various lock issues, there is no lock release operation, no Performance consumption due to possible deadlock.

Summary: CPU is not the bottleneck of Redis. The bottleneck of Redis is most likely the size of machine memory or network bandwidth. Since single-threaded is easy to implement, and the CPU will not become a bottleneck, it is logical to adopt a single-threaded solution

Solve the downtime of the redis master-slave structure.
If there is a downtime in the master-slave replication architecture, you need to look at it separately:

Downtime from Redis

a) This is relatively simple. After the slave library is restarted in Redis, it will automatically be added to the master-slave architecture to automatically complete data synchronization;

b) The problem? If the main library does not change much while the slave library is disconnected, after the slave library is restarted, will the main library still perform RDB operations on all data? Or incremental update? (Under the premise of persistence from the library)

No, because it has been implemented after Redis 2.8, incremental replication can be realized when the master and slave are restored after disconnection.

Main Redis down

Method 1: Manual recovery

  1. Execute the SLAVEOFNO ONE command in the slave database, disconnect the master-slave relationship and promote the slave database to the master database to continue serving
  2. After restarting the main library, execute the SLAVEOF command to set it as a slave library of other libraries, then the data can be updated back

Method 2: Automatic recovery of sentinel function. The
basic principle is: heartbeat mechanism + voting decision

  1. After starting redis in sentinel mode, it automatically monitors the running status of master/slave, which has been integrated in the redis2.4+ version
  2. If the Master is abnormal, a Master-Slave switch will be performed, one of the Slaves will be used as the Master, and the previous Master will be used as the Slave

Guess you like

Origin blog.csdn.net/weixin_46687295/article/details/108555256