Redis tutorial of x: persistence details

  • 2020-05-06 11:58:15
  • OfStack

What persistence mechanisms does Redis provide

      1). RDB persistence:
      this mechanism refers to writing a snapshot of a data set in memory to disk at a specified time interval.      
      2). AOF persistence :
      this mechanism logs every write handled by the server, which is read at the beginning of Redis server startup to rebuild the database to ensure that the data in the database is complete after startup.
      3). No persistence:
We can configure       to disable persistence on Redis servers so that we can view Redis as an enhanced version of memcached.
      4). Both AOF and RDB should be used.
     
ii. Advantages and disadvantages of RDB mechanism:

What are the advantages of     RDB?

      1). Once you do this, your entire Redis database will contain only one file, which is perfect for backing up files. For example, you might want to archive the last 24 hours of data every hour and the last 30 days of data every day. With this backup strategy, it is easy to recover in the event of a catastrophic failure.
      2). RDB is a great option for disaster recovery. Because we can easily compress a single file and move it to another storage medium.
      3). Maximum performance. For the Redis service process, the only thing it needs to do at the beginning of the persistence is fork out of the child process, and then by the child process to complete the persistence work, so that the service process can greatly avoid IO operation.
      4). RDB starts more efficiently if the data set is large compared to the AOF mechanism.
     
What are the disadvantages of     RDB?

      Because once the system goes down before the scheduled persistence, any data that has not been written to disk before will be lost.
      2). Because RDB assists in data persistence through the fork child process, if the data set is large, the entire server may be out of service for hundreds of milliseconds, or even a second.
     
iii. Advantages and disadvantages of AOF mechanism:

What are the advantages of     AOF?

      1). This mechanism can lead to higher data security, namely data persistence. Three synchronization policies are provided in Redis, i.e., synchronization per second, synchronization per modification, and unsynchronization. In fact, synchronization per second is also done asynchronously, which is very efficient, except that if the system goes down, the modified data will be lost within a second. Each time we modify synchronization, we can think of it as synchronous persistence, in which every data change is immediately recorded to disk. Predictably, this approach is the least efficient. As for the lack of synchronization, needless to say, I think we can all understand it correctly.
      However, if we only write half of the data in this operation and the system crashes, don't worry. Before the next launch of Redis, we can use the redis-check-aof tool to help us solve the data consistency problem.
      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 modified commands were executed during this time. Therefore, data security can be better guaranteed when switching rewrite.
      4). AOF contains a well-formed, easy-to-understand log file for recording all changes. In fact, we can also reconstruct the data through this file.
     
What are the disadvantages of       AOF?
      1). AOF files are generally larger than RDB files for the same number of data sets.
      2). Depending on the synchronization strategy, AOF tends to run slower than RDB. Overall, the synchronization per second policy is relatively efficient, and the synchronization disable policy is as efficient as RDB.
     
Iv. Others:

    1. Snapshotting:

      by default, Redis takes a snapshot of the dataset, dump, into the dump.rdb file. In addition, we can also modify the frequency of the dump server snapshot through the configuration file. After opening the es6379.conf file, we search save and see the following configuration information:
    save save 900 1                   # if at least one key changes after 900 seconds (15 minutes), Then dump memory snapshot.
    save 300 10           # after 300 seconds (5 minutes), if at least 10 key changes, then dump memory snapshot.
    save 60 10000           # after 60 seconds (1 minute), if at least 10000 key have changed, then dump memory snapshot.
       
    2. Dump snapshot mechanism:

      1). Redis first fork child process.
      2). The child process writes the snapshot data to the temporary RDB file.
      3). When the child process completes the data write operation, replace the old file with a temporary file.
       
    3. AOF file:

      as mentioned above many times, RDB's snapshot timing dump mechanism does not guarantee good data persistence. If our application is really concerned about this, we can consider using the AOF mechanism in Redis. For Redis servers, the default mechanism is RDB, and if you need to use AOF, you need to modify the following entry in the configuration file:
      change appendonly no to appendonly yes
From now on, every time Redis receives a command to modify data, it appends it to the AOF file. On the next reboot of Redis, the information in the AOF file needs to be loaded to build the latest data into memory.
       
    4. AOF configuration:

      has three synchronization modes in the configuration file of Redis:
      always       # is written to AOF every time a data change occurs.
      appendfsync   # is synchronized every second. This policy is the default policy for AOF.
    appendfsync           # is never synchronized. Efficient but not persistent.
       
5. How to repair damaged AOF files:

1). Make an extra copy of the existing damaged AOF file.
      2). Execute "redis-check-aof --fix < filename > "Command to repair damaged AOF files.
      3). Restart Redis server with the repaired AOF file.
       
    6. Data backup for Redis:

      Redis we can backup the running Redis data files online by copy. This is because the RDB file will not be modified once it has been generated. Redis each time puts the latest data dump into a temporary file, and then atomically changes the temporary file to the original data file name using the rename function. Therefore, we can say that the copy data file is safe and consistent at any time. With this in mind, we can regularly back up Redis's data files by creating cron job, and then back up copy to a secure disk medium.  


Related articles: