Redis source code analysis 17-persistent Snapshot

Source: Internet
Author: User
Redis persistence supports the snapshot method. The Snapshot method dumps the entire dbdump to the disk. The client can release the savebgsave command to allow the server to UMP dbdto the disk. Bgsave executes dump in the background (new sub-process executes dump), while save is a blocking dumpdb, which affects command execution by other clients. In addition to issuing command execution snapshots

Redis persistence supports the snapshot method. The Snapshot mode dumps the entire db to the disk. The client can release the save/bgsave command to allow the server to dump the database to the disk. Bgsave executes dump in the background (new sub-process executes dump), while save is a blocking dump db, which affects command execution by other clients. In addition to issuing command execution snapshots

Redis persistence supports the snapshot method. The Snapshot mode dumps the entire db to the disk.

The client can release the save/bgsave command to allow the server to dump the database to the disk. Bgsave executes dump in the background (new sub-process executes dump), while save is a blocking dump db, which affects command execution by other clients. In addition to issuing commands to execute snapshot saving, redis's serverCron will also execute the background dump according to the configured parameters. In addition, when the slave establishes a connection, the master will also execute a background dump, before sending data to slave (this is described in the master-slave replication section ).

static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {    ---/* Check if a background saving or AOF rewrite in progress terminated */    if (server.bgsavechildpid != -1 || server.bgrewritechildpid != -1) {      ---    }else {        /* If there is not a background saving in progress check if         * we have to save now */         time_t now = time(NULL);         for (j = 0; j < server.saveparamslen; j++) {            struct saveparam *sp = server.saveparams+j;            if (server.dirty >= sp->changes &&                now-server.lastsave > sp->seconds) {                redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",                    sp->changes, sp->seconds);                rdbSaveBackground(server.dbfilename);                break;            }         }}---}

Whether it is creating a sub-process or creating a blocking execution snapshot (rdbSaveBackground is called first when a sub-process is created), rdbSave is called to save the database.

In rdbSave, we can see that redis stores data in db in the type, key, and val modes.

RdbLoad is a function used to load data when the server starts after data is saved in snapshot mode. It is the inverse process of rdbSave.

static int rdbSave(char *filename) {    ---    for (j = 0; j < server.dbnum; j++) {        redisDb *db = server.db+j;       ---        /* Iterate this DB writing every entry */        while((de = dictNext(di)) != NULL) {            robj *key = dictGetEntryKey(de);            robj *o = dictGetEntryVal(de);            time_t expiretime = getExpire(db,key);            ---            /* Save type, key, value */           if (rdbSaveType(fp,o->type) == -1) goto werr;          if (rdbSaveStringObject(fp,key) == -1) goto werr;          if (rdbSaveObject(fp,o) == -1) goto werr;          ---        }        dictReleaseIterator(di);    }    ---    /* Use RENAME to make sure the DB file is changed atomically only     * if the generate DB file is ok. */    if (rename(tmpfile,filename) == -1) {        redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));        unlink(tmpfile);        return REDIS_ERR;    }    ---}

Original article address: redis source code analysis 17-persistent snapshot, thanks to the original author for sharing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.