本文内容为 http://www.redis.io/commands/client-kill 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
CLIENT KILL [ip:port] [ID client-id] [TYPE normal|slave|pubsub] [ADDR ip:port] [SKIPME yes/no]

命令 CLIENT KILL 用于关闭指定客户端(Client)连接(Connection)

The CLIENT KILL command closes a given client connection.

Redis 2.8.11 版本之前,只能通过客户端地址来关闭一个连接,按如下格式:

CLIENT KILL addr:port

ip:port 必须匹配由命令 CLIENT LIST 返回的结果中的某一行(地址 域)。

Up to Redis 2.8.11 it was possible to close a connection only by client address, using the following form:

CLIENT KILL addr:port

The ip:port should match a line returned by the CLIENT LIST command (addr field).

然而,从 Redis 2.8.12 或更高版本开始,当前命令支持以下格式:

CLIENT KILL <filter> <value> ... ... <filter> <value>

使用新的格式时,可以通过不同属性来结束(Kill)客户端,而不仅仅只能通过地址(Address),可用的过滤器有:

  • CLIENT KILL ADDR ip:port 这个其实就是原来的老方法。
  • CLIENT KILL ID client-id 允许通过唯一 ID 字段来结束客户端,该字段由 Redis 2.8.12 版本中的命令 CLIENT LIST 引入。
  • CLIENT KILL TYPE type 其中参数 type 可以为 normalslavepubsub 中的某个,该指令将会关闭所有指定类型的连接。注意 当客户端被阻塞(Blocked)于命令 MONITOR 中时,其被认为属于 normal 类型。
  • CLIENT KILL SKIPME yes/no 默认地,该选项被设置为 yes,即,调用该指令的当前客户端不会被结束。如果设置为 no,调用当前命令时,可能会结束当前客户端。
  • CLIENT KILL ADDR ip:port. This is exactly the same as the old three-arguments behavior.
  • CLIENT KILL ID client-id. Allows to kill a client by its unique ID field, which was introduced in the CLIENT LIST command starting from Redis 2.8.12.
  • CLIENT KILL TYPE type, where type is one of normal, slave, pubsub. This closes the connections of all the clients in the specified class. Note that clients blocked into the MONITOR command are considered to belong to the normal class.
  • CLIENT KILL SKIPME yes/no. By default this option is set to yes, that is, the client calling the command will not get killed, however setting this option to no will have the effect of also killing the client calling the command.

可以在同一时刻提供多个过滤条件。所有过滤条件按逻辑与(Logical AND)组合在一起。

The command will ahdnle multiple filters via logical AND. For example:
CLIENT KILL addr 127.0.0.1:6379 type slave
is valid and will kill only a slaves with the specified address. This format containing multiple filters is rarely useful currently.

当使用新版本格式时,当前命令不再返回 OK 或一个错误,而是返回所结束的进程数(可能为 0)。

When the new form is used the command no longer returns OK or an error, but instead the number of killed clients, that may be zero.

  • 当以 3 个参数的格式被调用时,如果连接存在,且成功关闭,则返回 OK

    当以过滤器/值(Filter/Value)的格式被调用时,将返回被结束的客户端数。

    When called with the three arguments format:

    Simple string reply: OK if the connection exists and has been closed

    When called with the filter / value format:

    Integer reply: the number of clients killed.

  • 最新版本(2.8.12 及更高)的 Redis 哨兵功能中,使用命令 CLIENT KILL 来结束一个被重配置(Reconfigured)的实例(Instance),以强制客户端与一个 Sentinel 进行再次握手(Handshake),且同时更新其配置。

    CLIENT KILL and Redis Sentinel

    Recent versions of Redis Sentinel (Redis 2.8.12 or greater) use CLIENT KILL in order to kill clients when an instance is reconfigured, in order to force clients to perform the handshake with one Sentinel again and update its configuration.

  • 版本支持

    2.4.0+

    时间复杂度(Time complexity)

    O(N)N 为客户端连接数。

    其它

    由于 Redis 的单线程特性,当客户端连接正处于命令执行过程中时,它是不能够被结束掉的。而从客户端角度来看,连接本来就不能够在命令执行过程中被结束掉。

    然而,其所带来的问题就是,客户端只有在下一条命令被发送时才会意识到连接已被关闭(这个时候会返回一个网络错误)。

    Due to the single-treaded nature of Redis, it is not possible to kill a client connection while it is executing a command. From the client point of view, the connection can never be closed in the middle of the execution of a command. However, the client will notice the connection has been closed only when the next command is sent (and results in network error).