Redis中的重命名是否始终可以使用数据?

时间:2017-10-11 23:51:21

标签: redis

当我运行重命名命令时,我认为它确实是这样的,

  1. 为新数据使用新名称
  2. 删除旧名称的参考
  3. 删除旧数据(如果它很大,可能需要一些时间)
  4. 对于访问此数据的客户,是否有时间会发生这些数据?

    1. 密钥不存在
    2. 数据状况不佳
    3. Redis在访问期间挂起
    4. Redis重命名命令期间执行了哪些步骤?

1 个答案:

答案 0 :(得分:3)

Since Redis has single threaded execution of commands, the rename will be atomic, so the answer to 1 and 2 are no. The thing about it "removing old data" is only if the destination key already points to a large structure that it needs to delete (Redis will clobber it.) The original data object will not be copied. Only hash table entries pointing to it might be moved around. Since rehashing in Redis is incremental, this should essentially be constant time.

Redis will always "hang" on slow commands due to the single threaded command execution. So for 3, it can always be yes depending on what you're doing, but in this case, only if you're doing significantly large implicit delete.

Edit: as of Redis 4.0 you can actually specify the config option lazyfree-lazy-server-del yes (default is no) and the server will actually delete asynchronously for side-effect deletes such as this. In other words, instead of delete blocking, the object will be queued for background deletion. This would effectively make RENAME constant time. See sample cfg: https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf