persistence

First, let’s talk about persistence. Persistence is the mechanism by which program data is transferred between persistent and transient states. In layman’s terms, transient data (such as data in memory, which is not permanent) is persisted to persistent data (such as persistent data to a database, which is durable). In addition, the reason why we use Redis is fast is because the data is stored in memory, in order to ensure that the server can recover the data after an exception, so there is Redis persistence.

RDB and AOF

Redis has two kinds of persistence, one is snapshot RDB, the other is incremental file AOF.

RDB

RDB persistence saves a snapshot of the data at a certain point in time at a specific time interval. After we get the snapshot of the data, we can replicate the data completely according to the snapshot. This way we can be used to back up data, snapshot files back up, send to another server can be directly restored data. But this is all the data at one point in time, if we want the latest data, we have to periodically generate snapshot files.

RDB is mainly implemented by creating a sub-process to achieve RDB file snapshot generation. The sub-process is used to achieve the backup function without affecting the performance of the main process. At the same time, it is also mentioned that the RDB snapshot file stores the data at a certain interval, which will cause that if the interval is too long, the server will lose all the data at this interval before the snapshot is generated. Then some students will say, we can set the interval a little shorter, appropriate shortening is ok, but if the interval is set a little shorter, frequent snapshot generation will have an impact on the system, especially in the case of a large amount of data, high performance environment is not allowed to this situation.

You can configure RDB in redis. Conf, including the policy for generating snapshots, and the path and name of log files. There is also a timed backup rule, as shown in the figure below. The comments in the rule are very clear. Simply speaking, the snapshot will be triggered when the number of keys changes within a certain period of time. For example, save 300 10 indicates that production snapshots will be triggered if 10 keys change within 5 minutes, and the same applies to other keys.

In addition to automatically creating snapshot files in the configuration file, Redis provides related commands to manually create snapshot files, respectively: SAVE and BGSAVE. These two commands have the same function, but the way and effect are different. After executing the SAVE command, the server process is blocked. After blocking the server can’t process any request, so cannot be used in the production, and SAVE command directly blocking the server process, BGSAVE command is to generate a child process, through the child process to create the RDB file, main process can still deal with receive the command, which does not block the server, can be used in the production.

We modify the snapshot generation strategy to save 10 2, and then start the Redis service locally and use the Redis – CLI link to access. The steps are as follows

  1. Modify the configuration as follows

  2. Start the Redis service, we can see from the startup log, the default is to read the RDB file for recovery

  3. Link to Redis service and set 3 keys within 10 seconds

  4. At this time, we will see the following output in the Redis log, because the rule is triggered, so start the child process for data backup, and in the corresponding file path, we also see the RDB file.

As can be seen from the above, the configured rules have taken effect and the RDB file has been successfully generated. If the server is abnormal in the future, the corresponding RDB file will be read for data backup as long as the server is restarted.

AOF

AOF is a form of appending command execution, which differs from RDB in that AOF does not save the data, but the actions performed. When AOF is enabled, every command executed by the client after the connection is logged. MySQL binlog is also a command to record operations. You can then restore data according to the file.

AOF is a file in append command format. Similarly, we can define how often data should be synchronized. Redis itself provides three strategies to achieve command synchronization, namely, no synchronization, once per second, and once when there is a query. The default policy and the most-used policy is one synchronization per second, so we know that we’re losing at most one second of data. With this mechanism, AOF is much more reliable than RDB, but AOF files are generally larger than RDB files because of the commands that are executed.

Appendonly yes: set appendfsync everysec to synchronize once per second. The AOF function is disabled by default. You can recover data from this file.

Similarly, we can see in redis.conf that AOF is disabled by default, and we can also specify the corresponding file name and path.

Next, we will test the AOF function, modify the configuration and restart the Redis server. We will find that the RDB file has not been read, and an AOF file has been generated under the log file path. It should be noted that because we restarted the service and turned on AOF, there is no data we added before in the Redis server (what is the problem?). .

Next we use the client connection to enter, set the following values, and then we can look at the contents of the AOF file

We can see that the content in the AOF file is the command executed, but it is stored in a fixed format. If we do not need any data when backing up, we can manually delete the corresponding command to back up the data again.

File load sequence

Redis has two persistence schemes, and both schemes generate corresponding files. How do Redis load files when restoring data? And if both files exist, which one will prevail?

Through the above test, we can actually see that Redis takes AOF as the priority. After all, COMPARED with RDB, AOF can save more complete data in the case of exceptions.

Finally:

Some interview questions for 2020 are summarized. The interview questions are divided into 19 modules, which are: Java Basics, Containers, multithreading, reflection, object copy, Java Web, exceptions, Networking, Design patterns, Spring/Spring MVC, Spring Boot/Spring Cloud, Hibernate, MyBatis, RabbitMQ, Kafka, Zookeeper, MySQL, Redis, JVM.

Access to information above information: concern public number: programmers with stories, access to learning materials. Remember to click follow + comment oh ~