`

Redis持久化机制对比分析

阅读更多

 

                                                 

一, AOF(Append Only File)持久化

1.原理

       AOF是通过保存发送到服务器端的命令来保存数据库状态,类似关系数据库的redo log。Redis Client SDK,比如Jedis,与Redis Server通信的协议是 RESP (REdis Serialization Protocol), AOF本质就是把RESP的内容直接以Append的方式保存到文件里。以SET KEY VALUE命令为例,追加到AOF文件的内容是:   

                *3\r\n$3\r\nSET\r\n$3\r\nKEY\r\n$5\r\nVALUE\r\n

2.追加时机        

          参考:/redis-5.0.4/src/server.h

          struct redisServer{

                   // ...

                  // AOF缓冲区

                  sds aof_buf;

          }

         Redis Server进程本质上是一个单线程的IO多路复用(epoll机制)的事件循环 ,在这个循环中接收客户端的命令请求,执行数据操作,发送命令回复;时间事件负责执行serverCron需要定时运行的函数。服务器端在接收到命令请求后,会把存放到aof_buf缓冲区中,在服务器每次结束一次事件循环之前,调用flushAppenOnlyFile函数,根据参数配置将aof_buf缓冲区的内容写入和同步AOF文件里,伪代码如下:

     Def eventLoop():

        While True:

           // 接收客户端的命令请求,执行数据操作,发送命令回复

           processFileEvents();

           //时间事件 执行serverCron函数

           processTimeEvents()

           // 根据参数配置将aof_buf缓冲区的内容写入和同步AOF文件

           flushAppendOnlyFile()        

 

3.flushAppendOnlyFile

函数的行为由appendfsync选项的值决定:

  fsync系统调用确保文件fd所有已修改的内容已经正确同步到硬盘上,该调用会阻塞等待直到设备报告IO完成。

# The default is "everysec"

        # no: don't fsync, just let the OS flush the data when it wants. Faster.

# always: fsync after every write to the append only log. Slow, Safest.

# everysec: fsync only one time every second. Compromise. 

 

always的配置保证最多丢失一条命令的数据,性能相对最差,但具体性能指标要看硬件配置。

4.AOF重写

     随着服务器运行,AOF一直追加,文件会越来越大,并且数据可能已经变化,保留全部的命令也没有意义。AOF重写将当前缓存的数据以RESP格式写入文件,Redis根据配置在文件大小超过默认64M后,每增加1倍时进行自动触发重写,Redis4.0以后的重写是先以RDB的格式保存全量数据,以RESP在文件尾追求增量数据,所以文件格式是混合格式。详细过程如下:

 

How it works

a)Log rewriting uses the same copy-on-write trick already in use for snapshotting. This is how it works:

b)Redis forks, so now we have a child and a parent process.

c)The child starts writing the new AOF in a temporary file.

d)The parent accumulates all the new changes in an in-memory buffer (but at the same time it writes the new changes in the old append-only file, so if the rewriting fails, we are safe).

e)When the child is done rewriting the file, the parent gets a signal, and appends the in-memory buffer at the end of the file generated by the child.

f)Profit! Now Redis atomically renames the old file into the new one, and starts appending new data into the new file.

 

5.AOF重写可能产生的问题

重写是在子进程中进行的,在重写期间,如果有大量的写入操作修改数据,Linux的COW机制起作用,会存在内存页的复制,内存使用会上升,具体情况取决于修改的页数量。重写需要的时间取决于数据量的大小与磁盘写的性能,进而影响重写期间父进程COW机制复制的页数,此问题在RDB重写时同样存在。

二, RDB持久化

1.RDB持久化

RDB是当前内存数据集的二进制快照,具体触发的就是在上面processTimeEvents函数中,RDB具体格式参考如下链接:

https://github.com/sripathikrishnan/redis-rdb-tools/wiki/Redis-RDB-Dump-File-Format

 

2.How it works

a)Whenever Redis needs to dump the dataset to disk, this is what happens:

b)Redis forks. We now have a child and a parent process.

c)The child starts to write the dataset to a temporary RDB file.

d)When the child is done writing the new RDB file, it replaces the old one.

e)This method allows Redis to benefit from copy-on-write semantics

3.默认配置参数

#  automatically save the dataset every N seconds if there are at least M changes in the dataset

save 900 1             # after 900 sec (15 min) if at least 1 key changed

save 300 10            #after 300 sec (5 min) if at least 10 keys changed

save 60 10000          #after 60 sec if at least 10000 keys changed

 

三,官方对比

RDB advantages

a)RDB is a very compact single-file point-in-time representation of your Redis data. RDB files are perfect for backups. For instance you may want to archive your RDB files every hour for the latest 24 hours, and to save an RDB snapshot every day for 30 days. This allows you to easily restore different versions of the data set in case of disasters.

b)RDB is very good for disaster recovery, being a single compact file that can be transferred to far data centers, or onto Amazon S3 (possibly encrypted).

c)RDB maximizes Redis performances since the only work the Redis parent process needs to do in order to persist is forking a child that will do all the rest. The parent instance will never perform disk I/O or alike.

d)RDB allows faster restarts with big datasets compared to AOF.

RDB disadvantages

a)RDB is NOT good if you need to minimize the chance of data loss in case Redis stops working (for example after a power outage). You can configure different save points where an RDB is produced (for instance after at least five minutes and 100 writes against the data set, but you can have multiple save points). However you'll usually create an RDB snapshot every five minutes or more, so in case of Redis stopping working without a correct shutdown for any reason you should be prepared to lose the latest minutes of data.

b)RDB needs to fork() often in order to persist on disk using a child process. Fork() can be time consuming if the dataset is big, and may result in Redis to stop serving clients for some millisecond or even for one second if the dataset is very big and the CPU performance not great. AOF also needs to fork() but you can tune how often you want to rewrite your logs without any trade-off on durability.

AOF advantages

a)Using AOF Redis is much more durable: you can have different fsync policies: no fsync at all, fsync every second, fsync at every query. With the default policy of fsync every second write performances are still great (fsync is performed using a background thread and the main thread will try hard to perform writes when no fsync is in progress.) but you can only lose one second worth of writes.

b)The AOF log is an append only log, so there are no seeks, nor corruption problems if there is a power outage. Even if the log ends with an half-written command for some reason (disk full or other reasons) the redis-check-aof tool is able to fix it easily.

c)Redis is able to automatically rewrite the AOF in background when it gets too big. The rewrite is completely safe as while Redis continues appending to the old file, a completely new one is produced with the minimal set of operations needed to create the current data set, and once this second file is ready Redis switches the two and starts appending to the new one.

d)AOF contains a log of all the operations one after the other in an easy to understand and parse format. You can even easily export an AOF file. For instance even if you flushed everything for an error using a FLUSHALL command, if no rewrite of the log was performed in the meantime you can still save your data set just stopping the server, removing the latest command, and restarting Redis again.

AOF disadvantages

a)AOF files are usually bigger than the equivalent RDB files for the same dataset.

b)AOF can be slower than RDB depending on the exact fsync policy. In general with fsync set to every second performance is still very high, and with fsync disabled it should be exactly as fast as RDB even under high load. Still RDB is able to provide more guarantees about the maximum latency even in the case of an huge write load.

Ok, so what should I use?

The general indication is that you should use both persistence methods if you want a degree of data safety comparable to what PostgreSQL can provide you.

If you care a lot about your data, but still can live with a few minutes of data loss in case of disasters, you can simply use RDB alone.

There are many users using AOF alone, but we discourage it since to have an RDB snapshot from time to time is a great idea for doing database backups, for faster restarts, and in the event of bugs in the AOF engine.

 

分享到:
评论

相关推荐

    从源码解读redis持久化

    【Redis持久化机制详解】 Redis作为一种高性能的内存数据库,其数据存储在内存中,因此面临一个问题:当系统崩溃或服务器意外关机时,内存中的数据可能会丢失。为了解决这个问题,Redis提供了两种主要的持久化机制...

    Redis持久化机制-浅入

    Redis持久化就是将数据存入磁盘,以保证数据在redis在崩溃时减小对数据的影响,redis提供两种持久化方式。 1.快照(snapshotting)方式RDB,RDB是redis默认的持久化方式,可以再redis.conf中配置其存储频度,如图: ...

    REDIS persistence -- Redis中国用户组(CRUG)1

    本文将对 Redis 持久化机制进行详细的介绍和比较。 RDB 持久化方式 RDB(Redis Database)持久化方式是 Redis 的一种默认持久化机制。这种机制可以在指定的时间间隔内对数据进行快照存储,生成一个紧凑的单一文件...

    Redis持久化RDB和AOF区别详解

    为了确保数据在服务器重启或异常情况下的安全,Redis提供了两种主要的持久化机制:RDB(Redis Database Persistence,即快照)和AOF(Append Only File,追加日志)。下面将详细解释这两种持久化方式的区别。 **RDB...

    04_redis 数据持久化 和 lamp 环境搭建.docx

    为了确保数据的安全性和持久性,Redis提供了两种主要的持久化机制:RDB(Redis Database Backup)和AOF(Append Only File)。通过这两种机制,Redis能够将内存中的数据以某种形式持久化到磁盘上,即使服务器重启也...

    Redis实战Redis实战

    其次,Redis的持久化机制是其稳定性和可靠性的重要保障。书中会介绍两种主要的持久化方式:RDB(快照)和AOF(Append Only File)。RDB会在指定时间点或满足特定条件时生成数据库的全量快照,而AOF则是记录每次写...

    Memcached Redis MongoDB对比

    Redis支持数据持久化,包括快照和AOF(Append Only File)两种方式。AOF提供了更可靠的持久化,但对性能有一定影响。Memcached本身不支持持久化,主要用于缓存,提升系统性能。MongoDB则在1.8版本之后支持了基于...

    redis-7.2.3.zip

    但为了数据安全,Redis支持RDB(Snapshotting)和AOF(Append Only File)两种持久化机制,将内存中的数据定期或实时写入磁盘。 3. **发布/订阅**:Redis提供了pub/sub(发布/订阅)模式,允许消息的发布者与多个...

    redis介绍文档学习

    ### Redis介绍文档学习...以上就是关于Redis的基本介绍、特点、优势、与Memcached的对比、持久化机制、主从复制、哨兵以及集群架构的相关知识点。通过学习这些内容,初学者可以更好地理解Redis的工作原理和应用场景。

    memcache与redis的比较

    在分布式系统中,缓存机制是非常重要的一部分,memcache和Redis都是常用的缓存解决方案,本文将对两者进行比较,探讨它们的异同。 数据类型支持 Redis支持多种数据类型,如string、list、set、zset、hash等,而...

    redis.googlecode.com.zip

    在2.2版本中,Redis对这两种持久化机制进行了优化,提高了数据恢复的速度和一致性。 文件名"redis.googlecode.com"暗示了这些源代码可能来自于Google Code项目托管平台,这是一个早期的开源项目托管服务,后来被...

    Redis从初始到集群到哨兵模式

    Redis是一款开源的>NoSQL键值存储数据库,提供了丰富的数据类型、持久化机制、事务支持、消息队列等功能,广泛应用于缓存、消息队列、分布式锁、Leader 选举等场景。 Redis数据类型 Redis支持五种基本数据类型:...

    Redis数据库 v6.0.20.zip

    Redis以其高效的数据处理能力,丰富的数据结构以及强大的持久化机制,在IT行业中备受青睐。本次提供的压缩包"Redis数据库 v6.0.20.zip"包含了Redis的源码及相关的文档,这对于学习、研究或者进行毕业设计论文的撰写...

    Redis缓存数据库_redis_数据库系统_

    同时,Redis支持通过持久化机制将内存中的数据保存到磁盘,确保数据安全。 二、Redis功能特性 1. 高性能:Redis的所有操作都在内存中进行,读写速度极快。 2. 数据持久化:RDB(快照)和AOF(日志)两种方式保证...

    100道 Redis面试题及答案.docx

    在面试中,Redis 的知识通常包括其核心特性、持久化机制、性能优化、与其他缓存系统的对比等方面。以下是根据提供的内容对这些知识点的详细解释: 1. **Redis 持久化策略选择**: - 如果数据无足轻重,可以不进行...

    尚硅谷Redis教学视频

    6. **持久化机制**:包括RDB快照和AOF日志两种持久化方式的工作原理、优缺点对比及应用场景分析。 7. **集群与高可用**:探讨如何搭建Redis集群实现负载均衡,以及主从复制、哨兵机制等技术方案以提升系统的可靠性和...

Global site tag (gtag.js) - Google Analytics