MONITOR

实时监听服务器收到的所有请求。

语法

MONITOR

引入版本:1.0.0

复杂度摘要:—

MONITOR 是一个调试命令,它会流回 Redis 服务器处理的每个命令。它可以帮助了解数据库发生的情况。该命令既可以通过 redis-cli 也可以通过 telnet 使用。

当使用 Redis 作为数据库和分布式缓存系统时,查看服务器处理的所有请求的能力对于发现应用程序中的错误非常有用。

$ 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] "eval" "return redis.call('set','x','7')" "0"
1339518100.363799 [0 lua] "set" "x" "7"
1339518100.544926 [0 127.0.0.1:60866] "del" "x"

使用 SIGINT (Ctrl-C) 停止通过 redis-cli 运行的 MONITOR 流。

$ 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 或 RESET 命令以停止通过 telnet 运行的 MONITOR 流。

MONITOR 未记录的命令

出于安全考虑,MONITOR 的输出不会记录任何管理命令,并且敏感数据会在命令 AUTH 中进行编辑。

此外,命令 QUIT 也不会被记录。

运行 MONITOR 的成本

由于 MONITOR 流回 全部 命令,因此使用它是有代价的。以下(完全不科学)基准数据说明了运行 MONITOR 的成本可能是多少。

没有 MONITOR 运行基准结果:

$ 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

与 MONITOR 运行基准结果(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

在这种特殊情况下,运行单个 MONITOR 客户端可能会导致吞吐量降低 50% 以上。运行更多 MONITOR 客户端将进一步降低吞吐量。

行为改变史

Redis Software 与 Redis Cloud 兼容性

Redis
软件
Redis
Cloud
说明
✅ 标准
✅ 主动-主动
✅ 标准
✅ 主动-主动

返回值

RESP2

非标准返回值。以无限流转储接收到的命令。

RESP3

非标准返回值。以无限流转储接收到的命令。

资料来源:Redis 官方 MONITOR 文档;中文内容参考 Redis 文档许可。