Redis的SET命令不能失败,但是可能会失败吗?

6
我正在尝试调试一些我所遇到的Redis问题,发现了一些关于SET命令的不确定文档。 在我的Redis配置中,我有以下行(片段):
# Note: with all the kind of policies, Redis will return an error on write
#       operations, when there are not suitable keys for eviction.
#
#       At the date of writing this commands are: set setnx setex append

SET 命令的文档页面上,我找到了以下内容:

Status code reply: always OK since SET can't fail.

有关于明确行为的任何见解吗?
1个回答

13

简而言之:如果redis实例内存不足,SET将返回错误响应。

redis.c的源代码可以看出,当处理命令时,大致流程如下(伪代码):

IF memory is needed
    IF we can free keys
        Free keys
        Process the command
            SET -> process and return OK response
    ELSE return error response
ELSE
    Process command
        SET -> process and return OK response

虽然没有直接这么写,但基本思想是:在处理命令之前会检查内存,因此即使命令不会失败,如果没有内存,将返回错误响应,无论实际响应如何。


6
+1,会出现错误的不是 SET 命令本身,而是如果你已经配置了“当达到内存限制时,请返回错误”的选项,Redis 在处理命令之前就可能返回错误。 - antirez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接