There are three commonly used deletion strategies in Redis:
1. Passive deletion (lazy deletion): when a read/write item has expired When a Key is found, the lazy deletion strategy will be triggered and the Key will be deleted directly;
2. Active deletion (regular deletion): Redis will perform regular inspections to clean up expired keys;
3. When the memory reaches the maxmemory configuration, the key deletion operation will be triggered;
Active deletion
In Redis, regular operations are implemented by redis.c/serverCron , it mainly performs the following operations:
1. Update various statistical information of the server, such as time, memory usage, database usage, etc.
2. Clean up expired key-value pairs in the database.
3. Adjust the size of the unreasonable database.
4. Close and clean up clients with failed connections.
5. Try to perform AOF or RDB persistence operation.
6. If the server is the master node, perform regular synchronization of the slave nodes.
If you are in cluster mode, perform regular synchronization and connection tests on the cluster.
Redis runs serverCron as a time event to ensure that it will run automatically every once in a while. And because serverCron needs to run regularly while the Redis server is running, it is a cyclic time event: serverCron It will be executed periodically until the server is shut down.
Summary
If a large number of Keys expire in Redis every day (for example, tens of millions), then you must consider the cleanup of expired Keys:
Increase the frequency of Redis active cleaning (by increasing the hz parameter)
Manually clean up expired keys. The simplest way is to perform a scan operation. The scan operation will trigger the first passive deletion. Don’t forget to do the scan operation. Add count; the number of Keys returned by the
dbsize command includes expired Keys. The keys returned by the
randomkey command do not include expired Keys. The keys returned by the
scan command include expired keys. Key
info command returns the # Keyspace
db6:keys=1034937352, expires=994731489,avg_ttl=507838502
The number of keys corresponding to keys is equal to dbsize
expires refers to the number of Keys with expiration time set
avg_ttl refers to the average expiration time of the Key with expiration time set (unit: milliseconds)
The above is the detailed content of How to delete data in redis. For more information, please follow other related articles on the PHP Chinese website!