Video in progress

Strong Junior ⚡ Redis and caching

What is the difference between the DEL and UNLINK commands?

Both delete a key, but DEL frees the memory immediately on the main thread, while UNLINK makes the key invisible right away and frees the memory in a background thread. For big keys, UNLINK doesn't block Redis.

📖 Answer

For a small String there's no difference. But if you DEL a Set or Hash with 5 million elements, Redis is busy for a few hundred milliseconds freeing every element, and all clients wait.

UNLINK shop:recently-viewed:all     # returns immediately

There are similar settings too:

lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-user-del yes   # DEL behaves like UNLINK

In Redis 7.x these settings default to no, while in Valkey 8 most of them are on by default. Check your version with CONFIG GET lazyfree*.

Spring Data Redis has redisTemplate.unlink(key). FLUSHDB also has an ASYNC variant: FLUSHDB ASYNC.

Big keys are not a good sign in general. Look for them with --bigkeys and split them into smaller pieces where you can.