针对rocksdb压缩特性进行性能测试
测试目标
了解 rocksdb 关于压缩特性的性能表现。
测试准备
测试机器
系统:Centos7.9
CPU:20C,基于x86_64架构
内存:30G
磁盘:1.8T
测试服务
服务:rocksdb
版本:6.7.3
配置:使用调试模式,并已开启所有压缩库支持(默认是关闭的)
测试工具:db_bench
测试说明
rocksdb 对存储数据进行压缩的方式主要有两类:
- 数据压缩(COMPRESSION):主要通过内置压缩算法实现,在写入数据时会根据指定算法类型开启压缩。
- 分层压缩(COMPACTION):主要通过对分层的 LSM tree 结构数据进行去重合并实现,对已经写入的数据,会根据数据规模判断是否触发压缩,并确定需要压缩的等级。
数据压缩
主要对 rocksdb 内置的压缩算法性能进行对比,采用 db_bench 提供的基准测试顺序写和随机写场景,单条数据初始大小为116字节(key 16byte,value 100byte)。
分层压缩
主要对 rocksdb 的各个分层级别从压缩算法策略上进行对比,包含 level、universal 和 fifo 三种,默认分层级别为7个,L0 级别不压缩,故由 L1 开始,并设置级别递增梯度为2倍,关闭算法压缩(便于单一维度观察),基准测试场景采用顺序写+随机写。
【说明】
- Leveled Compaction:默认压缩策略,特点是通过增加读放大和写放大,最小化空间放大,适合于存储性能要求高的场景。
- Universal Compaction:常用策略,特点是通过增加读放大和空间放大,来最小化写放大,适合于写性能要求高的场景。
- FIFO Compaction:最简单的合并策略,只有一层(L0),写放大最小,适合于带时间序列的数据场景。
结合测试资源及服务配置,计算压缩级别与触发数据规模对应关系如下:
| 压缩级别 | 触发压缩条件 | 对应数据条目 |
|---|---|---|
| LEVEL 0 | 不压缩 | 小于 2314099 |
| LEVEL 1 | 256MB | 2314099 |
| LEVEL 2 | 512MB | 4628198 |
| LEVEL 3 | 1GB | 9256395 |
| LEVEL 4 | 2GB | 18512790 |
| LEVEL 5 | 4GB | 37025580 |
| LEVEL 6 | 8GB | 74051160 |
【注意】 以上压缩触发条件、数据条目均为计算参考值,实际生产使用时时会因为叠加写入场景、使用算法不同而有所差异。
数据压缩测试
数据条目:1000000(默认,属于L0范围,不会触发分层压缩),压缩率:0.5(默认)。
不开启算法压缩
顺序写场景
./db_bench --benchmarks=fillseq,stats --compression_type=none --disable_auto_compactions=true --compression_ratio=1

随机写场景
./db_bench --benchmarks=fillrandom,stats --compression_type=none --disable_auto_compactions=true --compression_ratio=1

使用 snappy 压缩
db_bench 默认压缩算法。
顺序写场景
./db_bench --benchmarks=fillseq,stats

随机写场景
./db_bench --benchmarks=fillrandom,stats

使用 zlib 压缩
顺序写场景
./db_bench --benchmarks=fillseq,stats --compression_type=zlib

随机写场景
./db_bench --benchmarks=fillrandom,stats --compression_type=zlib

使用 bzip2 压缩
顺序写场景
./db_bench --benchmarks=fillseq,stats --compression_type=bzip2

随机写场景
./db_bench --benchmarks=fillrandom,stats --compression_type=bzip2

使用 lz4 压缩
顺序写场景
./db_bench --benchmarks=fillseq,stats --compression_type=lz4

随机写场景
./db_bench --benchmarks=fillrandom,stats --compression_type=lz4

使用 lz4hc 压缩
顺序写场景
./db_bench --benchmarks=fillseq,stats --compression_type=lz4hc

随机写场景
./db_bench --benchmarks=fillrandom,stats --compression_type=lz4hc

使用 zstd 压缩
顺序写场景
./db_bench --benchmarks=fillseq,stats --compression_type=zstd

随机写场景
./db_bench --benchmarks=fillrandom,stats --compression_type=zstd

测试结果


这部分数据压缩均发生在L0层,即未触发分层测试,L0的数据也是都没有排序和去重的,从反馈数据上看,随机写场景(按随机key顺序写入数据)压缩效果表现更好,综合表现较好算法还是snappy,这也是rocksdb的默认压缩算法(由levelDB继承而来)。
分层压缩测试
LEVEL 1
顺序写场景
no compaction
./db_bench --benchmarks=fillseq,stats --num=2314099 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillseq,stats --num=2314099 --compression_type=none --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillseq,stats --num=2314099 --compression_type=none --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillseq,stats --num=2314099 --compression_type=none --compaction_style=2

随机写场景
no compaction
./db_bench --benchmarks=fillrandom,stats --num=2314099 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillrandom,stats --num=2314099 --compression_type=none --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillrandom,stats --num=2314099 --compression_type=none --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillrandom,stats --num=2314099 --compression_type=none --compaction_style=2

LEVEL 2
顺序写场景
no compaction
./db_bench --benchmarks=fillseq,stats --num=4628198 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillseq,stats --num=4628198 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillseq,stats --num=4628198 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillseq,stats --num=4628198 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

随机写场景
no compaction
./db_bench --benchmarks=fillrandom,stats --num=4628198 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillrandom,stats --num=4628198 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillrandom,stats --num=4628198 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillrandom,stats --num=4628198 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

LEVEL 3
顺序写场景
no compaction
./db_bench --benchmarks=fillseq,stats --num=9256395 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillseq,stats --num=9256395 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillseq,stats --num=9256395 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillseq,stats --num=9256395 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

随机写场景
no compaction
./db_bench --benchmarks=fillrandom,stats --num=9256395 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillrandom,stats --num=9256395 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillrandom,stats --num=9256395 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillrandom,stats --num=9256395 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

LEVEL 4
顺序写场景
no compaction
./db_bench --benchmarks=fillseq,stats --num=18512790 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillseq,stats --num=18512790 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillseq,stats --num=18512790 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillseq,stats --num=18512790 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

随机写场景
no compaction
./db_bench --benchmarks=fillrandom,stats --num=18512790 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillrandom,stats --num=18512790 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillrandom,stats --num=18512790 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillrandom,stats --num=18512790 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

LEVEL 5
顺序写场景
no compaction
./db_bench --benchmarks=fillseq,stats --num=37025580 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillseq,stats --num=37025580 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillseq,stats --num=37025580 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillseq,stats --num=37025580 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

随机写场景
no compaction
./db_bench --benchmarks=fillrandom,stats --num=37025580 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillrandom,stats --num=37025580 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillrandom,stats --num=37025580 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillrandom,stats --num=37025580 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

LEVEL 6
顺序写场景
no compaction
./db_bench --benchmarks=fillseq,stats --num=74051160 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillseq,stats --num=74051160 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillseq,stats --num=74051160 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillseq,stats --num=74051160 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

随机写场景
no compaction
./db_bench --benchmarks=fillrandom,stats --num=74051160 --compression_type=none --disable_auto_compactions=true

Leveled Compaction
./db_bench --benchmarks=fillrandom,stats --num=74051160 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=0

Universal Compaction
./db_bench --benchmarks=fillrandom,stats --num=74051160 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=1

Fifo Compaction
./db_bench --benchmarks=fillrandom,stats --num=74051160 --compression_type=none --max_bytes_for_level_multiplier=2 --compaction_style=2

测试结果
顺序写场景



从测试结果来看,分层压缩实际是已经触发了,但是顺序写场景的数据触发压缩前后几乎是相同的,甚至压缩后略有增大(即发生分层后产生的数据体积比不压缩还大,空间放大比较明显),整体未能体现分层压缩的特性,初步分析与使用的基准测试顺序写场景及构造的测试数据特性有关,即按照严格的key排序写入,不好体现对LSM tree结构去重合并的优势。
随机写场景



从测试结果来看,随机写场景更能体现对LSM tree结构去重合并的特性,但也会因为使用策略的不同而有所差异,对于 leveld compaction 来讲,各个层级的压缩效果表现的较为均衡,而对于也是常用的压缩策略 universal compaction 来讲,从L2开始,对于越老的数据压缩效果反而越好,从测试输出上也可以看出,universal策略从开始就会从最后一层再往前推,但leveld策略则是L0开始一层层往后推进这个差异。而FIFO策略则不管是哪个场景都未体现差异,可能和使用测试配置有关。
在测试验证的过程中还发现了两个比较影响观测结果的问题:放大问题和配置参数问题。
-
放大问题:由于使用了LSM tree结构数据,不可避免地出现了读放大、写放大和空间放大等因素影响了服务性能,就像微服务架构理论中的cap问题,所以rocksdb采用了level、tiere等压缩策略来优化放大问题带来的影响,但测试过程的一些反常数据,还是表明压缩效果受到了放大问题的干扰,如顺序读场景下压缩率出现负的情况(即压缩后数据文件体积反而变大)。
-
配置参数问题:这个主要因为rocksdb的配置参数使用比较复杂,某个特定服务属性的控制,可能需要多个配置参数的组合使用才能实现,但要理解并用好rocksdb几百个配置参数并不是一件容易的事情,比如universal策略下要搭配哪些参数配置更合适,FIFO策略下要搭配哪些参数配置才会生效之类问题。
【注意】 以上数据均是基于所使用的的测试资源得出,仅作参考。

浙公网安备 33010602011771号