侧边栏壁纸
博主头像
Terry

『LESSON 5』

  • 累计撰写 90 篇文章
  • 累计创建 21 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Redis集群string/hash测试

Terry
2020-12-26 / 0 评论 / 0 点赞 / 770 阅读 / 4,080 字 / 正在检测是否收录...

Redis集群string/hash测试

名称信息
Redis版本6.0.9
OSLinux
master节点端口9002,9003,9004
slave节点端接口9001,9005,9006

以下计算只需要计算master节点,slave节点不必计算

初始化占用内存

[116.198.162.25:9005]   VIRT: 2.59M     RSS: 13.36M     Peak: 122.72M   Max: 0B System: 15.51G
[116.198.162.25:9004]   VIRT: 2.58M     RSS: 16.43M     Peak: 141.75M   Max: 0B System: 15.51G
[116.198.162.25:9002]   VIRT: 2.60M     RSS: 13.95M     Peak: 136.43M   Max: 0B System: 15.51G
[116.198.162.25:9001]   VIRT: 2.59M     RSS: 15.24M     Peak: 134.04M   Max: 0B System: 15.51G
[116.198.162.25:9006]   VIRT: 2.60M     RSS: 15.65M     Peak: 74.17M    Max: 0B System: 15.51G
[116.198.162.25:9003]   VIRT: 2.64M     RSS: 15.87M     Peak: 94.24M    Max: 0B System: 15.51G

总内存:2.58+2.60+2.64 = 7.82m

7位纯数字测试

value

[116.198.162.25:9005]   VIRT: 21.74M    RSS: 29.18M     Peak: 122.72M   Max: 0B System: 15.51G
[116.198.162.25:9004]   VIRT: 37.13M    RSS: 47.61M     Peak: 141.75M   Max: 0B System: 15.51G
[116.198.162.25:9002]   VIRT: 40.73M    RSS: 51.83M     Peak: 136.43M   Max: 0B System: 15.51G
[116.198.162.25:9001]   VIRT: 37.11M    RSS: 44.16M     Peak: 134.04M   Max: 0B System: 15.51G
[116.198.162.25:9006]   VIRT: 34.99M    RSS: 42.93M     Peak: 74.17M    Max: 0B System: 15.51G
[116.198.162.25:9003]   VIRT: 37.16M    RSS: 46.55M     Peak: 94.24M    Max: 0B System: 15.51G

总内存:37.13+40.73+37.16-7.82 = 107.2m
计算:每天加一个k-v增加112字节,
112*1000000 = 106.81m

hash

[116.198.162.25:9005]   VIRT: 5.95M     RSS: 15.68M     Peak: 122.72M   Max: 0B System: 15.51G
[116.198.162.25:9004]   VIRT: 5.84M     RSS: 18.08M     Peak: 141.75M   Max: 0B System: 15.51G
[116.198.162.25:9002]   VIRT: 5.94M     RSS: 18.78M     Peak: 136.43M   Max: 0B System: 15.51G
[116.198.162.25:9001]   VIRT: 5.84M     RSS: 16.66M     Peak: 134.04M   Max: 0B System: 15.51G
[116.198.162.25:9006]   VIRT: 5.90M     RSS: 14.41M     Peak: 74.17M    Max: 0B System: 15.51G
[116.198.162.25:9003]   VIRT: 5.95M     RSS: 16.68M     Peak: 94.24M    Max: 0B System: 15.51G

总内存:5.94+5.95+5.84-7.82 = 9.91m
计算:每添加一个key-entry增加128字节(hash表本身也需要带有一些信息),每添加内部key-value增加8字节(int类型,4+4=8),
128*1001 - 8*1001 + 8*1000000 = 7.74m

string测试,默认value: 18888888888terryterryterryterryterryterryterryterry 51B

value

[116.198.162.25:9005]   VIRT: 54.91M    RSS: 62.37M     Peak: 122.72M   Max: 0B System: 15.51G
[116.198.162.25:9004]   VIRT: 54.96M    RSS: 62.77M     Peak: 141.75M   Max: 0B System: 15.51G
[116.198.162.25:9002]   VIRT: 54.90M    RSS: 62.70M     Peak: 136.43M   Max: 0B System: 15.51G
[116.198.162.25:9001]   VIRT: 54.94M    RSS: 62.37M     Peak: 134.04M   Max: 0B System: 15.51G
[116.198.162.25:9006]   VIRT: 54.92M    RSS: 60.97M     Peak: 74.17M    Max: 0B System: 15.51G
[116.198.162.25:9003]   VIRT: 54.95M    RSS: 61.38M     Peak: 94.24M    Max: 0B System: 15.51G
总内存:54.96+54.90+54.95-7.82 = 156.99 
计算:每添加一个k-v增加168字节
168*1000000 = 160.22m

hash

[116.198.162.25:9005]   VIRT: 21.24M    RSS: 37.23M     Peak: 122.72M   Max: 0B System: 15.51G
[116.198.162.25:9004]   VIRT: 20.54M    RSS: 37.73M     Peak: 141.75M   Max: 0B System: 15.51G
[116.198.162.25:9002]   VIRT: 21.17M    RSS: 38.38M     Peak: 136.43M   Max: 0B System: 15.51G
[116.198.162.25:9001]   VIRT: 20.53M    RSS: 34.56M     Peak: 134.04M   Max: 0B System: 15.51G
[116.198.162.25:9006]   VIRT: 20.90M    RSS: 35.75M     Peak: 74.17M    Max: 0B System: 15.51G
[116.198.162.25:9003]   VIRT: 20.96M    RSS: 35.92M     Peak: 94.24M    Max: 0B System: 15.51G
总内存:20.54+21.17+20.96-7.82 = 54.85m
计算:128*1001 + (51+4)*1000000 = 52.57m

总结

1.纯数字类型object encoding=int,用普通方式存每个k-v需要112字节,本身只需要8字节现在额外多了100多字节String类型需要额外的内存空间记录数据长度、空间使用 等元数据,当缓存的数据较小,元数据的空间开销很大,显得喧宾夺主。,浪费空间

而使用hash来存,object encoding=ziplist,k-v占用8字节。所以数据越小使用hash削过越明显

2.一个键值对占用内存120字节左右,如果用hash能够将低到50字节以下,减少两倍内存占用。不过以下几点导致无法使用hash

  • 集群环境下无法使用Redis事务,从而不能保证hset后再设置expire原子性
  • 如果直接hset的话,所用的时间和set一样,但是hset还要执行一次expire,导致所用时间翻倍。

附带上一个好用的Redis集群脚本

redis-tools

0

评论区