本文内容为 http://redis.io/commands/monitor 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
MONITOR

MONITOR 是一条调试(Debugging)命令,可以逆流输出被 Redis 服务器处理的每条命令。

MONITOR is a debugging command that streams back every command processed by the Redis server. 

它可以用于帮助弄明白数据库中到底发生了什么。当前命令既可以通过 redis-cli 方式使用,也可以通过 telnet 方式使用。

This command can both be used via redis-cli and via telnet.

你可以通过查看所有被服务端所执行的请求来快速定位 Bug,在将 Redis 作为数据库使用,或作为分布式缓存系统(Distributed caching system)使用时,这一点变得非常有用。

$ redis-cli monitor
1339518083.107412 [0 127.0.0.1:60866] "keys" "*"
1339518087.877697 [0 127.0.0.1:60866] "dbsize"
1339518090.420270 [0 127.0.0.1:60866] "set" "x" "6"
1339518096.506257 [0 127.0.0.1:60866] "get" "x"
1339518099.363765 [0 127.0.0.1:60866] "del" "x"
1339518100.544926 [0 127.0.0.1:60866] "get" "x"

可以使用信号 SIGINT (Ctrl-C)来终止以 redis-cli 方式运行的 MONITOR 流。

Use SIGINT (Ctrl-C) to stop a MONITOR stream running via redis-cli.
$ telnet localhost 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
MONITOR
+OK
+1339518083.107412 [0 127.0.0.1:60866] "keys" "*"
+1339518087.877697 [0 127.0.0.1:60866] "dbsize"
+1339518090.420270 [0 127.0.0.1:60866] "set" "x" "6"
+1339518096.506257 [0 127.0.0.1:60866] "get" "x"
+1339518099.363765 [0 127.0.0.1:60866] "del" "x"
+1339518100.544926 [0 127.0.0.1:60866] "get" "x"
QUIT
+OK
Connection closed by foreign host.

可以手动地运行 QUIT 命令来终止以 telnet 方式运行的 MONITOR 流。

  • 非标准返回值,以无限滚动的方式不断列出所有收到的命令。

    Non standard return value, just dumps the received commands in an infinite flow.
  • Cost of running MONITOR

    Because MONITOR streams back all commands, its use comes at a cost. The following (totally unscientific) benchmark numbers illustrate what the cost of running MONITOR can be.
    Benchmark result withoutMONITOR running:
    $ src/redis-benchmark -c 10 -n 100000 -q
    PING_INLINE: 101936.80 requests per second
    PING_BULK: 102880.66 requests per second
    SET: 95419.85 requests per second
    GET: 104275.29 requests per second
    INCR: 93283.58 requests per second
    Benchmark result withMONITOR running (redis-cli monitor > /dev/null):
    $ src/redis-benchmark -c 10 -n 100000 -q
    PING_INLINE: 58479.53 requests per second
    PING_BULK: 59136.61 requests per second
    SET: 41823.50 requests per second
    GET: 45330.91 requests per second
    INCR: 41771.09 requests per second
    In this particular case, running a single MONITOR client can reduce the throughput by more than 50%. Running more MONITOR clients will reduce throughput even more.

  • 版本支持

    1.0.0+