最近项目中接连遇到redis出现瓶颈的问题,现在把排查的一些经验记录下来备查,本篇只是思路的整理,不涉及具体的使用。

大体的思路如下:

1.通过slow log查看

参考 http://www.cnblogs.com/onmyway20xx/p/5486604.html

  查看下是否有较为明显的慢查询?一般认为出现慢查询的话,redis性能瓶颈已经比较明显了

2. 通过info 查看;

  info里面的信息比较多,通常关注以下几块

   # Memory    
    used_memory_human:795.13K  #redis现在占用的内存,有可能包括SWAP虚拟内存。
    used_memory_rss:18259968  #系统给redis分配的内存  
    used_memory_peak_human:9.51M  #Redis所用内存的峰值 
    mem_fragmentation_ratio:22.43 #used_memory_rss/used_memory ,当mem_fragmentation_ratio <1 时,说明used_memory > used_memory_rss,

    这时Redis已经在使用SWAP,运行性能会受很大影响。

3. 通过benchmark测试下当前服务器的性能;

4. 通过MONITOR测算一次请求对redis操作的次数;

1. INFO

info指令返回服务器相关信息,包括:

  • server: General information about the Redis server

  • clients: Client connections section

  • memory: Memory consumption related information

  • persistence: RDB and AOF related information

  • stats: General statistics

  • replication: Master/slave replication information

  • cpu: CPU consumption statistics

  • commandstats: Redis command statistics

  • cluster: Redis Cluster section

  • keyspace: Database related statistics

其本身支持定制返回列表:

[root@~]# redis-cli info
[root@~]# redis-cli info default
[root@~]# redis-cli info all

详情请见: http://www.redis.cn/commands/info.html

2. MONITOR

MONITOR是一个调试命令,返回服务器处理的每一个命令,它能帮助我们了解在数据库上发生了什么操作。共有3种操作方法:

[root@~]# redis-cli monitor
OK
1417532512.619715 [0 127.0.0.1:55043] "REPLCONF" "ACK" "6623624"
[root@~]# telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
monitor
+OK
+1417532567.733458 [0 127.0.0.1:55043] "REPLCONF" "ACK" "6623708"
+1417532568.735936 [0 127.0.0.1:55043] "REPLCONF" "ACK" "6623708"
quit
+OK
Connection closed by foreign host.
[root@~]# redis-cli
127.0.0.1:6379> monitor
OK
1417532590.785487 [0 127.0.0.1:55043] "REPLCONF" "ACK" "6623736"

由于MONITOR命令返回服务器处理的所有的命令, 所以在性能上会有一些消耗。使用官方的压测工具测试结果如下

在不运行MONITOR命令的情况下,benchmark的测试结果:

[root@~/software/redis-2.8.17]# src/redis-benchmark -c 10 -n 100000 -q
PING_INLINE: 51020.41 requests per second
PING_BULK: 50607.29 requests per second
SET: 37257.82 requests per second
GET: 49800.80 requests per second
INCR: 38699.69 requests per second
LPUSH: 38910.51 requests per second
LPOP: 39277.30 requests per second
SADD: 54614.96 requests per second
SPOP: 51948.05 requests per second
LPUSH (needed to benchmark LRANGE): 38819.88 requests per second
LRANGE_100 (first 100 elements): 20112.63 requests per second
LRANGE_300 (first 300 elements): 9025.27 requests per second
LRANGE_500 (first 450 elements): 6836.67 requests per second
LRANGE_600 (first 600 elements): 5406.28 requests per second
MSET (10 keys): 19394.88 requests per second

在运行MONITOR命令的情况下,benchmark的测试结果: (redis-cli monitor > /dev/null):

