(Redis): Persistent RDB

table of Contents

Introduction to persistence

RDB startup method

RDB startup mode-save command

RDB startup mode-save command related configuration

RDB startup mode-how the save command works

RDB startup mode-bgsave instruction

RDB startup mode-working principle of bgsave instruction

RDB startup mode-bgsave command related configuration

RDB startup mode-save configuration

RDB startup mode-save configuration principle

RDB startup method comparison

RDB special startup form

RDB advantages and disadvantages

Introduction to persistence

  • What is persistence

    • The working mechanism of using permanent storage media to save data and restoring the saved data at a specific time is called persistence.
  • Why persist
    • Prevent accidental loss of data and ensure data security
  • What is saved in the persistence process
    • Save the current data state in the form of a snapshot, store the data results, the storage format is simple, and the focus is on the data
    • Save the operation process of the data, log format, storage operation process, storage format is complex, the focus is on the data operation process

  • Data (snapshot) RDB

  • Process (log) AOF

RDB startup method

  • Who, when and what
  • Command execution
    • Who: redis operator (user)
    • When: Immediately (at any time)
    • What to do: save data

RDB startup mode-save command

save
  • Role: Manually perform a save operation

Example

  • save save data

  • View the rdb file in the data storage file directory

RDB startup mode-save command related configuration

dbfilename dump.rdb
  • Description: Set the local database file name , the default value is dump.rdb
  • Experience: usually set to dump-port number.rdb
to you
  • Description: Set the path for storing .rdb files
  • Experience: usually set to a directory with larger storage space, the directory name data
rdbcompression yes
  • Description: Set whether to compress data when storing to the local database , the default is yes, using LZF compression
  • Experience: usually the default is on , if set to no, it can save CPU running time, but it will make the stored files larger (huge)
rdbchecksum yes
  • Description: Set whether to verify the format of the RDB file . The verification process is performed both in the process of writing and reading files
  • Experience: Usually the default is on . If it is set to no, it can save about 10% of the time consumption of the read-write process, but there is a certain risk of data corruption when storing

Example

  • Restart redis after setting

Verify recovered data

  • Shut down the redis server

  • Start the server again

  • Use the client to view the data before the server restarts

RDB startup mode-how the save command works

  • Note: The execution of the save command will block the current Redis server until the current RDB process is completed, which may cause long-term blocking. It is not recommended to use it in an online environment.

RDB startup mode-bgsave instruction

bgsave
  • Function: manually start the background save operation, but not immediately

RDB startup mode-working principle of bgsave instruction

  • Note: The bgsave command is optimized for the save blocking problem.
    • All RDB operations involved in Redis use the bgsave method, and the save command can be abandoned
  • View the information returned after the successful execution of the bgsave command in the log

RDB startup mode-bgsave command related configuration

stop-writes-on-bgsave-error yes
Note: If there is an error in the background storage process, whether to stop the save operation
Experience: usually the default is on
What should I do if I forget to execute the save command repeatedly? I don’t know how many changes have been made to the data and when to save it?
  • Automatic execution
    • Who: the redis server initiates the instruction (based on conditions)
    • When: meet the conditions
    • What to do: save data

RDB startup mode-save configuration

  • Configuration
save second changes
effect
  • When the number of changes in the key within the limited time range reaches the specified number, it will be persisted
parameter
  • second: monitoring time range
  • changes: monitor key changes
position
  • Configure in the conf file

Example

RDB startup mode-save configuration principle

  • note:
  • The save configuration should be set according to actual business conditions. If the frequency is too high or too low, performance problems will occur, and the result may be catastrophic
  • In the save configuration, the second and changes settings usually have a complementary relationship, try not to set it as an inclusive relationship
  • After the save configuration is started, the bgsave operation is performed

RDB startup method comparison

RDB special startup form

  • Full copy
    • Explain in detail in master-slave replication
  • Restart while the server is running
debug reload
  • Specify save data when shutting down the server
shutdown save
  • When the shutdown command is executed by default, bgsave is automatically executed (if the AOF persistence function is not turned on)

RDB advantages and disadvantages

RDB advantages
  • RDB is a compact and compressed binary file with high storage efficiency
  • RDB internally stores redis data snapshots at a certain point in time , which is very suitable for data backup, full copy and other scenarios
  • RDB recovers data much faster than AOF
  • Application: Perform bgsave backup every X hours in the server, and copy RDB files to remote machines for disaster recovery.
Rdb disadvantages
  • The RDB method is unable to achieve real-time persistence, whether it is executing instructions or using configuration, and has a greater possibility of data loss
  • Each time the bgsave instruction is run, a fork operation is executed to create a child process, and some performance is sacrificed
  • Among the many versions of Redis, the RDB file format has not been unified. It is possible that the data format between various versions of services may not be compatible.

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/108936459