Redis大key治理

redis大key定义

拒绝BigKey防止网卡流量,慢查询。

阿里云对大key的定义

String类型控制在10kb以内,hash,list,set,zset元素个数不用超过5000

大key排查

#!/bin/bash

# Connect to your Redis server
REDIS_CLI="/path/to/your/redis-cli"
REDIS_HOST="your_redis_host"
REDIS_PORT="your_redis_port"
REDIS_PASSWORD="your_redis_password"  # Only if Redis requires authentication

# Define the threshold in bytes (10K = 10000 bytes)
THRESHOLD=10000

# Function to get keys larger than the threshold
get_big_keys() {
    big_keys=$($REDIS_CLI -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASSWORD --no-raw SCAN 0 MATCH "*" COUNT 1000 | awk 'NR % 2 == 0')
    for key in $big_keys
    do
        key_size=$($REDIS_CLI -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASSWORD memory usage "$key")
        if [ $key_size -gt $THRESHOLD ]; then
            echo "$key" >> bigkey.txt
        fi
    done
}

# Clear the bigkey.txt file if it already exists
if [ -f "bigkey.txt" ]; then
    rm bigkey.txt
fi

# Call the function to get big keys and save them in bigkey.txt
get_big_keys

echo "Big keys (>10K) have been saved to bigkey.txt"

治理方案

非字符串的BigKey不要使用del删除,使用hscan,sscan,zscan方式渐进式删除,同时注意防止BigKey过期时间自动删除(类似100万的zset设置固定时间过期,会触发del操作造成堵塞,而且该操作不会出现在慢查询中)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值