杨梅冲
每天在想什么呢?

github:https://github.com/redis/redis

1.redis 安装

wget https://download.redis.io/releases/redis-7.0.15.tar.gz

yum install systemd-devel
tar -xzvf redis-7.0.15.tar.gz
cd redis-7.0.15
make BUILD_WITH_SYSTEMD=yes MALLOC=jemalloc
make install

开机启动

[root@linux-101 redis]# cat /etc/systemd/system/redis.service
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
Wants=network-online.target
After=network-online.target

[Service]
User=root
Group=root
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli shutdown
PIDFile=/var/run/redis_6379.pid
LimitNOFILE=65536
Type=notify
TimeoutStartSec=300
TimeoutStopSec=300
Restart=always
RestartSec=5s


[Install]
WantedBy=multi-user.target

redis.conf

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/usr/local/redis/logs/redis.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /usr/local/redis/data
maxmemory 2gb
maxmemory-policy allkeys-lru
io-threads 2
io-threads-do-reads yes
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-diskless-sync-max-replicas 0
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
aof-timestamp-enabled no
 
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
redis.conf

一般不需要优化,如果需要优化

1.内存上限设置限制:
maxmemory 2gb
maxmemory-policy allkeys-lru. # 和研发商量

‌系统内存大小‌:
Redis是内存数据库,可用内存直接影响能存储的数据大小
大value会占用连续内存块,可能导致内存碎片问题

‌操作系统配置‌:
操作系统的进程内存限制(ulimit)
内核参数配置(如overcommit_memory)

‌Redis实例配置‌:
maxmemory参数设置的内存上限
客户端缓冲区限制(client-output-buffer-limit)

‌maxmemory-policy常见淘汰策略‌:
volatile-lru:从已设置过期时间的键中使用LRU算法淘汰
allkeys-lru:从所有键中使用LRU算法淘汰
volatile-random:从已设置过期时间的键中随机淘汰
allkeys-random:从所有键中随机淘汰
volatile-ttl:优先淘汰剩余时间短的键
noeviction:不淘汰,返回错误(默认策略)
2.多线程 IO 配置
# 启用2个I/O线程
io-threads 2
# 允许I/O线程处理读操作
io-threads-do-reads yes

io-threads参数
‌作用‌:设置Redis用于处理网络I/O的线程数量,默认值为1(单线程模式)
‌取值范围‌:1到128之间的整数
‌推荐设置‌:通常设置为CPU核心数的1/2到3/4之间
‌生效条件‌:需要与io-threads-do-reads参数配合使用才能完全发挥多线程优势

io-threads-do-reads参数
‌作用‌:控制是否启用I/O线程处理读操作,默认值为no(仅写操作使用多线程)
‌启用效果‌:当设置为yes时,读操作也会由I/O线程处理,进一步提升网络吞吐量
‌注意事项‌:命令执行仍由主线程单线程处理以保证原子性

‌触发条件‌:
多线程模式根据积压的客户端请求量动态启用
当积压量≥io-threads*2时启用多线程处理

‌性能影响‌:
可显著提升网络吞吐量(特别是大value场景)
可能增加少量CPU开销(线程调度)
对延迟敏感型应用建议测试后再启用

性能提升数据
根据官方文档和权威测试报告,Redis启用多线程后的性能提升主要体现在以下几个方面:

‌网络吞吐量提升‌:

在4核CPU环境下,启用4个I/O线程可使QPS提升约200%
单线程Redis QPS约10万,多线程可达到20-30万
网络密集型场景(如大value传输)性能提升最显著
‌不同线程数下的性能对比‌:

2线程:提升约80-120%
4线程:提升约180-220%
8线程:提升约250-300%(需更高核数CPU支持)
‌特殊场景优化‌:

TLS加密场景性能退化可从70%降低到20%
大规格服务器(高CPU核心数)资源利用率可提升50%以上
实际应用案例
在生产环境中的性能提升案例表明:

‌热点数据场景‌:

多线程可有效应对突发高并发
在电商秒杀等场景下,QPS提升可达2-3倍
‌资源利用率优化‌:

在16核服务器上,启用8线程可将CPU利用率从15%提升至60%
避免了单线程模型下CPU资源的浪费
‌大value传输场景‌:

传输1MB大小的value时,吞吐量提升可达300%
网络延迟降低约40%

 

posted on 2025-08-13 12:07  杨梅冲  阅读(55)  评论(0)    收藏  举报