When the key expires or the DEL delete command is used, Redis will not only remove the object from the global hash table, but also release the memory allocated by the object. When a big key is encountered, releasing memory will cause the main thread to block.
To this end, Redis 4.0 introduced the UNLINK command, which puts the operation of releasing object memory into the bio background thread for execution. This effectively reduces main thread blocking.
Redis 6.0 goes a step further and introduces Lazy-free related configurations. When the configuration is enabled, the "release object" operation will be "executed asynchronously" within the key expiration and DEL commands.
void delCommand(client *c) { delGenericCommand(c,server.lazyfree_lazy_user_del);}void delGenericCommand(client *c, int lazy) { int numdel = 0, j; for (j = 1; j < c->argc; j++) { expireIfNeeded(c->db,c->argv[j]); // 开启 lazy free 则使用异步删除 int deleted = lazy ? dbAsyncDelete(c->db,c->argv[j]) : dbSyncDelete(c->db,c->argv[j]); ... }}
It is recommended to upgrade to at least Redis 6 and turn on Lazy-free.
The above is the detailed content of How to use lazy deletion Lazy free in Redis. For more information, please follow other related articles on the PHP Chinese website!