iTranslated by AI
Redis: Are You Still Exhausted by the DEL Command?
Apologies for the provocative title.
Blocking due to Redis Data Deletion
In Redis, client-initiated data deletions up to v3 series included DEL, FLUSHALL, and FLUSHDB.
Additionally, Redis's own deletion mechanisms included EXPIRE when TTL (Time To Live) expired, EVICTION when memory overflowed, and flush when a Replica performed a Full Sync.
In particular, DEL and FLUSH-related commands block operations proportionally to the number of data items to be deleted. When dealing with SET or HASH types with a very large number of elements, even deleting a single key with DEL can halt processing for several seconds.
Lazy Free Feature
From Redis v4 onwards, UNLINK (corresponding to DEL) and async option for FLUSH-related commands were added.
These commands and options enable lazy memory release. This allows data deletion with minimal blocking during key deletion.
redis> SET key1 "Hello"
"OK"
redis> SET key2 "World"
"OK"
redis> UNLINK key1 key2 key3
(integer) 2
redis> GET KEY1
(nil)
There is a trade-off in terms of worsened predictability. With Lazy Free, delayed memory release naturally causes Redis's memory consumption to temporarily spike. If sufficient free memory (approximately equivalent to the amount of data being deleted) can be secured, it is a feature where performance can be gained for that memory.
Additionally, settings have been added to apply Lazy Free to Redis's own EXPIRE and EVICTION, as well as flushes before a Replica's Full Sync.
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
*Enable by setting to yes.
Appendix
When it's difficult to replace application's DEL usage
From Redis v6, a new setting lazyfree-lazy-user-del has been added.
When a DEL command is issued by a client, it internally performs processing similar to UNLINK.
*(As you can see from the source code, this single flag converts it to UNLINK.)
lazyfree-lazy-user-del no
*Enable by setting to yes.
Since this flag can be turned on/off online with the CONFIG SET command, it might be a good idea to try it experimentally before rewriting your application.
Discussion on Lazy Free
If you're interested in past discussions, you can follow this issue.
Lazy free of keys and databases · Issue #1748 · redis/redis · GitHub
Discussion