Redis learning four configuration file analysis and persistence operations

Redis configuration file analysis

1. Unit is not case sensitive, for example, 1GB and 1gb are the same
Insert picture description here

2. Other configuration files can be included.
Insert picture description here
3. Network aspects

bind 127.0.0.1
protected-mode yes #保护模式
port 6379

4.
Run as a daemon (run in the background)

daemonize yes

Process file specified

pidfile /www/server/redis/redis.pid

5. Log related
Log level
Insert picture description here
Location of log file
Insert picture description here
6.
Number of databases

databases 16
always-show-logo yes #是否总是显示LOGO

7.
Persistence, how many operations are performed within the specified time will be persisted to the file .rdb.aof

#如果900s内,至少有一个key进行了修改,就及时进行持久化操作
save 900 1
#如果300s内,至少有10个key进行了修改,就及时进行持久化操作
save 300 10
#如果60s内,至少有10000个key进行了修改,就及时进行持久化操作
save 60 10000

stop-writes-on-bgsave-error yes #持久化如果出错,默认继续工作
rdbcompression yes #是否压缩rdb文件
rdbchecksum yes #保存rdb文件时进行错误的检查校验
dir /www/server/redis/ #rdb文件保存的目录

8. You can set the password in the SECURITY module

You can also use the command

config get requirepass #获取redis密码
config set requirepass “123456” #设置redis的密码

auth 123456 #使用密码登录

9.
Set the maximum number of clients that can connect to redis

maxclients 10000
maxmemory <bytes> #redis配置最大的内存容量
maxmemory-policy noeviction #内存达到上限之后的处理策略

maxmemory-policy 六种方式
1volatile-lru:只对设置了过期时间的key进行LRU(默认值) 
2、allkeys-lru : 删除lru算法的key   
3volatile-random:随机删除即将过期key   
4、allkeys-random:随机删除   
5volatile-ttl : 删除即将过期的   
6、noeviction : 永不过期,返回错误

10. Aof configuration (another persistence)

appendonly no #默认不开启,用rdb就够了
appendfilename "appendonly.aof" #持久化的文件名字

三种同步
# appendfsync always #每次修改都会同步
appendfsync everysec #每秒执行一次同步
# appendfsync no #不执行同步,让操作系统自己执行同步,速度最快

Redis persistence

Redis provides two different persistence methods to store data in the hard disk. One method is called snapshot (RDB), which can write all the data that exists at a certain moment into the hard disk. Another method is called append-only file (AOF), which will copy the executed write command to the hard disk when the write command is executed.

RDB operation

The RDB operation will generate a dump.rdb file and store it in the path specified by the dir option.
If any one of Redis, system, or hardware crashes before the new snapshot file is created, then Redis will lose the write after the last snapshot was created All data entered
For example,
suppose that Redis currently stores certain data in memory. The last snapshot was created at 2:35 pm and it has been created successfully. A new snapshot was created at 3:06 pm, and 35 keys were updated before the snapshot file was created at 3:08 pm. If the system crashes between 3:06 and 3:08 and Redis cannot complete the creation of a new snapshot, then Redis will lose all data written after 2:35 pm. On the other hand, if the system crashes right after the new snapshot file is created, Redis will only lose 35 key update data

Trigger method

1. Save trigger mode
This command will block the current Redis server. During the execution of the save command, Redis cannot process other commands until the RDB process is completed. When the execution is complete, if there is an old RDB file, the new one will replace the old one. Our clients may be tens of thousands or hundreds of thousands, which is obviously not advisable.

2. bgsave trigger mode When
this command is executed, Redis will perform a snapshot operation asynchronously in the background, and the snapshot can also respond to client requests at the same time. The specific operation is that the Redis process executes a fork operation to create a child process. The RDB persistence process is responsible for the child process and ends automatically after completion. Blocking only occurs in the fork phase, and generally takes a short time. Basically all RDB operations in Redis use the bgsave command.

3. Automatic triggering
Automatic triggering is done by our configuration file. In the redis.conf configuration file, refer to point 7 of the Redis configuration file detailed explanation for the relevant configuration

Trigger mechanism

1. When the save rules are met, the RDB rules will be automatically triggered.
2. The flushing command will also be triggered.
3. The exit of redis will also be triggered.

How to recover rdb files

Just put the rdb file in the redis startup directory, and redis will automatically scan when it starts.
The method of viewing the directory:

127.0.0.1:6379> config get dir
1) "dir"
2) "/www/server/redis"

Advantages:
1. Suitable for large-scale data scale
2. Low requirements for data integrity

Disadvantages:
1. It takes a certain time interval for process operations. If redis crashes unexpectedly, the last modified data will be gone.
2. When the fork process, it will take up a certain amount of content space

AOF operation

Record all the commands and execute all the files when you restore. A
popular understanding is log records. Record each write operation in the form of a log, record all the instructions executed by Redis (read operations are not recorded), only append files but not rewrite files, redis will read the file to rebuild the data at the beginning of the startup. That is to say, if redis restarts, it will execute the write command from front to back according to the content of the log file to complete the data recovery. AOF saves the appendonly.aof file.

Principles of File Rewriting

AOF will cause persistent files to become larger and larger. In order to compress the persistent file of AOF. redis provides the bgrewriteaof command. The data in the memory is saved to a temporary file in the form of commands, and a new process will be fork to rewrite the file.
The operation of rewriting the aof file does not read the old aof file, but rewrites the database content in the entire memory with a command to rewrite a new aof file, which is similar to a snapshot.

Operation configuration file is required to enable AOF

appendonly yes


Change the default no to yes and restart redis to take effect. If there is an error in the .aof file, redis will fail to open, and the client will not be able to connect. You need to use a command to fix it.

redis-check-aof --fix

AOF also has three triggering mechanisms
(1) Synchronization always for every modification: Synchronization and persistence will be recorded to the disk immediately every time a data change occurs. Poor performance but better data integrity

(2) Synchronous everysec per second: asynchronous operation, recording every second if there is data loss if the machine goes down within one second

(3) Different no: never synchronize

The default of AOF is to synchronize once per second

appendfsync everysec

Advantages:
1. Every modification is synchronized, and the integrity of the file will be better.
2. It will be synchronized once a second, and at most one second of data will be lost.
3. Even if the AOF log file is too large, it will not be rewritten in the background. Will affect the client's read and write.

Disadvantages:
1. Compared with data files, AOF is much larger than RDB, and the repair speed is also slower than RDB
2. There have been bugs in AOF before, which means that the same data is not restored when data is restored through the log recorded by AOF. come out.

reference

Redis actual combat
https://baijiahao.baidu.com/s?id=1654694618189745916&wfr=spider&for=pc
https://www.bilibili.com/video/BV1S54y1R7SB

Guess you like

Origin blog.csdn.net/qq_43610675/article/details/113743182