In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Redis database persistence
I. Overview of the database
Second, the function and realization of persistence
3. Configuration of RDB persistence mode
4. Configuration of AOF persistence mode
I. Overview of the database:
Redis is an advanced key-value database. It is similar to memcached, except that data can be persisted and supports a wide range of data types. There are strings, linked lists, sets and ordered collections. Support in the server-side calculation of the collection union, intersection and complement set (difference), etc., but also supports a variety of sorting functions. So Redis can also be seen as a data structure server.
Second, the function and realization of persistence:
Function:
All data in Redis is stored in memory. If persistence is not configured, all data will be lost after redis restart, so you need to enable the persistence feature of redis to save the data to disk. When redis restarts, you can recover the data from disk. Then save it to disk asynchronously from time to time (semi-persistent mode); you can also write every data change to an append only file (full persistence mode). If two persistence modes are enabled in the server, AOF persistence mode is implemented by default
Implementation method:
RDB persistence: periodically dump Reids database records in memory to disk, similar to the snapshot function.
AOF persistence: the principle of append only file-- is to write the operation log of Reids to a file in an additional way, which is similar to real-time.
The difference between the two:
RDB persistence means that the snapshot of the dataset in memory is written to disk within a specified time interval. The actual operation is a child process of fork, which first writes the dataset to a temporary file, and then replaces the previous file and stores it with binary compression.
AOF persistence records every write and delete operation processed by the server in the form of a log. Query operations are not recorded, but are recorded in the form of text. You can open a file to see a detailed operation record.
Advantages of RDB:
1)。 Once you use this approach, your entire Redis database will contain only one file, which is perfect for file backup. For example, you might plan to archive data for the last 24 hours every hour, as well as data for the last 30 days once a day. Through this backup strategy, in the event of a catastrophic failure of the system, we can recover very easily.
2)。 RDB is a very good choice for disaster recovery. Because we can easily compress a single file and then transfer it to other storage media.
3)。 Maximize performance. For the Redis service process, at the beginning of persistence, the only thing it needs to do is to fork the child process, and then the child process completes these persistence work, which can greatly prevent the service master process from performing IO operations.
4)。 Compared with the AOF mechanism, if the dataset is large, the RDB startup efficiency will be higher.
RDB's disadvantages:
1)。 If you want to ensure high availability of data, that is, to avoid data loss as much as possible, then RDB will not be a good choice. Because once the system downtime occurs during the scheduled persistence cycle, the data that has not been written to disk before will be lost.
2)。 Because RDB assists in data persistence through the help child process, if the dataset is large, it may cause the entire server to go out of service for hundreds of milliseconds, or even 1 second.
Advantages of AOF:
1)。 This mechanism can bring higher data security, that is, data persistence. Three synchronization strategies are provided in Redis, that is, synchronization per second, synchronization per modification, and async. In fact, synchronization per second is also done asynchronously, and its efficiency is also very high. The only difference is that once the system downtime occurs, then the modified data within this second will be lost. Every time we modify synchronization, we can think of it as synchronous persistence, that is, every data change that occurs is immediately recorded to disk. It can be predicted that this approach is the least efficient.
2)。 Because this mechanism uses append mode to write log files, even if there is a downtime in the writing process, it will not destroy the content that already exists in the log file. However, if we only write half the data this time and the system crashes, don't worry, we can use the redis-check-aof tool to help us solve the data consistency problem before the next startup of Redis.
3)。 If the log is too large, Redis can automatically enable the rewrite mechanism. That is, Redis constantly writes the modified data to the old disk file in append mode, and Redis also creates a new file to record which modification commands were executed during this period. Therefore, data security can be better guaranteed when rewrite switching is carried out.
4)。 AOF contains a clearly formatted, easy-to-understand log file for recording all changes. In fact, we can also complete the reconstruction of the data through this file.
AOF's disadvantages:
1)。 For the same number of datasets, AOF files are usually larger than RDB files. RDB recovers large datasets faster than AOF does.
2)。 According to the different synchronization strategies, if the amount of data is relatively large, the running efficiency of AOF is often slower than that of RDB.
Application scenarios:
1)。 Willing to sacrifice some performance (choose AOF)
2)。 In exchange for higher cache consistency to ensure the highest possible database integrity (select AOF)
3)。 When you are willing to write frequently, do not enable backup in exchange for higher performance. When you run save manually, do a backup (RDB).
3. Configuration of RDB persistence mode
Environment preparation: install the Redis service according to "Redis High performance Database"
[root@redis ~] # vi / usr/local/redis/redis.conf218 save 9001 # # enable the RDB snapshot function, and complete a key that is worth changing to trigger the snapshot function in 900s. If all save configuration items are commented out, the RDB snapshot function 219 save 10220 save 60 10000235 stop-writes-on-bgsave-error yes # # will be turned off when the RDB snapshot background process fails It does not affect whether the user has to write 241 rdbcompression yes # # whether to compress the RDB snapshot file. After closing, it will increase the performance 250 rdbchecksum no # # close the check check of the RDB snapshot file, increase the performance 253 dbfilename dump.rdb # # the name of the snapshot file 263 dir. / # # the storage path of the snapshot file . / under the installation directory for redis, [root@redis redis] # / etc/init.d/redis restart [root@redis redis] # redis-h 192.168.100.101-p 6379-a 123123192.168.100.101etc/init.d/redis restart 6379 > set heheOK192.168.100.101:6379 > set name 111OK192.168.100.101:6379 > keys * 1) "" 2) "name" 192.168.100.101Ze6379 > exit [root@redis redis] # reboot # # verify whether the key value still exists after restart [root@redis redis] # redis-h 192.168.100.101-p 6379-a 123123192.168.100.101keys * 1) "" 2) "name" 192.168.100.101keys 6379 > exit IV, Configuration of AOF persistence mode
[root@redis ~] # vi / usr/local/redis/redis.conf
218 # save 900 1
219 # save 300 10
220 # save 60 10000
672 appendonly yes # # enable AOF persistence
676 appendfilename "appendonly.aof" # # specify persistent files
702 appendfsync everysec # # frequency. The parameters are shown in Table 1 below.
724 no-appendfsync-on-rewrite no # # if set to yes, the commands executed by redis will be stored in the buffer until the system automatically synchronizes to the hard disk
743 auto-aof-rewrite-percentage 100 # # if the currently written AOF file reaches 100% of the size of the last rewrite file, the rewrite operation will be triggered
744 auto-aof-rewrite-min-size 64mb # # sets the minimum value for AOF persistent rewrite files, and triggers the rewrite operation when it reaches 60m and meets the 100% condition
The generation process of AOF file includes three steps: command appending, file writing and file synchronization.
When Redis turns on AOF persistence, Redis will trace the executed write command back to the end of the buffer inside Redis after executing a write command. This process is the append process of the command. Next, the write command for the buffer is written to the AOF file, which is the file writing process. For the operating system, calling the write function does not immediately write the data to the hard disk. In order to actually write the data to the hard disk, it is also necessary to call the fsync function. Calling the fsync function is the process of file synchronization. It is only after the file synchronization process that the AOF file actually saves the Redis write command on the hard disk. The appendfsync configuration option is used to configure how often write commands are synchronized to files, and the values of each option are shown in Table 1. The value of no indicates that the AOF file is written, but disk synchronization is not performed. Disk synchronization is performed according to the default of the linux system. The default is 30s, which is the most efficient.
[root@redis] # / etc/init.d/redis restart [root@redis] # redis-h 192.168.100.101-p 6379-a 123123192.168.100.101root@redis 6379 > keys * 1) "" 2) "name" 192.168.100.101root@redis 6379 > set benet cloudOK192.168.100.101:6379 > keys * 1) "benet" 2) "" 3) "name" 192.168.100.101 6379 > get benet "cloud" 192.168. 100.101key 6379 > exit [root@redis ~] # cat / root/appendonly.aof * 2 $6SELECT$10*3 $3set$4$11*3 $3set$4name$12*3 $3set$5benet$4accp [root@redis ~] # reboot # # verify whether key exists [root@redis ~] # / etc/init.d/redis restart [root@redis ~] # redis-h 192.168.100.101-p 6379-a 123123192.168.100.101 key 6379 > keys * 1) "name" 2) "" 3) "benet" 192.168.100.101 exit
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Https://www.cyberciti.biz/tips/identifying-linux-bottlenecks-sar-graphs-with-ksar.html
© 2024 shulou.com SLNews company. All rights reserved.