Ridis persistence

Redis persistence

Redis is a high-performance key-value memory database . Compared with Memcached, it supports richer data types, including string (string), list (linked list), set (collection), zset (sorted set -- ordered set) and hash (hash type). Like Memcached, data is cached in memory to ensure efficiency. The difference is that Redis will periodically write updated data to disk or write modification operations to additional record files , which largely compensates for the lack of key/value storage such as Memcached.

Redis has two persistence methods: RDB snapshot and AOF log.

1. RDB snapshot

The default way. Think of it as a backup.

Every once in a while, the data in the memory is generated as a point-in-time snapshot of the dataset, and the data in the memory is written to the RDB file and saved in the disk. When the data in memory crashes and restarts, this file will be read.

redis.conf configuration

# rdb生成策略,从下往上匹配
save 900 1						# 在900秒内,如果有1个key发生变化,执行RDB
save 300 10						# 在300秒内,如果有10个key发生变化,执行RDB
save 60 10000					# 在60秒内,如果10000个key发生变化,执行RDB   

# 其他参数
stop-writes-on-bgsave-error yes	# 当后台写进程出错时,禁止写入新的数据 
rdbcompression yes      		# RDB是否压缩。如果看中性能,选no。压缩会节约空间,但会影响备份和恢复性能
rdbchecksum yes 				# 执行rdb恢复的时候,检查rdb文件
dbfilename dump.rdb 			# 生成的rdb文件
dir ./ 							# 存放rdb的目录

Advantages and disadvantages of RDB

  • Advantages: fast, especially fast recovery
  • Disadvantage: Data loss may occur between two RDBs. Because take a snapshot every once in a while. (In order to solve this problem, the AOF log persistence method can be used.)

2. AOF log

When the client operates Redis, the client's operation record (write operation) is recorded in a file. If a crash occurs, all operations are completely executed to restore the data.

redis.conf configuration

appendonly yes  				# aof日志默认是禁用的,需要修改此参数来启动aof

# aof记录日志策略
appendfsync always        		# 每个操作都记录日志:优点:安全。缺点:慢。
appendfsync everysec       		# 每秒记录一次日志(默认采用)
appendfsync no           		# 由操作系统来决定记录日志的方式。一般不会用到。

# aof重写的参数设置
auto-aof-rewrite-percentage 100 # aof文件比上次重写时,超过的百分比 
auto-aof-rewrite-min-size 64mb  # 执行重写的aof文件大小。64M触发重写
         

AOF log rewriting
Redis can also rewrite the AOF file in the background, so that the volume of the AOF file will not exceed the actual size required to save the state of the data set.

Example: Assume that each operation records a log

> set money 0
> incr money
> incr money
> incr money
> incr money
> incr money
> incr money
> incr money
> incr money
> incr money

...increment 100 times

If a log is recorded for each operation, 101 logs will be recorded. The final result is money 100.

Perform log rewriting:

Merge 101 items into 1 item: set money 100

redis-benchmark simulates user access:

./bin/redis-benchmark -n 100000

RDB and AOF exist at the same time, AOF
Redis can be used with AOF persistence and RDB persistence at the same time. In this case, when Redis restarts, it will preferentially use the AOF file to restore the data set, because the data set saved by the AOF file is usually more complete than the data set saved by the RDB file.

Guess you like

Origin blog.csdn.net/u011886447/article/details/104889484