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操作造成堵塞,而且该操作不会出现在慢查询中)