[root@~/software/redis-2.8.17]# src/redis-benchmark -c 10 -n 100000 -q
PING_INLINE: 42211.91 requests per second
PING_BULK: 42936.88 requests per second
SET: 26143.79 requests per second
GET: 33990.48 requests per second
INCR: 26553.37 requests per second
LPUSH: 27337.34 requests per second
LPOP: 27225.70 requests per second
SADD: 30459.95 requests per second
SPOP: 39494.47 requests per second
LPUSH (needed to benchmark LRANGE): 26315.79 requests per second
LRANGE_100 (first 100 elements): 22055.58 requests per second
LRANGE_300 (first 300 elements): 8104.38 requests per second
LRANGE_500 (first 450 elements): 6371.05 requests per second
LRANGE_600 (first 600 elements): 5031.95 requests per second
MSET (10 keys): 14861.05 requests per second

可以看到各项指标基本都有所下降。

详情请见: http://www.redis.cn/commands/monitor.html

3. SLOWLOG

通过SLOWLOG可以读取慢查询日志。

使用SLOWLOG LEN就可以获取当前慢日志长度。

[root@~/software/redis-2.8.17]# redis-cli
127.0.0.1:6379> slowlog len
(integer) 28

使用SLOWLOG GET就可以获取所有慢日志。

127.0.0.1:6379> slowlog get
1) 1) (integer) 27
2) (integer) 1417531320
3) (integer) 24623
4) 1) "info"

其中,各项指标表示:

  • A unique progressive identifier for every slow log entry.

  • The unix timestamp at which the logged command was processed.

  • The amount of time needed for its execution, in microseconds(注意,microseconds翻译成微秒,而不是毫秒).

  • The array composing the arguments of the command.

使用SLOWLOG GET N就可以获取最近N条慢日志。

127.0.0.1:6379> slowlog get 2
1) 1) (integer) 27
2) (integer) 1417531320
3) (integer) 24623
4) 1) "info"
2) 1) (integer) 26
2) (integer) 1417528379
3) (integer) 21363
4) 1) "get"
2) "user:score"

使用SLOWLOG RESET命令重置慢日志。一旦执行,将丢失以前的所有慢日志。

127.0.0.1:6379> slowlog reset
3. redis延迟时间排查

最近数据量越来越多,并发写操作很多的情况下,Redis出现响应慢的情况;


可以使用 Redis命令来测试一下redis的响应速度:

redis-cli --latency -h xxx -p xxxx

这条命令会向Redis插入示例数据来检查平均延时。 Ctrl+C可以随时结束测试;


下面我们列一下会出现延时的可能:


  • 硬件,系统:硬件问题是所有问题最底层的问题了,如果硬件慢,例如CPU主频低,内存小,磁盘IO慢,这些会让所有运行在上面的系统响应慢;另外,使用虚拟机会让系统运行的性能太为下降;当然,有钱的话,这问题很容易解决;系统方面,Linux本身的系统资源调度也会产生一定的延时。这些一般不会很大,可以忽略不计;

  • 网络:如果客户端和redis在同一台服务器上,使用socket建立连接会比监听 TCP/IP 端口快很多;

  • Redis命令:一些时间复杂度比较高的命令,如 lrem,sort,sunion等命令会花比较长时间;另外,大量的重复连接也会造成延时,重用连接是一种很好的品质;如果有大量写操作,可以使用 pipeline 管道的方式(类似mysql事务),一次性提交,这样数据量也少了,连接次数也少了,不用每次都返回数据,速度自然会快很多;

  • 持久化:Redis持久化需要fork出一个进程来进行持久化操作,这本身就会引发延时,如果数据变化大,RDB配置时间短,那这个代价还是挺大的;再加上,硬盘这东西真有点不靠谱,如果还是虚拟机上的虚拟硬盘,如果还是NFS共享目录,那这延时会让你崩溃。所以,如果系统不需要持久化,关了吧。

												

redis 性能监控和排查的更多相关文章

  1. Redis性能监控

    参考地址: redis教程:http://www.runoob.com/redis/redis-tutorial.html redis百度百科:https://baike.baidu.com/item ...

  2. redis性能监控(一): Redis Info 命令 - 获取 Redis 服务器的各种信息和统计数值

    https://blog.csdn.net/wmx3ng/article/details/82772891 https://blog.csdn.net/lxpbs8851/article/detail ...

  3. Redis性能问题排查解决手册(七)

     阅读目录: 性能相关的数据指标 内存使用率used_memory 命令处理总数total_commands_processed 延迟时间 内存碎片率 回收key 总结 性能相关的数据指标 通过Red ...

  4. Redis性能问题排查解决手册

    转自:http://www.cnblogs.com/mushroom/p/4738170.html 阅读目录: 性能相关的数据指标 内存使用率used_memory 命令处理总数total_comma ...

  5. Redis(二十一):Redis性能问题排查解决手册(转)

    性能相关的数据指标 通过Redis-cli命令行界面访问到Redis服务器,然后使用info命令获取所有与Redis服务相关的信息.通过这些信息来分析文章后面提到的一些性能指标. info命令输出的数 ...

  6. Redis为什么变慢了?透彻解读如何排查Redis性能问题

    Redis 作为优秀的内存数据库,其拥有非常高的性能,单个实例的 OPS 能够达到 10W 左右.但也正因此如此,当我们在使用 Redis 时,如果发现操作延迟变大的情况,就会与我们的预期不符. 你也 ...

  7. 关于redis性能问题分析和优化

    一.如何查看Redis性能 info命令输出的数据可以分为10个分类,分别是: server,clients,memory,persistence,stats,replication,cpu,comm ...

  8. Redis变慢?深入浅出Redis性能诊断系列文章(一)

    (本文首发于"数据库架构师"公号,订阅"数据库架构师"公号,一起学习数据库技术)   Redis 作为一款业内使用率最高的内存数据库,其拥有非常高的性能,单节点 ...

  9. Redis变慢?深入浅出Redis性能诊断系列文章(二)

    (本文首发于"数据库架构师"公号,订阅"数据库架构师"公号,一起学习数据库技术) 本篇为Redis性能问题诊断系列的第二篇,本文主要从应用发起的典型命令使用上进 ...

随机推荐

  1. JavaScript学习笔记(13)——BOM

    1.window 所有浏览器都支持window对象,它表示浏览器窗口本身. 所有 JavaScript 全局对象.函数以及变量均自动成为 window 对象的成员. 全局变量是 window 对象的属 ...

  2. 验证hashmap非线程安全

    http://www.blogjava.net/lukangping/articles/331089.html final HashMap<String, String> firstHas ...

  3. JavaScript 函数的执行过程

    每一个JavaScript函数都是Function对象的一个实例, 它有一个仅供JavaScript引擎存取的内部属性[[Scope]]. 这个[[Scope]]存储着一个作用域的集合, 这个集合就叫 ...

  4. IDEA 2016.2.2激活地址(2016-08-22)

    http://idea.imsxm.com/ 2016/10/13  http://idea.iteblog.com/key.php

  5. String.Format数字格式化输出 {0:N2} {0:D2} {0:C2

    //格式为sring输出 //   Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); //   Label2.Text = ...

  6. AJAX异步请求原理和过程

    AJAX 指异步 JavaScript 及 XML(Asynchronous JavaScript And XML),它不是一种新的编程语言,而是一种使用现有标准的新方法. AJAX 基于 JavaS ...

  7. thymeleaf 模板引擎

    1.创建模板解析器 Create Template Resolver  用来加载模板 // create template resolver //创建模板解析器可以用Servlet上下文模板解析器Se ...

  8. emoji处理方法汇总

    emoji资料 今天研究了emoji,挺有意思,资料挺多,摘要一些信息给大家分享,也算是自己记录学习. emoji介绍 Emoji (絵文字,词义来自日语えもじ,e-moji,moji在日语中的含义是 ...

  9. Huawei HG556a A版 刷 openwrt

    一直想玩玩openwrt,调研了一下 HG556a尽管散热很烂,但性价比超高,于是淘宝入手一台A版,A版和C版区别为wifi芯片: 到货后在网上找了几个教程便开始动手刷openwrt,但刷机的过程中还 ...

  10. js 使用技巧 - [近几年工作中的经验总结的技巧]

    1.如果 ajax 返回单一的 json 格式,接收方需要这样再格式化一下赋值: var str = eval("(" + msg + ")"); 开发引用: ...