Databases 9 min read

How to Identify and Safely Remove Redis Big Keys: Tips, Commands, and Code

This article explains what constitutes a Redis big key, the performance problems they cause, and provides practical methods—including redis-cli, SCAN, and RdbTools—to locate them and safely delete them using batch or asynchronous techniques with example Python code.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
How to Identify and Safely Remove Redis Big Keys: Tips, Commands, and Code

What is a Redis big key?

A big key refers to a key whose value is large, not the key name itself.

Two typical cases are considered big keys:

String values larger than 10 KB;

Hash, List, Set, or ZSet with more than 5 000 elements.

Problems caused by big keys

Big keys can lead to four main issues:

Client timeout and blocking : Redis processes commands single‑threadedly; a long‑running operation on a big key blocks the server and makes clients wait.

Network congestion : Transferring a large key generates heavy traffic; e.g., a 1 MB key accessed 1 000 times per second creates 1 GB/s of traffic.

Worker thread blockage : Deleting a big key with DEL blocks the worker thread, preventing subsequent commands.

Uneven memory distribution : In a clustered setup, nodes holding big keys consume more memory and QPS, causing data and query skew.

How to find big keys

1. redis-cli --bigkeys

Run the command on a replica to avoid blocking the master:

redis-cli -h 127.0.0.1 -p 6379 -a "password" -- bigkeys

Notes:

Prefer executing on a slave node.

If no slave is available, run during low‑traffic periods or use the -i option to throttle scanning.

Limitations:

Only the largest key of each type is returned.

For collection types it reports element count, not actual memory usage.

2. SCAN command

Iterate over keys with SCAN, check each key’s type, and measure size:

String: STRLEN gives the byte length.

Collections: use LLEN, HLEN, SCARD, ZCARD if average element size is known, or MEMORY USAGE (Redis 4.0+) for actual memory consumption.

3. RdbTools

Parse an RDB snapshot with the open‑source tool:

rdb dump.rdb -c memory --bytes 10240 -f redis.csv

How to delete big keys safely

Two recommended approaches:

Batch deletion

Asynchronous deletion (Redis 4.0+)

1. Batch deletion

Delete large hashes with HSCAN + HDEL in chunks of 100 fields:

def del_large_hash():
    r = redis.StrictRedis(host='redis-host1', port=6379)
    large_hash_key = "xxx"
    cursor = '0'
    while cursor != 0:
        cursor, data = r.hscan(large_hash_key, cursor=cursor, count=100)
        for item in data.items():
            r.hdel(large_hash_key, item[0])

Delete large lists with LTRIM to remove the rightmost 100 elements repeatedly:

def del_large_list():
    r = redis.StrictRedis(host='redis-host1', port=6379)
    large_list_key = 'xxx'
    while r.llen(large_list_key) > 0:
        r.ltrim(large_list_key, 0, -101)

Delete large sets with SSCAN + SREM:

def del_large_set():
    r = redis.StrictRedis(host='redis-host1', port=6379)
    large_set_key = 'xxx'
    cursor = '0'
    while cursor != 0:
        cursor, data = r.sscan(large_set_key, cursor=cursor, count=100)
        for item in data:
            r.srem(large_set_key, item)

Delete large sorted sets with ZREMRANGEBYRANK to remove the top 100 elements:

def del_large_sortedset():
    r = redis.StrictRedis(host='large_sortedset_key', port=6379)
    large_sortedset_key = 'xxx'
    while r.zcard(large_sortedset_key) > 0:
        r.zremrangebyrank(large_sortedset_key, 0, 99)

2. Asynchronous deletion

From Redis 4.0 onward, use UNLINK instead of DEL. The key is freed in a background thread, avoiding main‑thread blockage.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIPythonBig Key
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.