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,UNLINK
is a command that deletes keys in a non-blocking manner. WhileDEL
frees the memory associated with a key synchronously,UNLINK
does 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 (
KEYS
andDEL
): When you need to delete multiple keys matching a certain pattern, you might use theKEYS
command 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, asKEYS
scans 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 (
SCAN
andDEL
): A more performance-friendly way to delete keys matching a pattern is by using theSCAN
command withDEL
.SCAN
retrieves 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 done
Take 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?
Other Common Redis Questions (and Answers)
- What is the default port used by Redis?
- How to start Redis server?
- Is Redis persistent?
- How fast is Redis?
- How to install Redis on Mac?
- How to check if Redis is running?
- How to restart Redis?
- Does Redis persist data?
- How to install Redis on Ubuntu?
- How to stop Redis server?
- How to see Redis version?
- Does Redis have tables?
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