当前位置:网站首页>Introduction to redis I: redis practical reading notes
Introduction to redis I: redis practical reading notes
2022-07-28 20:47:00 【51CTO】
Redis Provide 5 Different types of data structures , And store keys and 5 Mapping between two different types of values , All kinds of problems can be naturally mapped to these data structures . By copying 、 Features such as persistence and client fragmentation , You can persist key value pairs stored in memory to the hard disk , You can use the replication feature to extend read performance , You can also use client slicing to extend write performance , Users can easily put Redis Expand to include hundreds of GB data 、 Systems processing millions of requests per second .( Slicing is a way of dividing data into parts , The partition of data can be based on the... Contained by the key ID、 Key based hash values , Or based on some combination of the above two . By slicing the data , Users can store data in multiple machines , You can also get data from multiple machines , This method can achieve linear level performance improvement when solving some problems .)

Redis There are two different forms of persistence , They can write data stored in memory to the hard disk in a small and compact format : The first persistence method is a point-in-time dump (point-in-time dump), The dump operation is available in both “ A specified number of writes are executed within a specified time period ” This condition is implemented when it is met , You can also dump to the hard disk by calling two (dump-to-disk) Any one of the commands to execute ; The second persistence method writes all the commands of the modified database to a append only (append-only) In the document , Users can decide how important the data is , Set only append write to unsync 、 Synchronize once per second or every time a command is written . In order to extend the Redis Read performance of , And for Redis Provide failover support ,Redis The master-slave replication feature is realized : The slave server performing the replication connects to the master server , Receives an initial copy of the entire database sent by the master server ; The write command executed by the primary server , Will be sent to all connected slave servers for execution , To update the data set from the server in real time . Because the data contained from the server is constantly updated , So the client can send a read request to either of the slave servers , To avoid centralized access to the primary server .
Redis data structure
STRING( character string ) LIST( list ) SET( aggregate ) HASH( hash ) ZSET( Ordered set ), At the same time, a bitmap is created based on the string (Bitmaps) and HyperLogLog, And with LBS(Location Based Service, Location based services ) Continuous development ,Redis3.2 Add relevant information to the version GEO( Location of geographic information ) The function of .

Redis String in


set、get and del Use example of :

A value of type string can actually be a string ( Simple string 、 Complex string ( for example JSON、XML)、 Numbers ( Integers 、 Floating point numbers ))、 Even binary ( picture 、 Audio 、 video ) But the maximum value cannot exceed 512MB.

Redis List in

LPUSH and RPUSH Commands are used to push elements to the left of the list (left end) And the right end (right end);LPOP and RPOP The command is used to pop an element from the left and right end of the list, respectively ;LINDEX The command is used to get an element of the list at a given location ;LRANGE The command is used to get all the elements of a list on a given scope .




Redis The set in

A list can store multiple identical strings , The set ensures that each string stored by itself is different by using hash table ( These hash tables only have keys , But there is no value associated with the key ). because Redis The collection of uses an unordered way to store elements , So users can't use lists like that , Push elements into one end of the collection , Or pop an element from one end of the collection . But users can use it SADD The command adds an element to the collection , Or use SREM The command removes an element from the collection . In addition, you can also use SISMEMBER The command quickly checks to see if an element already exists in the collection , Or use SMEMBERS The command gets all the elements that the collection contains .




Redis Hash in

Redis Can store mappings between multiple key-value pairs . Just like a string , The value stored in the hash can be either a string or a numeric value , And the user can also self-increment or self-decrement numeric values stored in the hash .




Redis Ordered set in
An ordered set is the same as a hash , Both are used to store key-value pairs : The keys of an ordered set are called members (member), Each member is different ; The value of an ordered set is called a score (score), Score must be floating point . An ordered set is Redis The only element can be accessed according to members ( This is the same thing as a hash ), You can also access the structure of the elements according to their points and the order in which they are arranged .





Publish and subscribe
Publish and subscribe ( also called pub/sub) Is characterized by subscribers (listener) Subscribe to the channel (channel), sender (publisher) Responsible for sending binary string messages to channels (binary string message). Whenever a message is sent to a given channel , All subscribers to the channel will receive messages .


Redis Yes 5 A command allows the user to perform operations on multiple keys without being interrupted , They are WATCH、MULTI、EXEC、UNWATCH and DISCARD.
Redis The basic business of (basic transaction) Need to use MULTI Command and EXEC command , This transaction allows one client to execute multiple commands without being interrupted by other clients . stay Redis Inside , By MULT Command and EXEC All the names surrounded by the command will be executed one by one , Until all the commands are executed . When a transaction is completed ,Redis Will handle commands from other clients . To be in Redis Inside the execution of affairs , The first thing we need to do is execute MULTI command , Then enter the commands we want to execute in the transaction , Finally, we will carry out EXE command . When Redis Received... From a client MULTI On command ,Redis All the commands sent by the client will be put into a queue , Until this client sends EXEC It's up to the order , then Redis Without being interrupted , One by one, execute the commands stored in the queue .
In user use WATCH After the command monitors the key , Until the user performs EXEC During the period of command , If any other client replaces any monitored key first 、 Update or delete etc , So when the user tries to execute EXEC When ordered , The transaction will fail with an error . By using WATCH、MULTI/EXEC、UNWATCH/DISCARD Wait for the order , When the program can perform some important operations , Avoid data errors by ensuring that the data you are using has not changed .
Key expiration time : In the use of Redis When storing data , Some data may no longer be useful after a certain point in time , Users can use DEL The command explicitly removes the useless data , It can also be done through Redis The expiration time of (expiration) Feature to make a key automatically deleted after a given time limit .

