Question: How does deletion operation affect performance in Redis?


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.

  1. Single Deletion (DEL): This is a simple operation that will immediately delete a key from Redis. It's an O(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
  1. Bulk Deletion (UNLINK): Introduced in Redis 4.0, UNLINK is a command that deletes keys in a non-blocking manner. While DEL 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
  1. Pattern-based Deletion (KEYS and DEL): When you need to delete multiple keys matching a certain pattern, you might use the KEYS command to find these keys and then delete them using DEL. However, this operation can block the server for a long time if the server has a lot of data, as KEYS scans the entire key space.
EVAL "return'del', unpack('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.

  1. Scan & Delete (SCAN and DEL): A more performance-friendly way to delete keys matching a pattern is by using the SCAN command with DEL. SCAN retrieves keys in a cursor-based iterative manner, not blocking the server like KEYS.
# 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?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.