对Redis数据的持久性进行验证
关于Redis
Redis是一种内存键值存储(In-Memory Key-Value Store),可以实现快速的数据查询,在各个网站上都有介绍。
内存存储?这意味着它只在内存中运行,停止时数据会怎样?
我大致明白了,但对于我想了解的内容没有找到确切的解释,所以我个人打算进行验证。
Redis的安装设置
使用itamae的配方在CentOS7服务器上进行设置
cd ~/itamae_cookbooks
mkdir redis
cd redis
package "redis" do
action :install
end
# 事前確認
itamae local redis.rb --dry-run
# itamae実行
itamae local redis.rb
# 起動
systemctl start redis.service
核实
在使用redis-cli添加数据之后,正常关闭并启动redis服务,并确认数据的正确性。
添加数据
# cli起動
redis-cli
# データ追加
127.0.0.1:6379> SET key1 val1
#=> OK
# データ確認
127.0.0.1:6379> GET key1
#=> "val1"
# データ確認2
127.0.0.1:6379> GET key2
#=> (nil)
# データ確認3
127.0.0.1:6379> GET key3
#=> (nil)
Redis顺利停止->正常启动
# 停止
systemctl stop redis.service
# 確認
ps ax|grep redis
#=> 14223 pts/0 S+ 0:00 grep --color=auto redis
# 再びredis起動
systemctl start redis.service
# 確認
ps ax|grep redis
#=> 14232 ? Ssl 0:00 /usr/bin/redis-server *:6379
#=> 14236 pts/0 S+ 0:00 grep --color=auto redis
在Redis上查看数据
# cli起動
redis-cli
# データ確認
127.0.0.1:6379> GET key1
#=> "val1"
# データ確認2
127.0.0.1:6379> GET key2
#=> (nil)
# データ確認3
127.0.0.1:6379> GET key3
#=> (nil)
永久化
使用redis-cli添加数据后,通过kill命令强制终止redis进程->重新启动,然后确认数据。
添加数据
# cli起動
redis-cli
# データ追加2
127.0.0.1:6379> SET key2 val2
#=> OK
# データ確認
127.0.0.1:6379> GET key1
#=> "val1"
# データ確認2
127.0.0.1:6379> GET key2
#=> "val2"
# データ確認3
127.0.0.1:6379> GET key3
#=> (nil)
强制关闭redis -> 正常启动
# 強制終了
ps ax|grep redis|grep -v grep|awk '{print $1}'|xargs kill -9
# 確認
ps ax|grep redis
#=> 14280 pts/0 S+ 0:00 grep --color=auto redis
# 再びredis起動
systemctl start redis.service
# 確認
ps ax|grep redis
#=> 14287 ? Ssl 0:00 /usr/bin/redis-server *:6379
#=> 14291 pts/0 S+ 0:00 grep --color=auto redis
在Redis上查看数据
# cli起動
redis-cli
# データ確認
127.0.0.1:6379> GET key1
#=> "val1"
# データ確認2
127.0.0.1:6379> GET key2
#=> (nil)
# データ確認3
127.0.0.1:6379> GET key3
#=> (nil)
消失了 shī le)
使用redis-cli命令添加数据,执行SAVE命令,并使用kill命令强制终止redis进程->重新启动,以确认数据。
数据添加
# cli起動
redis-cli
# データ確認
127.0.0.1:6379> GET key1
#=> "val1"
# データ確認2
127.0.0.1:6379> GET key2
#=> (nil)
# データ確認3
127.0.0.1:6379> GET key3
#=> (nil)
# データ追加3
127.0.0.1:6379> SET key3 val3
#=> OK
# SAVE実行
127.0.0.1:6379> SAVE
#=> OK
# データ追加2
127.0.0.1:6379> SET key2 val2
#=> OK
# データ確認
127.0.0.1:6379> GET key1
#=> "val1"
# データ確認2
127.0.0.1:6379> GET key2
#=> "val2"
# データ確認3
127.0.0.1:6379> GET key3
#=> "val3"
強制終止Redis -> 正常启动
# 強制終了
ps ax|grep redis|grep -v grep|awk '{print $1}'|xargs kill -9
# 確認
ps ax|grep redis
#=> 14309 pts/0 S+ 0:00 grep --color=auto redis
# 再びredis起動
systemctl start redis.service
# 確認
ps ax|grep redis
#=> 14316 ? Ssl 0:00 /usr/bin/redis-server *:6379
#=> 14320 pts/0 S+ 0:00 grep --color=auto redis
在 Redis 上查看数据
# cli起動
redis-cli
# データ確認
127.0.0.1:6379> GET key1
#=> "val1"
# データ確認2
127.0.0.1:6379> GET key2
#=> (nil)
# データ確認3
127.0.0.1:6379> GET key3
#=> "val3"
永恒化和消失发生了
总结
举个例子,比如以RDS的Oracle为例,它采用的是延迟写入的方式,而不是实时更新数据区域。它通过REDO日志、UNDO缓冲等来集中更新数据,因此在发生进程异常停止等恢复时,会出现前滚(将未记录在数据区域的数据从REDO日志中反映出来)和回滚(将已提交的数据回滚到未提交的状态)来保持数据的一致性。
一方面,Redis具有默认设置,基本上在内存中运行。换句话说,如果进程本身出现故障,基本上数据会丢失。然而,在正常结束、执行SAVE命令或自动保存时(类似于RDS的延迟写入),可以将数据以快照文件的形式写入磁盘,以便在重新启动时从快照中读取数据,因此可以说数据是持久化的。
增加
可以通过redis.conf文件更改持久性设置。
可以在save选项中更改自动磁盘写入设置。
# 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
此外,还有一个名为appendonly的选项。通过将更新操作记录到一个名为appendonly.aof的只写文件(类似于REDO日志文件),即使快照中未记录更新操作,在重新启动时也可以进行重建,从而防止数据丢失。(虽然是说appendonly,但可以在数据库中进行数据更新,没有问题)
############################## 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"
此外,还可以通过appendfsync选项来设置将数据写入appendonly.aof文件的时机。
# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# 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.
#
# 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
还有自动重写设置,以防止aof文件无限膨胀。
# Automatic rewrite of the append only file.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size grows by the specified percentage.
#
# This is how it works: Redis remembers the size of the AOF file after the
# latest rewrite (if no rewrite has happened since the restart, the size of
# the AOF at startup is used).
#
# 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