Redis BigKey场景实战

声明以下数据均产生于本人办公电脑,配置不同性能表现会有差异。

一:100w条字符串数据执行keys *需要多少时间。

首先先写一个单元测试,生成100w条数据。

java 复制代码
@SpringBootTest
class RedisJedisApplicationTests {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Test
    void testRedis100w() {
        for (int i = 0; i < 100 * 10000; i++) {
            String key = "key:"+i;
            String value = "value:"+i;
            redisTemplate.opsForValue().set(key,value);
        }
    }
}

连接上redis执行。

居然这么长时间,所以keys * 在大数据量下执行是超级危险的。

二:不能使用keys* ,如何遍历固定前缀的key。

可使用scan命令进行遍历。

powershell 复制代码
SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]

**cursor:**游标参数。首次调用时设为0,后续使用返回的游标值。当返回游标为0时表示迭代完成。

**MATCH pattern:**可选,匹配键名的模式(如user:)。支持 、?等通配符,但需注意非精确匹配可能返回重复键。
COUNT count :可选,单次迭代返回的键数量(默认10)。实际返回数量可能略多或略少,需结合业务容忍度调整。

**TYPE type:**可选(Redis 3.2+),按数据类型过滤(如TYPE string、TYPE hash)。

powershell 复制代码
# Redis CLI示例:匹配以"user:"开头的键
SCAN 0 MATCH user:* COUNT 500

三:生成环境如何避免keys *、flushall、flushdb被调用。

可在redis配置文件中配置rename-command将相关的危险命令禁用。如下。

rename-command keys ""

rename-command flushdb ""

rename-command flushall ""

四:memory usage如何使用。

MEMORY USAGE 是 Redis 中用于查询指定键(key)内存占用情况的命令,其使用方法及核心要点如下:

powershell 复制代码
MEMORY USAGE key [SAMPLES count]

**key:**需查询内存占用的键名。
SAMPLES count(可选) :针对嵌套数据类型(如 Hash、List、Set、ZSet)的抽样数量,默认值为 5。若需遍历所有元素,可设为 0。
返回一个整数,表示该键及其值在内存中占用的字节数(包括数据本身和管理开销)。

五:如何发现bigkey?

可以使用redis-cli扫描bigkey,结合memory usage分析,找到目标bigkey做后续处理。

事先说明 我插入了一个bighash:001 有10w个field 和 一个biglist:001 有10witem。

powershell 复制代码
D:\app_software\Redis7.0.4\bin>redis-cli.exe --bigkeys
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).

[00.00%] Biggest string found so far '"key:53444"' with 13 bytes
[00.00%] Biggest string found so far '"key:182408"' with 14 bytes
[17.04%] Biggest hash   found so far '"customer:001"' with 4 fields
[62.28%] Biggest hash   found so far '"bighash:001"' with 100000 fields
[87.53%] Biggest list   found so far '"biglist:001"' with 100000 items
[100.00%] Sampled 1000000 keys so far

-------- summary -------

Sampled 1000003 keys in the keyspace!
Total key length in bytes is 9888924 (avg len 9.89)

Biggest   list found '"biglist:001"' has 100000 items
Biggest   hash found '"bighash:001"' has 100000 fields
Biggest string found '"key:182408"' has 14 bytes

1 lists with 100000 items (00.00% of keys, avg size 100000.00)
2 hashs with 100004 fields (00.00% of keys, avg size 50002.00)
1000000 strings with 13888890 bytes (100.00% of keys, avg size 13.89)
0 streams with 0 entries (00.00% of keys, avg size 0.00)
0 sets with 0 members (00.00% of keys, avg size 0.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00)
相关推荐
JavaEdge.7 小时前
榨干 CPU 性能:通过绑核将 Redis 尾延迟减半!
数据库·redis·缓存
YDS8298 小时前
Redis入门 —— 基本数据类型和Spring Data Redis
数据库·redis·spring
一个儒雅随和的男子8 小时前
Redis大Key调优指针
数据库·redis·缓存
光军oi12 小时前
面试Redis篇—————缓存穿透问题及解决策略
redis·缓存·面试
送秋三十五12 小时前
5分钟读懂MySQL+Redis双写一致性实现流程
数据库·redis·mysql
一个儒雅随和的男子12 小时前
Redis性能调优指南
数据库·redis·spring
江湖人称小鱼哥12 小时前
Redisson 与 Spring Boot 3.4 整合指南
spring boot·redis·后端·redission
苦学编程的谢13 小时前
Redis_1_初识Redis+浅谈分布式系统
数据库·redis·缓存