Question
我们知道,在 Redis 中,get 和 set 操作的 time complexity 均为 O (1)。然而,当我们提供一个超长的 key 给 Redis 时,理论上,Redis 需要花费更多内存来存储这个 key。
因此,key 的长度有没有可能影响 Redis 的性能呢?
Benchmark Script
在 key 为不同长度(包含的字符长度)时,Redis 的 get 和 set 操作的 Performance(QPS)测试脚本
#!/bin/bash
echo "Test the length of key in Redis will influence performance..."
for (( c=1; c<=100000000000000; c=c*10 ))
do
echo "Current length: ${c}"
redis-cli flushall
redis-benchmark -n 1000000 -t set,get -r ${c} -q
done
Get 操作
Set 操作
可以发现,Redis 对 Key 的长度并不敏感,或者说,当 key 的长度小于 100 个字符时(这是几乎 cover 我们正常使用的所有情况),key 的长度对 Redis 的性能几乎几乎没有任何影响。
但是,值得一提的是,由于总内存空间是一定的,因此,每个 key 的平均长度越长,可以给 value 使用的内存空间就相对更少了。
Reference
- https://redis.io/topics/benchmarks
- https://stackoverflow.com/questions/6320739/does-name-length-impact-performance-in-redis
- https://adamnengland.wordpress.com/2012/11/15/redis-performance-does-key-length-matter/
FEATURED TAGS
algorithm
algorithmproblem
architecturalpattern
architecture
aws
c#
cachesystem
codis
compile
concurrentcontrol
database
dataformat
datastructure
debug
design
designpattern
distributedsystem
django
docker
domain
engineering
freebsd
git
golang
grafana
hackintosh
hadoop
hardware
hexo
http
hugo
ios
iot
java
javaee
javascript
kafka
kubernetes
linux
linuxcommand
linuxio
lock
macos
markdown
microservices
mysql
nas
network
networkprogramming
nginx
node.js
npm
oop
openwrt
operatingsystem
padavan
performance
programming
prometheus
protobuf
python
redis
router
security
shell
software testing
spring
sql
systemdesign
truenas
ubuntu
vmware
vpn
windows
wmware
wordpress
xml
zookeeper