1. Introduction to RDB
RDB is a method used by Redis for persistence. It writes a snapshot of the data set in the current memory to the disk, that is, a Snapshot snapshot (all key-value pairs in the database). data). During recovery, the snapshot file is read directly into memory.
2. Triggering method
RDB has two triggering methods, namely automatic triggering and manual triggering.
①. Automatic triggering
Under SNAPSHOTTING in the redis.conf configuration file, we have introduced it in this article.
①.save:This is used to configure the RDB persistence conditions that trigger Redis, that is, when to save the data in the memory to the hard disk. . For example "save m n". Indicates that when the data set has been modified n times within m seconds, bgsave is automatically triggered (this command will be introduced below, and the command to manually trigger RDB persistence)
The default configuration is as follows:
save 900 1:表示900 秒内如果至少有 1 个 key 的值变化,则保存 save 300 10:表示300 秒内如果至少有 10 个 key 的值变化,则保存 save 60 10000:表示60 秒内如果至少有 10000 个 key 的值变化,则保存
Of course, if you If you are just using the caching function of Redis and do not need persistence, you can comment out all save lines to disable the saving function. You can use save "" to disable
②, stop-writes-on-bgsave-error: The default value is yes. When RDB is enabled and the last background save of data fails, whether Redis stops receiving data. This would make the user aware that the data was not persisted to disk correctly, otherwise no one would notice that a disaster had occurred. If Redis restarts, it can start receiving data again
③, rdbcompression;The default value is yes. For snapshots stored on disk, you can set whether to compress them for storage. If so, redis will use the LZF algorithm for compression. If you don't want to consume CPU for compression, you can set this feature to off, but the snapshots stored on disk will be larger.
④, rdbchecksum: The default value is yes. After storing the snapshot, we can also let redis use the CRC64 algorithm for data verification, but this will increase performance consumption by about 10%. If you want to get the maximum performance improvement, you can turn off this function.
⑤、dbfilename:Set the file name of the snapshot, the default is dump.rdb
⑥、dir:Set the storage path of the snapshot file, This configuration item must be a directory, not a file name. The default is to save it in the same directory as the current configuration file.
That is to say, through the save method configured in the configuration file, when the actual operation meets the configuration form, RDB persistence will be performed, and the current memory snapshot will be saved in the directory configured by dir. The file name is Determined by the configured dbfilename.
②. Manual trigger
There are two commands to manually trigger Redis for RDB persistence:
1. save
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.
Obviously, this command will cause long-term blocking for instances with relatively large memory. This is a fatal flaw. In order to solve this problem, Redis provides a second way.
2. bgsave
When executing this command, Redis will perform snapshot operations asynchronously in the background, and the snapshot can also respond to client requests. When the Redis process performs a fork operation to create a child process, the child process will be responsible for executing the RDB persistence process and automatically terminate after completion. Blocking only occurs during the fork phase and is generally very short-lived.
Basically all RDB operations inside Redis use the bgsave command.
ps: Executing the flushall command will also generate the dump.rdb file, but it is empty and meaningless
3. Restore data
Move the backup file (dump.rdb) to the redis installation directory and start the service. Redis will automatically load the file data into memory. The Redis server will block during loading of the RDB file until the loading work is completed.
To obtain the installation directory of redis, you can use the config get dir command
4. Stop RDB persistence
In some cases, we only If you want to take advantage of the caching function of Redis, it is not like using the persistence function of Redis, so we'd better stop RDB persistence at this time. You can disable the save function by commenting out all the save lines in the configuration file redis.conf as mentioned above, or by directly using an empty string to disable it: save ""
You can also pass the command:
1 |
|
5. Advantages and Disadvantages of RDB
①、Advantages
The Redis data set is saved in an RDB file at a specific point in time. This file is very compact. This file is ideal for backup and disaster recovery.
2. When generating an RDB file, the redis main process will fork() a child process to handle all saving work. The main process does not need to perform any disk IO operations.
3.RDB is faster than AOF when restoring large data sets.
②、Disadvantages
1. RDB data cannot achieve real-time persistence/second-level persistence. Because every time bgsave runs, it must perform a fork operation to create a child process, which is a heavyweight operation (the data in the memory is cloned, and roughly 2 times the expansion needs to be considered). Frequent execution costs are too high (affecting performance)
2. RDB files are saved in a specific binary format. During the evolution of the Redis version, there are multiple formats of RDB versions. There is a problem that the old version of the Redis service is not compatible with the new version of the RDB format (version incompatibility)
3. Make a backup at a certain interval, so if redis accidentally goes down, all modifications after the last snapshot will be lost (data will be lost)
6. The principle of RDB automatic saving
Redis has a server status structure:
1 2 3 4 5 6 7 8 9 |
|
① First, look at the saveparam array that records the save conditions. Each element in it is A saveparams structure:
1 2 3 4 5 6 |
|
Previously we configured save in the redis.conf configuration file:
1 2 3 |
|
Then the saveparam array in the server status will be It looks like this:
②、Dirty counter and lastsave attribute
The dirty counter records the distance since the last successful execution of the save command or bgsave command. The Redis server performs How many times has it been modified (including writing, deleting, updating, etc.).
The lastsave attribute is a timestamp that records the last time the save command or bgsave command was successfully executed.
Through these two commands, when the server successfully performs a modification operation, the dirty counter will be incremented by 1, and the lastsave attribute records the time when save or bgsave was last executed. The Redis server also has a periodic operation function. severCron is executed every 100 milliseconds by default. This function will traverse and check all save conditions in the saveparams array. As long as one condition is met, the bgsave command will be executed.
After the execution is completed, the dirty counter is updated to 0, and lastsave is also updated to the completion time of the executed command.
The above is the detailed content of Example analysis of RDB persistence in Redis. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Redis and SQL databases is that Redis is an in-memory database, suitable for high performance and flexibility requirements; SQL database is a relational database, suitable for complex queries and data consistency requirements. Specifically, 1) Redis provides high-speed data access and caching services, supports multiple data types, suitable for caching and real-time data processing; 2) SQL database manages data through a table structure, supports complex queries and transaction processing, and is suitable for scenarios such as e-commerce and financial systems that require data consistency.

Redisactsasbothadatastoreandaservice.1)Asadatastore,itusesin-memorystorageforfastoperations,supportingvariousdatastructureslikekey-valuepairsandsortedsets.2)Asaservice,itprovidesfunctionalitieslikepub/submessagingandLuascriptingforcomplexoperationsan

Compared with other databases, Redis has the following unique advantages: 1) extremely fast speed, and read and write operations are usually at the microsecond level; 2) supports rich data structures and operations; 3) flexible usage scenarios such as caches, counters and publish subscriptions. When choosing Redis or other databases, it depends on the specific needs and scenarios. Redis performs well in high-performance and low-latency applications.

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools
