美文网首页redis
redis数据持久化

redis数据持久化

作者: Geroge1226 | 来源:发表于2021-07-05 14:14 被阅读0次

1、说明

在使用Redis时候,我们会存在重启Redis情况,而Redis数据都保存在内存中,重启之后会存在丢失情况,此时我们想要Redis可以持久化,在重启之后Redis可以从持久化层获取数据,重新放入内存。redis提供两种方式持久化:RDB方式 与AOF方式。

2、RDB 持久化方式

2.1 RDB持久化快照方式介绍

机制:根据规则“定时”将内存中的数据存储的硬盘上。是使用内存快照方式。

将内存中的数据以快照的方式写入二进制文件中,默认的文件名是dump.rdb,存在当前进程目录。内容如下:

image.png

以下几种情况会触发快照:

  • 根据设定的规则自动触发;
  • 用户主动执行save或者 bgsave命令;
  • 执行FLUSHALL命令;
  • 执行复制(replication)时;

(1) savebgsave 区别

save命令操作会阻塞客户端的所有请求,将数据进行快照,如果Redis内存数据过大时,Redis会出现较长时间不响应,所以不适用于生产。
bgsave 命令操作是后台异步执行快照操作,服务器同时可继续响应来自客户端的请求

  • 查看最后一次快照时间命令:lastsave, 返回unix时间戳(秒级)
127.0.0.1:6379[1]> lastsave
(integer) 1625462617

(2)flushall命令清除数据时执行快照
只要自动快照条件不为空,在执行flushall时都会执行一次快照,与配置的自定义快照条件是否满足无关 。

(3)执行复制时触发快照
redis主从模式下,在复制初始化的时候会触发快照操作。

2.2、RDB中快照原理

(1)redis使用fork函数复制一份当前进程(父进程)的副本(子进程)。
(2)父进程继续接收并处理客户端发来的命令,而子进程开始将内存中的数据写入硬盘中的临时文件
(3)当子进程写入完所有数据后会用该临时文件替换旧的RDB文件完成快照。
:fork的副本内存小于redis使用内存,如,总共内存4G, redis 已使用3G, 做fork时候不会是占用6G。

2.3、持久化配置方式

修改redis配置文件:/usr/local/redis-5.0.5/redis.conf
redis 默认持久化是RDB快照方式

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

配置含义:

900秒内,如果超过1个key被修改,则发起快照保存
300秒内,如果超过10个key被修改,则发起快照保存
60秒内,如果1万个key被修改,则发起快照保存

3、AOF( appedn only file)持久化方式

机制:每执行一条更改Redis内存数据的命令都会将命令以纯文本形式保存在硬盘上。

3.1 配置AOF持久方式

aof 修改redis.conf文件,默认appendonlyno,把no改为yes重启redis即可。

############################## APPEND ONLY MODE ###############################

# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (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 no

# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.aof"

相关文章

  • Redis-2 数据持久化及持久化配置

    一、数据持久化 开启持久化功能后,重启redis,数据会自动通过持久化文件恢复!! 1、redis持久化 – 两种...

  • redis与memcache区别

    1、持久化 redis是支持持久化存储,宕机重启数据不会丢失,memcache重启后数据丢失 redis持久化的方...

  • 关于redis的几件小事(六)redis的持久化

    1.redis持久化的意义 redis持久化的意义,在于 故障恢复 。如果没有对数据进行持久化,那么如果redis...

  • Redis常见面试题

    Redis常见面试题 Redis持久化机制 Redis是一个支持持久化的内存数据库,通过持久化机制把内存中的数据同...

  • Redis持久化

    Redis持久化 为什么要持久化 Redis是内存数据库,宕机后数据会消失。 Redis重启后快速恢复数据,要提供...

  • redis笔记(四)redis持久化

    redis持久化 Redis持久化机制:redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中...

  • 基于Redis5.0.2的总结随笔

    Redis支持数据持久化,众多数据结构存储,master-slave模式数据备份等多种功能。 Redis持久化 持...

  • Redis与Memcached区别

    Redis优势 redis具有持久化机制,定期将内存中的数据持久化到硬盘。 redis具备binlog功能,所有操...

  • redis 学习(16)-- redis 持久化

    redis 持久化 什么是持久化 redis 将所有数据保持在内存中,对数据的更新将异步地保存在磁盘中 持久化的方...

  • redis缓存失效解决方案

    redis 持久化机制 Redis的数据都存放在内存中,如果没有配置持久化,redis重启后数据就全丢失了,于是需...

网友评论

    本文标题:redis数据持久化

    本文链接:https://www.haomeiwen.com/subject/ttblsktx.html