redis Learning (Four) - two kinds of persistent way RDB / AOF and Redis Affairs

Why persist?
redis if only just inside the data cached in memory, if redis is down and then restarted, the data on the memory of all lost ah. You have to use persistence mechanism redis while writing data to memory, asynchronous slowly write data to disk file, for persistence.

A, Redis persistence technology


1.1, redis persistence of two ways

  • A RDB (by default): It is performed on a periodic data persistence redis
  • AOF: for each write command as a log, written to a log file in append-only mode, when redis restart, may be re-build the entire data set by playing back the log write instruction AOF

RDB or AOF, can be redis memory data to be persisted to disk above, then you can back up data to other places, such as Ali cloud and other cloud services. If redis hang up before the data can be copied back from the cloud services.
If you use RDB and AOF two kinds of persistence mechanism, so when redis restart, AOF will be used to reconstruct the data because  the data AOF is more complete .


1.2 Both persistent manner the advantages and disadvantages (difference)


(1) RDB advantages and disadvantages:
Advantages:

       RDB generating a plurality of data files, each data file is data representing a certain moment in the redis way of a plurality of data files, very suitable for cold standby ; RDB read and write services to redis little influence ( high read performance ), can be let redis maintain high performance; faster recovery relative to the AOF;
disadvantages:

        RDB data snapshot files are generated once every 5 minutes or longer, AOF is 1 second, lost data and more than AOF ; RDB child process to fork every time snapshot of the data file generated when the large amount of data possible leading to service clients pause
(2) AOF advantages and disadvantages:
advantages:
(1) to better protect data is not lost, lost up to one second data;
(2) written in append-only mode, no disk addressing overhead, write high performance , and easy file repair unbreakable;
(3) even if the log file is too large, appears backstage rewrite operation, it will not affect the read-write client (because the instructions which will rewrite the log at the time of compression, create the minimum log out of a need to restore data, when you create a new log file, the old log file when the log file is written as usual after the new exchange are ready, then you can exchange the old and new log file).;
(4) recording is performed by highly-readable manner suitable for catastrophic emergency recovery of accidentally deleted
disadvantages:

      The same data is, AOF log files are usually larger than the RDB data snapshot file; after AOF open, backed write QPS (queries per second rate) can support RDB QPS than writing low; complex operations more prone BUG
**


1.3, RDB, and how to choose the AOF (preferably together with the two)


(1) Do not just use RDB, because that would cause you to lose a lot of data;
(2) Do not just use the AOF, because that there are two problems: First, you do cold standby by AOF, RDB did not come prepared to do cold faster recovery; second, RDB data to generate a snapshot of each simple and crude, more robust, bug can be avoided AOF this complex backup and recovery mechanisms;
(3) support for Redis open simultaneously open two kinds of persistent way, we can AOF and the integrated use of two kinds of RDB persistence mechanism, with  AOF to ensure that data is not lost , as the first choice for data recovery; with  RDB to do different degrees of cold standby , are lost or damaged when not available in the AOF documents, also RDB can be used for fast data recovery.

 

1.4, RDB and AOP configuration

(1) RDB enabled by default , redis.conf specific configuration parameters are as follows:

#dbfilename:持久化数据存储在本地的文件
dbfilename dump.rdb
#dir:持久化数据存储在本地的路径,如果是在/redis/redis-3.0.6/src下启动的redis-cli,则数据会存储在当前src目录下
dir ./
##snapshot触发的时机,save    
##如下为900秒后,至少有一个变更操作,才会snapshot  
##对于此值的设置,需要谨慎,评估系统的变更操作密集程度  
##可以通过“save “””来关闭snapshot功能  
#save时间,以下分别表示更改了1个key时间隔900s进行持久化存储;更改了10个key300s进行存储;更改10000个key60s进行存储。
save 900 1
save 300 10
save 60 10000
##当snapshot时出现错误无法继续时,是否阻塞客户端“变更操作”,“错误”可能因为磁盘已满/磁盘故障/OS级别异常等  
stop-writes-on-bgsave-error yes  
##是否启用rdb文件压缩,默认为“yes”,压缩往往意味着“额外的cpu消耗”,同时也意味这较小的文件尺寸以及较短的网络传输时间  
rdbcompression yes  

(2) AOF off by default, open the way, modify the configuration file reds.conf: appendonly yes:

Real-time record in a logging operation form, efficiency is not high, it will affect the overall performance (but safer)

##此选项为aof功能的开关,默认为“no”,可以通过“yes”来开启aof功能  
##只有在“yes”下,aof重写/文件同步等特性才会生效  
appendonly yes  

##指定aof文件名称  
appendfilename appendonly.aof  

##指定aof操作中文件同步策略,有三个合法值:always everysec no,默认为everysec  
appendfsync everysec  
##在aof-rewrite期间,appendfsync是否暂缓文件同步,"no"表示“不暂缓”,“yes”表示“暂缓”,默认为“no”  
no-appendfsync-on-rewrite no  

##aof文件rewrite触发的最小文件尺寸(mb,gb),只有大于此aof文件大于此尺寸是才会触发rewrite,默认“64mb”,建议“512mb”  
auto-aof-rewrite-min-size 64mb  

##相对于“上一次”rewrite,本次rewrite触发时aof文件应该增长的百分比。  
##每一次rewrite之后,redis都会记录下此时“新aof”文件的大小(例如A),那么当aof文件增长到A*(1 + p)之后  
##触发下一次rewrite,每一次aof记录的添加,都会检测当前aof文件的尺寸。  
auto-aof-rewrite-percentage 100  

 

Two, Redis affairs

redis and database synchronization, you should write operation principle ACID transactions

 

 

2.1, Redis Affairs introduced

Redis transaction can execute multiple commands once, and with two important guarantees:

(1) A transaction is a single isolation operations:

All commands are serialized transaction executed sequentially. During execution of a transaction, the command will not be sent to other client requests interrupted.

(2) A transaction is an atomic operation:

Commands in the transaction are either all executed or not executed all.

2.2, Redis transaction set example

From start to execute a transaction will go through three phases: start transaction command into the team, the enforcement branch

Example: first to MULTI begin a transaction, then multiple commands into teams to the transaction, the transaction is triggered by the last EXEC command, together with all the commands in the transaction.

redis 127.0.0.1:6379> MULTI
OK
redis 127.0.0.1:6379> SET book-name "Mastering C++ in 21 days"
QUEUED
redis 127.0.0.1:6379> GET book-name
QUEUED
redis 127.0.0.1:6379> SADD tag "C++" "Programming" "Mastering Series"
QUEUED
redis 127.0.0.1:6379> SMEMBERS tag
QUEUED

redis 127.0.0.1:6379> EXEC
1) OK
2) "Mastering C++ in 21 days"
3) (integer) 3
4) 1) "Mastering Series"
   2) "C++"
   3) "Programming"(⊙o⊙)…

2.3, SpringBoot operation Redis Affairs

public void setString(String key, Object object) {
        // 开启事务权限
		stringRedisTemplate.setEnableTransactionSupport(true);
		// 开启事务begin
		stringRedisTemplate.multi();
		try {
			// 如果是String 类型
			String value = (String) object;
			stringRedisTemplate.opsForValue().set(key, value);
		} catch (Exception e) {
			// 回滚事务
			stringRedisTemplate.discard();
		} finally {
			// 提交事务
			stringRedisTemplate.exec();
		}
	}

 

 

 

 

 

 

 

 

Published 52 original articles · won praise 116 · views 50000 +

Guess you like

Origin blog.csdn.net/RuiKe1400360107/article/details/103655830