Question: How does deletion operation affect performance in Redis?
Answer
Deleting keys in Redis can be a costly operation, especially when dealing with a large number of keys or large-size values. It's important to understand how it can impact performance.
- Single Deletion (
DEL): This is a simple operation that will immediately delete a key from Redis. It's anO(1)complexity operation, meaning it takes constant time regardless of the number of keys in the database. However, if the value stored at the key is a large data structure, the operation could take more time due to memory management overhead.
DEL mykey- Bulk Deletion (
UNLINK): Introduced in Redis 4.0,UNLINKis a command that deletes keys in a non-blocking manner. WhileDELfrees the memory associated with a key synchronously,UNLINKdoes this asynchronously. Meaning, it returns control to the client before the memory is actually freed, thereby causing a less immediate impact on performance.
UNLINK key1 key2 key3- Pattern-based Deletion (
KEYSandDEL): When you need to delete multiple keys matching a certain pattern, you might use theKEYScommand to find these keys and then delete them usingDEL. However, this operation can block the server for a long time if the server has a lot of data, asKEYSscans the entire key space.
EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 pattern*This script uses Lua scripting feature of Redis to delete keys matching a pattern in a single operation, which can be faster than multiple round trips. However, this can still cause high CPU load and should be used carefully.
- Scan & Delete (
SCANandDEL): A more performance-friendly way to delete keys matching a pattern is by using theSCANcommand withDEL.SCANretrieves keys in a cursor-based iterative manner, not blocking the server likeKEYS.
# Loop until the SCAN cursor returns 0
CURSOR=0
while [ $CURSOR -ne 0 ]; do
RESPONSE=$(redis-cli SCAN $CURSOR MATCH "pattern*")
# The first element of the response is the next cursor
CURSOR=$(echo $RESPONSE | cut -d' ' -f1)
# Remaining elements are the keys matching the pattern
KEYS=$(echo $RESPONSE | cut -d' ' -f2-)
# If any keys were found, delete them
if [ ! -z "$KEYS" ]; then
redis-cli DEL $KEYS
fi
doneTake note, frequent deletions of large amounts of data could lead to memory fragmentation over time. This might degrade overall Redis performance as it could increase memory usage or slow down certain operations.
Was this content helpful?
Help us improve by giving us your feedback.
Other Common Redis Questions (and Answers)
- How to check if Redis is running?
- How to restart Redis?
- Does Redis persist data?
- How to stop Redis server?
- How to see Redis version?
- How long does Redis store data?
- How is Redis so fast?
- How to connect to Redis server?
- Is Redis Multithreaded?
- Does Redis Use HTTP?
- How to Refresh Redis Cache in Spring Boot?
- How to Use Redis with Django?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost