您的位置:首页 > 数据库 > Redis

Redis详解(四)

2018-12-03 10:55 253 查看
一、Redis常见问题处理
二、Reids主从版本案例分享
三、Redis集群版参考
四、Redis性能测试
五、Redis监控指标
六、Redis产品设计理念

一、Redis常见问题处理

1.1 Redis获取所有的key和value

#!/bin/bash
#登陆方式
log="/home/admin/redis-2.8.17/src/redis-cli -a adeafgsd231234 -h 10.0.53.126  -p 6379"
#获取db_num
db_num=$($log info Keyspace |grep db |wc -l )
db_save=$($log info Keyspace |grep db   |awk -F"[b|:]" '{print $2}' )
echo "the db have at least values is $db_save ..."
echo "\n"
echo "================Begin================================="

lunxun_db() {
for i  in $(echo "keys *" | $log -n $1| awk -F"\"" '{print $1}') ;
do
value=$(echo "get $i" | $log -n $1 );
echo -e  "key:$i\t values:$value" ;
done
}

for fix in $($log info Keyspace |grep db   |awk -F"[b|:]" '{print $2}' ); do
echo -e "\n"
echo -e "the key-value of $fix is ..."
echo -e "==============================================="
lunxun_db $fix
echo
done

或者使用可视化工具:https://github.com/uglide/RedisDesktopManager

1.2 Redis访问慢

查看redis.log
查看info信息
connected_clients:162
used_memory_human:40.16G
used_memory_peak_human:40.19G
键数量:17495840 ,499key/ms
slowlog:/opt/csr/kvstore/bin/redis-cli -p port slowlog get 100
很多LREM的,需要修改业务代码 //左侧remove后面的数据需要重新排列,建议从右侧删除提高性能
默认的数据类型是list。测试发现对于数据量大的list结构在删除单个ID时性能非常差。建议将索引的数据类型强制设置为set结构。

127.0.0.1:6379> CONFIG GET slowlog-log-slower-than
127.0.0.1:6379> CONFIG SET slowlog-log-slower-than 100000
127.0.0.1:6379> CONFIG SET slowlog-max-len 1000
127.0.0.1:6379> SLOWLOG LEN //查看slowlog总条数
127.0.0.1:6379> SLOWLOG GET //查看slowlog

1.3 appendonly.aof占满磁盘空间

直接echo,对新写入的数据会重新aof,但是如果此时redis挂了就不能及时恢复了

1.4 Jedis常见问题

Jedis就是集成了redis的一些命令操作,封装了redis的java客户端。
参考: https://yq.aliyun.com/articles/236384?spm=a2c4e.11155435.0.0.2d6e75b5GSgfXK

二、Reids主从版本案例分享

需求:固定Master地址
架构模型:VIP-->负载均衡-->Primarty/[Slave1,Slave2]

2.1 Master配置

bind地址为192.168.1.16;slaveof 没有这个配置,其他和Slave1配置一样

2.2 Slave1配置

1)redis.conf

daemonize yes
pidfile "/var/run/redis.pid"
port 6379
tcp-backlog 1024
bind 192.168.1.15
timeout 0
tcp-keepalive 0
loglevel notice
logfile "redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/home/admin"
masterauth "testforauth"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "testforauth"
maxclients 30000
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
slaveof 192.168.1.11 6379

2)redis-sentinel配置

port 26379
dir "/tmp"
sentinel monitor redis_cluster 192.168.1.11 6379 2
sentinel down-after-milliseconds redis_cluster 5000
sentinel failover-timeout redis_cluster 15000
sentinel auth-pass redis_cluster adeafgsd231234
sentinel config-epoch redis_cluster 5
sentinel leader-epoch redis_cluster 5
sentinel known-slave redis_cluster 192.168.1.15 6379
sentinel known-slave redis_cluster 192.168.1.16 6379
sentinel known-sentinel redis_cluster 192.168.1.15 26379 befe42471a9a7c36d5619f244b80c0ee086a660d
sentinel known-sentinel redis_cluster 192.168.1.16 26379 4ed8c9c23ea17c386f90258873c185bfe9242153
sentinel current-epoch 5

启动server:/home/admin/redis-$VERSION/src/redis-server /home/admin/conf/redis.conf
启动sentinel:nohup /home/admin/redis-$VERSION/src/redis-sentinel /home/admin/conf/sentinel.conf > sentinel.log 2>&1 &

2.3 Slave2配置

同Slave1配置,只是bind地址变了而已
slaveof和master一样

配置文件说明:

https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf
https://raw.githubusercontent.com/antirez/redis/3.0/redis.conf
https://raw.githubusercontent.com/antirez/redis/2.8/redis.conf
https://raw.githubusercontent.com/antirez/redis/2.6/redis.conf
https://raw.githubusercontent.com/antirez/redis/2.4/redis.conf

三、Redis集群版参考

参考:
http://blog.51cto.com/hmtk520/2115789
http://www.redis.cn/topics/cluster-tutorial.html

四、Redis性能测试

#./redis-benchmark -h
Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]
-h <hostname>      Server hostname (default 127.0.0.1)
-p <port>          Server port (default 6379)
-s <socket>        Server socket (overrides host and port)
-a <password>      Password for Redis Auth  //密码
-c <clients>       Number of parallel connections (default 50)    //并行连接
-n <requests>      Total number of requests (default 100000)  //sum 请求
-d <size>          Data size of SET/GET value in bytes (default 2)  //数据大小
-dbnum <db>        SELECT the specified db number (default 0)  //指定db
-k <boolean>       1=keep alive 0=reconnect (default 1)  //是否启用保持连接
-r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD  //随机数个数
Using this option the benchmark will expand the string __rand_int__  inside an argument with a 12 digits number in the specified range
from 0 to keyspacelen-1. The substitution changes every time a command  is executed. Default tests use this to hit random keys in the specified range.
-P <numreq>        Pipeline <numreq> requests. Default 1 (no pipeline).
-e                 If server replies with errors, show them on stdout.
(no more than 1 error per second is displayed)
-q                 Quiet. Just show query/sec values
--csv              Output in CSV format
-l                 Loop. Run the tests forever
-t <tests>         Only run the comma separated list of tests. The test
names are the same as the ones produced as output.
-I                 Idle mode. Just open N idle connections and wait.

Examples:
Run the benchmark with the default configuration against 127.0.0.1:6379:
$ redis-benchmark

Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:  //并行20,共100k请求
$ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20

Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:  //
$ redis-benchmark -t set -n 1000000 -r 100000000

Benchmark 127.0.0.1:6379 for a few commands producing CSV output:
$ redis-benchmark -t ping,set,get -n 100000 --csv

Benchmark a specific command line:
$ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0

Fill a list with 10000 random elements:
$ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__

On user specified command lines __rand_int__ is replaced with a random integer
with a range of values selected by the -r option.

1.主从1G和32的,集群是16G和256G的
经测试主从1G和32的的qps为9W 集群是16G和256G的qps为11w 。
2.测试案例

./redis-benchmark -h m/s实例地址 -p 6379 -c 500 -n 1000000 -r 100000 -t set,get //测试多个k
set: 95k request per second
Get: 94k request per second  //32G m/s  (master/slave)
97K 97K //256G集群版
3.加上-a后
32G主从,正常的qps。
主从的qps正常在8~10w,但是受环境影响较大
集群8~10w*DB个数  //现场环境为256G,16c
32G主从:[set]75k  [get]90k
256G集群版:[set]92k [get]93k
4.调大-n 杜康上发现qps集中在其中6个db上
5.调整-c 为500
多开几个benchmark
开了12个benchmark值在8.8--9.5
那就是12*9.5 总数才是QPS值
6.在执行lpush和lrange命令时候,所有的操作好像都集中到了一台机器,这俩命令执行的时候性能就下来了
这个和redis-benchmark的实现有关,测试期间lpush只会操作一个key,所以只会打到一个节点上

redis-benchmark -h 192.168.1.1 -p 6379 -c 9000 -n 10000000 -r 100000 -t set,get
redis-benchmark -h 192.168.1.1 -p 6379 -c 9000 -n 10000000 -r 100000 -t set,get -a fuckBMC123 -e

五、Redis监控指标

Keys: 后端Redis所有db的key个数的总和,对于集群实例会汇聚后端所有的节点的数据
String: append,mget字符串登陆相关命令的调用次数
Hashes: 调用hget,hedel等操作hash类型的命令调用次数统计
Lists: 调用blpop,brpop等操作list数据类型的命令调用次数统计
Sets: 操作set数据类型的命令的调用次数
Zset: 操作set数据类型的命令的代用次数(有序)
HyperLog: 操作hyperlog数据类型的命令的调用次数
Pub/sub: 调用publish,subscribe等操作pub/sub功能的相关命令统计
Translation: 使用watch,multi,exec等事务相关的调用次数统计
Expires: 当前设置了过期数据的key的个数的总和
ExpiredKeys: 历史过期掉的Key的个数,这个值是历史过期掉的key的个数的总和,所以是不包含当前设置了过期key同时没有过期掉的值,同时他是一个历史累加值,也不是一个当前已经过期的key的个数,另外这个值如果做了一次主备切换会以新的主库为准
EvictedKeys: 历史淘汰掉的Key的个数,这个值是历史上因为内存满被淘汰掉了Key的个数的总和,所以他不是一个当前淘汰的每秒的key的个事,同时由于他是一个历史值,对于主备切换这个值会以新的主库为准
UsedMemory: 当前内存的使用值,这个值由于刚创建出来的时候有一定的元信息,所以对于主从实例这个值最小是30MB,另外对于集群实例这个数据的初始值会是30MB乘以节点数,目前最小会是200MB
InFlow: 后端Redis入口当前每秒的流量值,单位为KBytes/s
OutFlow: 后端Redis出口流量当前每秒的流量值,单位为KBytes/s
ConnCount: 当前Redis的客户端连接个数
FailedCount: 对于主从版本,目前这个值没有意义,因为客户端直接连接到后端DB,对于集群版本的实例该统计项标识Proxy到Redis的操作失败数目,包括超时、连接断开等异常引起的操作异常的数目,该值有部分版本的Redis为一个历史值,对于这种情况如果FaileCount没有增加则没有问题,目前新版本都为每秒的一个统计均值,后续会都升级成每秒的统计均值
TotalQps: 当前Redis的每秒操作次数
CpuUsage: 当前Redis后端的Cpu使用率
其他监控项:用户可以点击其他监控项查看到可以添加对不同操作命令的一个访问次数的监控,比如可以查看set每秒的次数,可以选择String监控项就可以看到set的每秒的监控项

六、Redis产品设计理念

6.1 涉及一个Redis产品需要涉及的问题
1) HA高可用组件:对Redis实例运行状态监测,如有异常主动进行主备切换,保证高可用
2) 备份系统:对落盘文件进行备份,建议使用三备份文件系统存储
3) 任务流控制系统:对Redis整个生命周期管理,包括创建,释放,变更配置,主备切换,健康检查等。
4) 监控系统:有一定的日志收集和处理agent,对实例性能信息收集,包括各种状态数据
5) 集群功能实现:使用开源或者自研组件对redis 分片集群机制支持。并在dns-->[db1,db2,db3,db4....],每一个dbX都是主备机制,有高可用机制

推荐站点
http://www.redis.cn/
http://www.redis.io/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis jedis