参考资料: http://tech.it168.com/a2012/0806/1381/000001381007_1.shtml
方式1: 简单的aof文件方式
这种方式只生成aof文件;
优点: 不会定期生成snapshot,对磁盘消耗小(不是少),也不会因为生成snapshot时写磁盘对服务带来影响(虽然影响不太大)
缺点: 重新启动服务时可能需要无法估计的时间
方式2: snapshot方式
该方式不生成aof文件,定期产生snapshot文件
优点: 重启服务时,启动速度快
缺点: 会丢失最后一次产生snapshot到意外宕机之间的写数据
方式3: aof + bgrewriteaof
1. 产生aof文件
2. 通过 bgrewriteaof 命令定期压缩aof(其实是根据内容中的数据重新生成aof)文件
优点: 不会导致aof文件太大而占用太大的磁盘文件,不会因为aof文件太大而导致重启服务时太慢
缺点: 毕竟重启服务还是要加载aof文件的
方式4: snapshot + aof + snapshot点 (该方式是否可行未经验证)
1. 定期产生snapshot文件
2. 产生snapshot文件后,删除文件中snapshot点之前的数据,这样aof就不会太大了
3. 服务重启时,先加载snapshop,在加载snapshot时间点后的aof文件
4. 这样的话,aof文件中记录的命令不应该有类似 incr decr之类的命令,应该都是set、delete之类的命令,这个和snapshot点的选择有关系(没法选择一个确切的snapshot点); 或者: snapshot期间允许用户执行写操作吗?不允许的话就没有这个问题了,如果不允许的话,这个条件也太苛刻了
5. 从redis的源码来看,是不会同时参考snapshot的rdb文件和aof文件的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* Function called at startup to load RDB or AOF file in memory. */ void loadDataFromDisk(void) { long long start = ustime(); if (server.aof_state == AOF_ON) { if (loadAppendOnlyFile(server.aof_filename) == C_OK) serverLog(LL_NOTICE,"DB loaded from append only file: %.3f seconds",(float)(ustime()-start)/1000000); } else { rdbSaveInfo rsi = RDB_SAVE_INFO_INIT; errno = 0; /* Prevent a stale value from affecting error checking */ if (rdbLoad(server.rdb_filename,&rsi,RDBFLAGS_NONE) == C_OK) { serverLog(LL_NOTICE,"DB loaded from disk: %.3f seconds", (float)(ustime()-start)/1000000); /* Restore the replication ID / offset from the RDB file. */ if ((server.masterhost || (server.cluster_enabled && nodeIsSlave(server.cluster->myself))) && rsi.repl_id_is_set && rsi.repl_offset != -1 && /* Note that older implementations may save a repl_stream_db * of -1 inside the RDB file in a wrong way, see more * information in function rdbPopulateSaveInfo. */ rsi.repl_stream_db != -1) { memcpy(server.replid,rsi.repl_id,sizeof(server.replid)); server.master_repl_offset = rsi.repl_offset; /* If we are a slave, create a cached master from this * information, in order to allow partial resynchronizations * with masters. */ replicationCacheMasterUsingMyself(); selectDb(server.cached_master,rsi.repl_stream_db); } } else if (errno != ENOENT) { serverLog(LL_WARNING,"Fatal error loading the DB: %s. Exiting.",strerror(errno)); exit(1); } } } |
其实, redis4.0之后,是支持rdb+aof 方式的,这时候,loadAppendOnlyFile() 函数中有rdb相关逻辑,而且,这时候的aof文件的前面部分其实就是rdb文件的内容,但是依然叫做aof文件,所以,依然时候loadAppendOnlyFile() 函数就搞定了。
参考: Redis-4.0以后的混合持久化_gdlsky的博客-CSDN博客_redis混合持久化
关于snapshot的频率:
原来以为 save 300 10 解释为: 超过300s或者超过10条变更就snapshot,其实不是的,而是: 超过300s并且超过10条变更就snapshot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
################################ SNAPSHOTTING ################################# # # Save the DB on disk: # # save <seconds> <changes> # # Will save the DB if both the given number of seconds and the given # number of write operations against the DB occurred. # # In the example below the behaviour will be to save: # after 900 sec (15 min) if at least 1 key changed # after 300 sec (5 min) if at least 10 keys changed # after 60 sec if at least 10000 keys changed # # Note: you can disable saving at all commenting all the "save" lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save "" save 900 1 save 300 10 save 60 10000 |