Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis SET command can't fail, but can?

Tags:

redis

I'm trying to debug some Redis issues I am experiencing and came by some inconclusive documentation about the SET command.

In my Redis config; I have the following lines (snippet):

# 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

On the documentation page for the SET command I found:

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

Any insights on the definitive behaviour?

like image 880
Sjaak Trekhaak Avatar asked Oct 16 '12 09:10

Sjaak Trekhaak


People also ask

Does Redis set overwrite?

Description. Redis SET command is used to set some string value in redis key. If the key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on a successful SET operation.

Are Redis commands blocking?

Redis has a few blocking commands among the built-in set of commands. One of the most used is BLPOP (or the symmetric BRPOP ) which blocks waiting for elements arriving in a list. The interesting fact about blocking commands is that they do not block the whole server, but just the client calling them.

What is NX in Redis?

NX -- Only set the key if it does not already exist. XX -- Only set the key if it already exist.

What is set in Redis?

Redis sets are unordered collections of unique strings that act like the sets from your favorite programming language (for example, Java HashSets, Python sets, and so on). With a Redis set, you can add, remove, and test for existence O(1) time (in other words, regardless of the number of set elements).


1 Answers

tl;dr: SET will return an error response if the redis instance runs out of memory.

As far as I can tell from the source code in redis.c, esentially when a command is to be processed the flow goes like this (pseudo code):

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

It's not exactly written this way, but the basic idea comes down to that: memory is being checked before the command is processed, so even if the command cannot fail, an error response will be returned if there's no memory regardless the actual response of the command.

like image 101
Mahn Avatar answered Jan 03 '23 10:01

Mahn