Distributed lock
Generally speaking , On the data “ Lock ” when , The program first needs to obtain the ability of exclusive access to data by acquiring locks , Then you can perform a series of operations on the data , Finally, release the lock to other programs .Redis Use WATCH Command instead of locking data , because WATCH The client that executes this command will only be notified if the data is modified by other clients first , It will not prevent other clients from modifying the data , So this command is called Optimism lock . Distributed locks also have a similar method of acquiring locks first , And then perform the operation , Finally, release the lock , But this kind of lock is not used by multiple threads in the same process , Nor is it for multiple processes on the same machine , It is By different machines Redis The client obtains and releases .
Coarse grained locks and fine-grained locks
Redis Two different persistence methods are provided to store data on the hard disk . One method is called snapshot (snapshotting), It can write all the data existing in a certain time into the hard disk . Another way is to append files only (append-only file, AOF), It will execute the write command , Copy the executed write command to the hard disk .Redis You can create a snapshot to get a copy of the data stored in memory at a certain point in time . After creating the snapshot , Users can back up the snapshot , You can replicate the snapshot to another server to create a server copy with the same data , You can also leave the snapshot in place for use when restarting the server .
There are several ways to create snapshots :

AOF Persistence writes the executed write command to AOF End of file , To record changes in data . therefore ,Redis Just do it again from the beginning to the end AOF All write commands contained in the file , You can recover AOF The data set recorded by the file . With Redis Keep running ,AOF The volume of the file will continue to grow , In extreme cases , Growing in size AOF Files may even run out of all available space on your hardware . Another problem is , because Redis After restart, you need to re execute AOF File records all write commands to restore the dataset , So if AOF The volume of the file is very large , Then the execution time of the restore operation may be very long . In order to solve AOF The problem of increasing file size , Users can Redis send out BGREWRITEAOF command , This command will remove AOF Redundant commands in files to override AOF file , send AOF The volume of the file becomes as small as possible . With snapshot persistence, you can set save Option to automate BGSAVE equally ,AOF Persistence can also be set by auto-aof-rewrite-percentage Options and auto-aof-rewrite-min-size Option to automate BGREWRITEAOF.
Replication allows other servers to have a constantly updated copy of data , Thus, the server with the data copy can be used to process the read request sent by the client . Relational databases typically use a master server (master) To multiple slave servers (slave) Send updates , And use the slave server to handle all read requests .Redis It also adopts the same method to realize its own replication characteristics , And use it as a way to scale performance .


Redis Master master replication is not supported (master-master replication), because Redis Allow users to use after the server starts SLAVEOF Command to set the slave server (slaving options), Therefore, some readers may mistakenly think that two Redis Instances are set as the primary servers of each other to realize multi master replication (multi-master replication), It is even possible to set multiple instances as the master server in a loop . Unfortunately , It's not gonna work : Two servers that are set up as the main server each other Redis The instance will only continuously consume a lot of processor resources and continuously try to communicate with each other , According to the different servers connected by the client , The request of the client may get inconsistent data , Or no data at all .

Redis from 2.8 Version officially provides a high availability implementation Redis Sentinel, It can guarantee Redis Node fault detection and automatic fault transfer .Redis from 3.0 The version officially provides a distributed implementation Redis Cluster, It is Redis True distributed implementation , Provides high availability 、 Read write and capacity scalability .
Reference resources :
Redis actual combat
http://try.redis.io/ https://redis.io/ http://redis.cn/
边栏推荐
- Explain rigid body and collider components in unity
- C# 委托 delegate 的理解
- Extract China map from global.Nc data and calculate regional CO2 value based on acgis
- One article makes you understand what typescript is
- Beautiful blue background form input box style
- 企业如何成功完成云迁移?
- Yum package management
- Yyds dry inventory interview must brush top101: every k nodes in the linked list are turned over
- Network shell
- Three deletion strategies and eviction algorithm of redis
猜你喜欢
![[POC - proof of concept]](/img/57/0916e3711b27e2debfbdb9c9cb9713.png)
[POC - proof of concept]

The product power is greatly improved, and the new Ford Explorer is released
![Linxu [basic instructions]](/img/94/98d7b2cb4a72c6437a9f604ec5da9d.png)
Linxu [basic instructions]
![[task01: getting familiar with database and SQL]](/img/de/c1370461d8c2561c4dfd0ff5c7e830.png)
[task01: getting familiar with database and SQL]

UE4 3dui widget translucent rendering blur and ghosting problems

How to balance security and performance in SQL?

Use of DDR3 (axi4) in Xilinx vivado (1) create an IP core

Network shell

One article makes you understand what typescript is

Read JSON configuration file to realize data-driven testing
随机推荐
Network shell
js图表散点图例子
【ADB常用命令及其用法大全(来自[醒不了的星期八]的全面总结)】
Speech controlled robot based on ROS (I): realization of basic functions
EasyNLP中文文图生成模型带你秒变艺术家
TCP.IP
JS chart scatter example
Classes and objects (medium)
漂亮的蓝色背景表单输入框样式
[pytorch] LSTM neural network
Redis入门二:redhat 6.5安装使用
Three deletion strategies and eviction algorithm of redis
NAT实验演示(Huawei交换机设备配置)
User and group and authority management
Clock distribution of jesd204 IP core (ultrascale Series)
Scheduled backup of MySQL database under Windows system
【1331. 数组序号转换】
平均海拔4000米!我们在世界屋脊建了一朵云
Mongoose condition queries part of the data of the specified attribute
prometheus配置alertmanager完整过程