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

原子地(Atomically)将一个键从源 Redis 实例转移到目标 Redis 实例。

Atomically transfer a key from a source Redis instance to a destination Redis instance.

一旦成功,该键将从原始实例中删除,且保证存在于目标实例。

On success the key is deleted from the original instance and is guaranteed to exist in the target instance.

当前命令是原子的,在转移键所需时间内会同时阻塞(Block)两个实例。在任意给定时间内该键要么存在于给定实例,要么存在于另一个实例中,除非出现一个超时错误。

The command is atomic and blocks the two instances for the time required to transfer the key, at any given time the key will appear to exist in a given instance or in the other instance, unless a timeout error occurs.

该命令内部使用命令 DUMP 来产生指定键的序列化版本,然后使用命令 RESTORE 在目标实例中重新合成该键。对目标实例来说,源实例就好像一个客户端程序(Client)。如果目标实例在执行命令 RESTORE 时返回 OK,源例实将会调用命令 DEL 删除该键。

The command internally uses DUMP to generate the serialized version of the key value, and RESTORE in order to synthesize the key in the target instance. The source instance acts as a client for the target instance. If the target instance returns OK to the RESTORE command, the source instance deletes the key using DEL.

选项 timeout 以毫秒为单位,其指定了与目标实例进行通信过程中,任意时刻的最大空闲时间(Maximum idle time,即等待响应时间)。意思是,当前操作不须要在某个指定毫秒数内完成,但整个传输过程必须一直进行,即使有阻塞也不能超过 timeout 毫秒数。

The timeout specifies the maximum idle time in any moment of the communication with the destination instance in milliseconds. This means that the operation does not need to be completed within the specified amount of milliseconds, but that the transfer should make progresses without blocking for more than the specified amount of milliseconds.

命令 MIGRATE 需要进行 I/O 操作,且需要遵循超时设定。当在传输过程中出现 I/O 错误,或达到超时上限,当前操作将会被迫中止,且返回错误 - IOERR。当该错误发生时,可能会出现下述两种情况:

  • key 可能同时存在于两个实例上。
  • key 只存在于源实例上。

MIGRATE needs to perform I/O operations and to honor the specified timeout. When there is an I/O error during the transfer or if the timeout is reached the operation is aborted and the special error - IOERR returned. When this happens the following two cases are possible:

The key may be on both the instances.

The key may be only in the source instance.

当超时发生时,当前键不可能被丢失,但调用 MIGRATE 的客户端程序必须在这个时候检测当前键是否同时存在于目标实例,并采取相应措施。

It is not possible for the key to get lost in the event of a timeout, but the client calling MIGRATE, in the event of a timeout error, should check if the key is also present in the target instance and act accordingly.

当任意其它错误(以 ERR 开头)被返回时,命令 MIGRATE 保证当前键只存在于源始实例中(除非目标实例已经存在相同名字的键)。

When any other error is returned (starting with ERR) MIGRATE guarantees that the key is still only present in the originating instance (unless a key with the same name was also already present on the target instance).

一旦成功将返回 OK

On success OK is returned.
  • COPY -- 不要将当前键从本地实例中删除。

    REPLACE -- 替换已经存在于远程实例中的键。

  • 返回响应字符串,如果成功则返回 OK

    The command returns OK on success.
  • 版本支持

    2.6.0+

    COPYREPLACE 选项从 3.0 版开始提供支持,2.62.8 版本中不可用。

    时间复杂度(Time complexity)

    This command actually executes a DUMP+DEL in the source instance, and a RESTORE in the target instance. See the pages of these commands for time complexity. Also an O(N) data transfer between the two instances is performed.