Data recovery from server crash using redis
Because redis is stored in memory and provides data structure storage types commonly used in general programming languages, it is often used for data recovery processing when servers crash.
The server can store the data that needs to be saved in redis in the form of json objects in certain specified processes, which is what we often call snapshots. When the server is running, redis is read to determine whether it needs to be restored. The data continues to be processed.
Just delete the redis data after a business process is completed.
Redis provides two methods for exporting memory data to the hard disk for data backup:
RDB method (default)
The persistence of RDB method is through snapshotting (snapshotting) Completed, when certain conditions are met, Redis will automatically snapshot all data in the memory and store it on the hard disk. The conditions for taking a snapshot can be customized by the user in the configuration file and consist of two parameters: time and the number of changed keys. A snapshot will be taken when the number of keys changed within the specified time is greater than the specified value. RDB is the default persistence method used by redis. Three conditions have been preset in the configuration file:
save 900 1 # If at least 1 key is changed within 900 seconds, a snapshot will be taken
save 300 10 # Take a snapshot if at least 10 keys have been changed within 300 seconds
save 60 10000 # Take a snapshot if at least 10,000 keys have been changed within 60 seconds
There can be multiple There are several conditions, and there is an "OR" relationship between the conditions. As long as one of the conditions is met, a snapshot will be taken. If you want to disable automatic snapshots, just delete all save parameters.
Redis will store the snapshot file in the dump.rdb file in the current directory (which can be viewed by CONFIG GET dir) by default. You can specify the storage path and file name of the snapshot file by configuring the dir and dbfilename parameters respectively. .
The process of Redis implementing snapshots
Redis uses the fork function to copy a copy (child process) of the current process (parent process);
The parent process continues to receive and process customers The command is sent from the terminal, and the child process starts to write the data in the memory to the temporary file on the hard disk;
When the child process finishes writing all the data, the old RDB file will be replaced with the temporary file, so far A snapshot operation is completed.
When executing fork, the operating system (Unix-like operating system) will use the copy-on-write strategy. That is, the parent and child processes share the same memory data at the moment the fork function occurs. When the parent process wants to When a certain piece of data is changed (such as executing a write command), the operating system will copy the piece of data to ensure that the data of the child process is not affected, so the new RDB file stores the memory data at the moment the fork is executed.
Redis will not modify the RDB file during the snapshot process. It will replace the old file with a new one only after the snapshot is completed, which means that the RDB file is complete at any time. This allows us to implement Redis database backup by regularly backing up RDB files. The RDB file is a compressed binary format (the rdbcompression parameter can be configured to disable compression to save CPU usage), so the space occupied will be smaller than the data size in the memory, making it easier to transmit.
In addition to automatic snapshots, you can also manually send the SAVE or BGSAVE command to let Redis perform the snapshot. The difference between the two commands is that the former is performed by the main process and will block other requests, while the latter will be performed through fork The child process performs snapshot operations. After Redis is started, it will read the RDB snapshot file and load the data from the hard disk to the memory. This time varies depending on the size and structure of the data and server performance. It usually takes 20 to 30 seconds to load a 1GB snapshot file that records 10 million string type keys into memory. Persistence is achieved through RDB. Once Redis exits abnormally, all data changed after the last snapshot will be lost. This requires developers to control possible data loss within an acceptable range by combining and setting automatic snapshot conditions based on specific application scenarios. If the data is so important that it cannot afford any loss, you can consider using the AOF method for persistence.
AOF method
By default, Redis does not enable AOF (append only file) persistence. It can be enabled through the appendonly parameter in redis.conf:
appendonly yes
When starting, Redis will execute the commands in the AOF file one by one to load the data in the hard disk into the memory. The loading speed will be slower than RDB.
Enable AOF persistence Every time a command is executed that changes the data in Redis, Redis will write the command to the AOF file on the hard disk. The saving location of the AOF file is the same as the location of the RDB file, both are set through the dir parameter. The default file name is appendonly.aof, which can be modified through the appendfilename parameter:
appendfilename appendonly.aof
Configure the conditions for redis to automatically rewrite AOF files
auto-aof-rewrite-percentage 100 # When the current AOF file size exceeds the percentage of the AOF file size during the last rewrite, it will be performed again Rewrite, if it has not been rewritten before, it will be based on the AOF file size at startup
auto-aof-rewrite-min-size 64mb #The minimum AOF file size allowed to be rewritten
Configure the mechanism that requires the system to refresh the hard disk cache after writing the AOF file
# appendfsync always # Synchronization will be performed every time a write is performed, which is the safest and slowest
appendfsync everysec # Perform a synchronization operation every second
# appendfsync no # Does not actively perform synchronization operations, but leaves it entirely to the operating system (i.e. once every 30 seconds), which is the fastest and least secure
Redis allows AOF and RDB to be opened at the same time, which not only ensures data security but also makes backup and other operations very easy. After restarting Redis at this time, Redis will use the AOF file to restore the data, because AOF persistence may lose less data
redis = require('redis'),//导入js模块 RDS_PORT = , //端口号 RDS_HOST = '', //服务器IP RDS_OPTS = {}, //设置项 redisdb = redis.createClient(RDS_PORT, RDS_HOST, RDS_OPTS);//创建连接 redisdb.select(20);//指定分区库 redisdb.on('ready', function (res) { console.log('ready'); }); redisdb.on('connect', function () { console.log('connect'); }); exports.redisdb = redisdb; function redis_opt(opt, key, value, callback) { if (opt == 'get') { redisdb.get(key, function (err, data) { if (err == null) { callback(data); } else { callback(err); } }); } else if (opt == 'set') { redisdb.set(key,value, function (err,result) { if (err == null) { callback(result); } else { callback(err); } }); } else if (opt == 'del') { redisdb.del(key, function (err, result) { if (err == null) { callback(result); } else { callback(err); } }); } else { callback("error opt!"); } } function update(key) { redis_opt("get", key, null, function (data) { console.log("the redis data is " + data); if (data) { count = parseInt(data); redis_opt("set", key, ++count , function (data) { console.log("set " + count + " " + data); }); } else { redis_opt("set", key, 10000, function (data) { console.log("set " + 10000 + " " + data); }); } }); } function clear(key) { redis_opt("del", key, null, function (ret) { console.log("del " + key + " " + ret); }); } function main() { var key = "count_test"; setInterval(function () { clear(key) }, 5000); setInterval(function () { update(key) }, 1000); } //testmain(); main();
The above code is a simple timer function, that is, it is read regularly after the server is started. The redis data is cumulatively modified if it exists, and initialized if it does not exist. At the same time, for the convenience of explanation, a timer is set to delete data regularly.
For more redis knowledge, please pay attention to the redis introductory tutorial column.
The above is the detailed content of Data recovery from server crash using redis. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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 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.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
