We know that Redis is with the ability to persist, and that in the end, what is persistent into what kind of??? Let's look for answers together.
One: Snapshot mode
Perhaps at the beginning of Redis, I have heard that there are two persistent modes of Redis, the first of which is the snapshotting mode, or the AOF mode, and the most used in combat scenarios.
There is no snapshotting mode, this does not need to refute it, and you may also know that using the snapshotting mode, you need to set the configuration parameters in redis.conf, such as the following:
# Save the DB on disk:## save<seconds> <changes># # would save the DBifBoth the given number of seconds and the given# number of write operations against the DB occurred.## in the exam PLE below the behaviour would be is to save:# after theSEC ( theMinifAt least1Key changed# after -SEC (5MinifAt leastTenkeys changed# after -SecifAt least10000keys changed## note:you can disable saving completely by commenting outAll"Save"lines.## It isalso possible to remove all the previously configured save# points by adding a save directive with a single empty /c5>stringargument# likeinchThe following example:## save""Save the 1Save - TenSave - 10000
The above three sets of commands are also very well understood, that is, 900 refers to the "number of seconds", 1 refers to the "change number", and then if there are 1 changes in "900s", then the save is executed, the same
The truth, if there are 10 times within the 300s change,60s within the 1w times change, then will also perform the save operation, so simple, see I just said a few words, is not a kind of intuition in
I'm telling you, there are two questions to clarify:
1. The above operation should be a synchronous operation of Redis itself, can I ask if I could execute save manually?
Of course you can do it manually, and Redis provides two operations commands: Save,bgsave, which forces data to be flushed to the hard disk, such as:
2. Look at the above diagram, it seems that Bgsave is to open a separate thread, please?
Indeed, as you said, Bgsave is to open the thread for data refresh, and if not, let's look at the code, the code is in the RDB.C source file, as follows:
From the above code, there is no point of view, that is the fork method, it is some of the cattle population said what fork out a thread, today you also counted finally see, in fact, Redis is not simply
Single-threaded service, at least fork tells us that it will also turn on the worker thread in some scenarios, and then you can see that the code will perform the synchronous bgsave operation in the worker thread, which is as simple as that.
3. Can you simply say the logic of saveparams parameters in Redis source code?
Yes, in fact, in Redis there is a periodic function called Servercron, which will start periodically and probably do seven things, as the Redis note says:
/*This is our timer interrupt, called server.hz times per second. * Here's where we do a number of the things that need to Be-done asynchronously. * For instance: * *-Active Expired Keys collection (it's also performed in a lazy-to-on * lookup). *-software watchdog. *-Update some statistic. *-Incremental rehashing of the DBs hash tables. *- triggering bgsave/aof rewrite, and handling of terminated children. *-Clients timeout of different kinds. *-Replication reconnection. *-many more ... * * Everything directly called here'll be called server.hz times per second, * So in order to throttle Execution of things we want to does less frequently * a macro is used:run_with_period (milliseconds) {...}*/intServercron (structAeeventloop *eventloop,Long LongIdvoid*clientdata) {
The red font above is to do the save operation we care about, read the comments of the method, and then we look for the specific logic.
From the above code logic, you should be able to find the following points:
<1>. The Saveparams parameter is under the server object, and the server object is exactly the Redisserver type, such as:
From the comments in the *saveparams above, you should know that *saveparams is an array of saveparam types, so now there is not a strong curiosity to see Saveparam
What is the definition of a type??? Such as:
You can see that there are two parameters in the Saveparam parameter, seconds is the number of seconds to save, changes is the amount of change, and these two parameters correspond to our configuration file 900 0 such
Configuration section, think of no Ha ~ ~ ~
<2> then we can find through if, if the end satisfies, will eventually call Rdbsavebackground to persist our Rdb file, simple ...
Well, that's probably it, I hope it helps you.
---restore content ends---
15 days Play redis--The tenth chapter of the snapshot mode in-depth analysis