rdb - Redis DataBase

官网介绍:

  在指定的时间间隔内存中的数据集快照写入磁盘,也就是行话将的Snapshot快照,它恢复时是将快照文件直接读到内存中。

是什么:

  Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到内存里一个临时文件中,待持久化过程都结束了,在用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能,如果需要进行大规模数据的恢复,且对于数据的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能对丢失。

Fork:

  Fork的作用是复制一个与当前进程一样的进程。新进程的所有数据(变量,环境变量,程序计数器等)数值都是和原进程一致,但是是一个全新的进程,并作为原进程的子进程。(存在一个问题:如果原程序的数据数据特别大,fork一份)

Rdb 保存的是dump.rdb文件

Save the DB on disk

# 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 completely by commenting out all "save" lines.
#

如果想禁用RDB持久化的策略,只要不设置任何save指令,或者给save传入一个空字符串参数也可以
# 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 ""

RDB是整个内存的压缩过的Snapshot,RDB的数据结构,可以配合复合的快照触发条件,

默认是

  1分钟内改了1万次,

  或5分钟内改了10万次,

  或15分钟内改了1次。

可以使用 save命令立即进行备份

# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes

如果在后台save出错了,前台要停止写

如果配置成no,表示你不在乎数据不一致或者有其他的手段发现和控制

# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes

rdbcompressiion:对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。如果你不想消耗CPU来进行压缩的话,可以设置关闭此功能

# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes

rdbchecksum:在存储快照后,还可以让redis使用CRC64算法来进行数据校验,但这样会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以关闭此功能,不过一般不用关,可以再空闲时进行压缩和校验

# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

使用config get dir 获取路径

 

快照:

  配置文件中默认的快照配置->冷拷贝后可以重新使用->可以cp dump.rdb dump_new.rdb,一般备份文件不会和主程序在同一台机器上。

  命令save或者bgsave  ,可以立刻的生成dump.rdb文件

    Save:save是只管保存,其他不管,全部阻塞

    BGSAVE: Redis会在后台异步进行快照操作,快照同时还可以响应客户端请求。可以通过lastsave命令获取最后一次成功执行快照的时间。

  执行flushall命令,也会产生dump.rdb文件,但里面是空的,无意义

如何恢复:

  将备份文件(dump.rdb) 移动到redis安装目录并启动服务即可,启动时会自动的读取文件,

  CONFIG GET dir获取目录

优势:

  适合大规模的数据恢复,对数据完整性和一致性要求不高

劣势:

  在一定间隔时间做一次备份,所以如果redis意外down掉的话,就会丢失最后一次快照后的所有修改

  Fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑

如何停止:

  动态所有停止RDB保存规则的方法:redis-cli config set save ""

aof - Append Only File

  

是什么:

  以日志的形式来记录每个写操作,将Redis执行过的所有写指令记录下来(读操作不记录),只许追加文件但不可改写文件,redis启动之初会读到该文件重新构建数据,换言之,重启的话就根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作。

aof在文件被破坏后可以使用如下命令修复

[root@node1 bin]# redis-server /myredis/redis_aof.conf
[root@node1 bin]# redis-cli -p 6379
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit
[root@node1 bin]# redis-check-aof --fix appendonly.aof
0x 51: Expected prefix ':', got: '*'
AOF analyzed: size=103, ok_up_to=81, diff=22
This will shrink the AOF from 103 bytes, with 22 bytes, to 81 bytes
Continue? [y/N]: y
Successfully truncated AOF
[root@node1 bin]#

# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.

appendonly yes  默认为no,改为yes为打开持久化开关

# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec".

# appendfsync always
appendfsync everysec
# appendfsync no

Appendfsync:

  Always:同步持久化,每次发生数据变更会被立即记录到磁盘,性能较差但数据完整性比较好

  Everysec:出厂默认推荐,异步操作,每秒记录,如果一秒内宕机,有数据丢失

  No

AOF启动/修复/恢复

  正常恢复

    启动:设置Yes,修改默认的appendonly no ,改为yes

    将有数据的aof文件复制一份保存到对应目录(config get dir)

    恢复:重启redis然后重新加载

  异常恢复:

    启动:设置Yes

    备份被写坏的AOF文件

    修复:Redis-check-aof --fix进行修复

    恢复:重启redis然后重新加载

Rewrite:

  是什么:

    AOF采用文件追加方式,文件会越来越大为避免出现此中情况,新增了重写机制,当AOF文件的大小超过所设定的阈值时,Redis就会启动AOF文件的内容压缩,只保留可以恢复数据的最小指令集。可以使用命令bgrewriteaof 

  重写原理:

    AOF文件持续增长而过大时,会fork出一条新进程来将文件重写(也是先写临时文件最后在rename),遍历新进程的内存中的数据,每条记录有一条的Set语句。重写aof文件的操作,并没有读取旧的aof文件,而是将整个内存中的数据库内容用命令的方式重写了一个新的aof文件,这点和快照有点类似。

  触发机制:

    Redis会记录上次重写是的AOF大小,默认配置是当AOF文件大小是上次rewrite后大小的一倍且大于64M时触发

# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

1.如果一个系统里边同时存在 RDB 和 AOF 是冲突还是协作?

  可以两者共存,系统先加载的是AOF

2.AOF 为什么会在RDB 之后产生?

