Redis persistence related notes

RDB: Snapshot storage of data on a regular basis. (Equivalent to data snapshot)
AOF: Save the write command to a file. When Redis is restarted, the program can restore the data by re-executing the commands in the AOF file. (Equivalent to operation log)

 

Stand-alone redis has 16 libraries, the default is library 0

Principle of master-slave replication : roughly the master sends the rdb file to the slave

RDB principle : redis will fork a child process, periodically write the data in the current memory to the disk, and generate a snapshot. When restoring, the snapshot file is directly read into the memory.
Triggering mechanism: 1. shutdown, if aof is not turned on, it will be triggered; 2. execute save or bgsave; 3. execute flushall; 4. configuration file configuration trigger
problem: unexpected downtime, Some data will be lost
Advantages: 1. Small files; 2. Fast recovery;
Note: rdb is enabled by default. If aof is enabled, you can delete the rdb configuration to improve performance, or just keep "900 1". If you enable it at the same time, AOF first


AOF : The AOF
principleis generally used in the company: redis will open the buffer, the main process writes commands into the buffer, and the buffer timing task writes the data into the aof file. Record write operation logs (additions, deletions and changes), and execute commands during recovery.
How to enable: appendonly yes
Trigger mechanism: appendfsync everysec (default-every second, may lose one second of data), always (real-time-data will not be lost but efficient Too low)
Advantage: Data security
Question: 1. The file is too large, and the file bursts the disk? "Answer: The default is 64M, and AOF rewrite is performed. For the file slimming plan, do not honestly record the log, and thin the original AOF according to the memory data. For example, incr key1 1, incr key1 2 will be merged into set key1 2. But it cannot be defaulted to 64M. , Small companies also need 5G or more, and large companies 100G is also possible, because rewriting will fork the sub-process and affect performance.”


After 4.0, the hybrid persistence mechanism (RDB+AOF optimizes the AOF rewrite):
Principle: The child process of fork first writes the full amount of the shared memory copy to the AOF file in RDB mode, and then rewrites the buffer Incremental commands are written to the file in AOF mode and
saved in AOF segments. RDB before rewriting and AOF after rewriting. The advantage is that the file size is reduced.
Advantages: fast and safe

 

Deletion strategy:
Timed deletion: equivalent to timer countdown deletion.
Periodic deletion: equivalent to polling and random extraction deletion.
Lazy deletion: Data will not be processed when it reaches the expiration time. When the data is accessed next time, if it has not expired, return the data; if it is found to be expired, delete

Redis use: lazy deletion + regular deletion

Eviction algorithm (what to do if the memory is full):
Before executing each command, redis will check whether the memory is sufficient (whether it is greater than maxmemory, usually set to 50% or more), if not enough, redis will select some (maxmemory-samples, select The number of data to be deleted) According to the eviction policy (maxmemory-policy), select a part of the data to be cleaned up

maxmemory-policy:
LRU (The Least Recently Used, the least recently used algorithm)
LFU (Least Frequently Used, the least recently used algorithm)

1) Detect volatile data (set the expiration time key)
    volatile-lru: select the least recently used data and eliminate (default) have expiration time + the longest unused
    volatile-lfu: have expiration time + the least recently unused
    volatile-ttl :Select the data that will expire and eliminate the expiration time + the fastest expiration
    volatile-random: Choose the data arbitrarily and eliminate the expiration time + random


2) Detect all database data (all keys)
    allkeys-lru: select the least recently used data and eliminate all keys + the least recently used
    allkeys-lfu: all keys + the least recently unused
    allkeys-random: choose any data to eliminate all keys + random


3) Give up data to expel 
    no-enviction: give up eviction, report an error

Guess you like

Origin blog.csdn.net/weixin_38230961/article/details/108586010