Let's talk about the persistence mechanism of Redis. Should we use RDB or AOF?
This article will take you to understand the persistence mechanism of Redis (RDB and AOF), and talk about whether to use RDB or AOF? I hope to be helpful!
RDB
#1. What is RDB
#RDB: Every once in a while, the The data is written to a temporary file on the disk as a snapshot, and the snapshot file is read into the memory during recovery. If it crashes and restarts, the data in the memory will definitely be gone. Then it will be restored after starting redis again. [Related recommendations: Redis video tutorial]
2. Backup and recovery
Memory backup--> Disk temporary file
Temporary File --> Restore to memory
3. RDB advantages and disadvantages
- Advantages
Back up every once in a while, Full backup
Disaster recovery is simple and can be transmitted remotely
When the child process is backed up, the main process will not have any io operations (no (write, modify or delete) to ensure the integrity of the backup data
Compared with AOF, when there are larger files, you can quickly restart and restore
- Disadvantages
When a failure occurs, the last backup data may be lost
The memory ratio occupied by the child process It will be exactly the same as the parent process. If it will cause CPU burden
Since scheduled full backup is a heavyweight operation, real-time backup cannot be processed.
4. RDB configuration
The saving location can be set in redis.conf Definition:
/user/local/redis/working/dump.rdbSaving mechanism:
save 900 1 save 300 10 save 60 10000 save 10 3
* 如果1个缓存更新,则15分钟后备份 * 如果10个缓存更新,则5分钟后备份 * 如果10000个缓存更新,则1分钟后备份
stop-writes-on-bgsave-error
- yes: If an error occurs during the save process, stop the write operation
- no: May cause data inconsistency
rdbcompression
- yes: Turn on rdb compression mode
- no: Turn it off, which will save CPU consumption, but the file will be larger, the same reason as nginx
rdbchecksum
- yes: Use CRC64 algorithm verification to perform data verification on rdb, with a 10% performance loss
- no: No verification Verification
Summary
RDB is suitable for the recovery of large amounts of data, but the integrity and consistency of the data may be insufficient.
AOF
AOF features
Record write operations requested by users in the form of logs. Read operations are not logged, as only write operations are stored.
The file is appended rather than modified.
Redis' aof recovery is actually to read and write the appended file from the beginning to the end.
Advantages
AOF is more durable and can be backed up in seconds. If a problem occurs, it will only Losing the last second of data greatly increases reliability and data integrity. So AOF can be backed up once per second, using fsync operation.
Append in the form of log log. If the disk is full, the redis-check-aof tool will be executed.
When the data is too large, Redis can automatically rewrite aof in the background. When redis continues to append logs to old files, rewriting is also very safe and will not affect the client's read and write operations.
All write operations contained in the AOF log will make it easier to parse and recover redis.
Disadvantages
The same data, the same data, AOF is larger than RDB
For different synchronization mechanisms, AOF will be slower than RDB, because AOF will back up and do write operations every second, which is slightly lower than RDB. There is nothing wrong with backing up fsync every second, but if the client performs a backup fsync every time it writes, the performance of redis will decrease.
A bug has occurred in AOF, that is, the data is incomplete during data recovery. This makes AOF more fragile and prone to bugs, because AOF is not as simple as RDB, but in order to prevent bugs generated, AOF will not be reconstructed based on the old instructions, but will be reconstructed based on the data instructions existing in the cache at that time, which makes it more robust and reliable.
AOF configuration
`# AOF 默认关闭,yes可以开启 appendonly no # AOF 的文件名 appendfilename "appendonly.aof" # no:不同步 # everysec:每秒备份,推荐使用 # always:每次操作都会备份,安全并且数据完整,但是慢性能差 appendfsync everysec # 重写的时候是否要同步,no可以保证数据安全 no-appendfsync-on-rewrite no # 重写机制:避免文件越来越大,自动优化压缩指令,会fork一个新的进程去完成重写动作,新进程里的内存数据会被重写,此时旧的aof文件不会被读取使用,类似rdb # 当前AOF文件的大小是上次AOF大小的100% 并且文件体积达到64m,满足两者则触发重写 auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb`
Should we use RDB or AOF?
If you can accept cache loss for a period of time, you can use RDB
If you are more careful about real-time data , then use AOF
Use RDB and AOF together for persistence. RDB is used as cold backup, which can restore different versions at different times, and AOF is used as hot backup to ensure that data is only lost for 1 second. When the AOF is damaged and unavailable, then use RDB to restore it, so that the two are combined. That is to say, Redis recovery will load AOF first, and if there is a problem with AOF, it will load RDB again, thus achieving the purpose of hot and cold backup. .
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of Let's talk about the persistence mechanism of Redis. Should we use RDB or AOF?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.