3.AOF 会有什么优缺点

  优势:

    每秒同步:appendfsync always 同步持久化 每次发生数据变更会被立即记录到磁盘,性能较差但数据完整性比较高

    每修改同步:appendfsync everysec (默认)异步操作,每秒记录

    不同步:appendfsync no 从不同步

  劣势:

    相同数据集的数据而言aof文件要远大于rdb文件恢复速度慢于rdb

    Aof运行效率要慢于rdb,每秒同步策略效率较好,不同步效率和rdb相同

AOF

应该使用哪种?

Redis_持久化之RDB的更多相关文章

  1. Redis两种持久化方式(RDB&AOF)

    爬虫和转载请注明原文地址;博客园蜗牛:http://www.cnblogs.com/tdws/p/5754706.html Redis所需内存 超过可用内存怎么办 Redis修改数据多线程并发—Red ...

  2. Redis学习——Redis持久化之RDB备份方式保存数据

    从这一个介绍里面知道,redis比memcache作为缓存数据库强大的地方,一个是支持的数据类型比较多,另一个就是redis持久化功能. 下面就介绍Redis的持久化之RDB! 一:什么是redis的 ...

  3. Redis 持久化之RDB和AOP

    Redis 持久化之RDB和AOP Redis 有两种持久化方案,RDB (Redis DataBase)和 AOP (Append Only File).如果你先快速了解和使用RDB和AOP,可以直 ...

  4. redis的持久化之RDB的配置和原理

    Redis优秀的性能是由于其将所有的数据都存储在内存中,同样memcached也是这样做的,内存中的数据会在服务器重启后就没有了,也就是不能保证持久化.但是为什么Redis能够脱颖而出呢,很大程度上是 ...

  5. Redis持久化之RDB

    本文及后续文章,Redis版本均是v3.2.8 上篇文章介绍了RDB的优缺点,我们先来回顾下RDB的主要原理,在某个时间点把内存中所有数据保存到磁盘文件中,这个过程既可以通过人工输入命令执行,也可以让 ...

  6. 5-Redis 的持久化之 RDB

    2016-12-22 13:58:48 该系列文章链接NoSQL 数据库简介Redis的安装及及一些杂项基础知识Redis 的常用五大数据类型(key,string,hash,list,set,zse ...

  7. Redis 持久化之RDB和AOF

    Redis 持久化之RDB和AOF Redis 有两种持久化方案,RDB (Redis DataBase)和 AOF (Append Only File).如果你想快速了解和使用RDB和AOF,可以直 ...

  8. Redis持久化之RDB&&AOF的区别

    在说Redis持久化之前,需要搞明白什么是数据库状态这个概念,因为持久化的就是将内存中的数据库状态保存到磁盘上.那么什么是数据库状态呢?Redis是一个key-value数据库服务器,一般默认是有16 ...

  9. 【Redis】Redis学习(七) Redis 持久化之RDB和AOF

    Redis 持久化提供了多种不同级别的持久化方式:一种是RDB,另一种是AOF. RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AOF ...

随机推荐

  1. Nginx针对https站点的部署

    一.Nginx安装(略)安装的时候需要注意加上 --with-http_ssl_module,因为http_ssl_module不属于Nginx的基本模块.Nginx安装方法:# ./configur ...

  2. (原创)mybatis学习一,夯实基础

    一,what?(是什么) MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可 ...

  3. Java 集合系列17之 TreeSet详细介绍(源码解析)和使用示例

    概要 这一章,我们对TreeSet进行学习.我们先对TreeSet有个整体认识,然后再学习它的源码,最后再通过实例来学会使用TreeSet.内容包括:第1部分 TreeSet介绍第2部分 TreeSe ...

  4. netcore web.config ConnectionStrings AppSettings

    new ConfigurationBuilder().Build().GetSection("ConnectionStrings") new ConfigurationBuilde ...

  5. 嵌入支付宝SDK,出现“LaunchServices: ERROR: There is no registered handler for URL scheme alipay”错误

    应用项目中嵌入支付宝SDK,在模拟器运行app后,会出现“LaunchServices: ERROR: There is no registered handler for URL scheme al ...

  6. 由于 ASP.NET 进程标识对全局程序集缓存没有读权限,因此未能执行请求。错误: 0x80131902

    由于 ASP.NET 进程标识对全局程序集缓存没有读权限,因此未能执行请求.错误: 0x80131902 分类: c#2013-06-17 10:22 89人阅读 评论(0) 收藏 举报 ASP.NE ...

  7. swifttextfield代理方法

    //MARK:textfield delegate //键盘的高度 func textFieldShouldBeginEditing(textField: UITextField) -> Boo ...

  8. struts2 访问Web元素的4种方法

    完整代码 :Struts12AccessWebElement.rar 第一种也是最常用的一种方法实现这几个接口 RequestAware,SessionAware,ApplicationAware s ...

  9. [Python]新手写爬虫全过程(已完成)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...

  10. Pathoto项目:AWS+golang+beego搭建

    帮兄弟写了一个网站,由于要在国外使用,选择了AWS作为服务器. 不知道后面的价格如何,12个月免费的确吸引了我. 花费3天时间,从注册到服务器搭建访问,终于搞定了.下面记录一下其中容易出错的命令. 1 ...