Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep empty data-keys in Redis

Redis can be used either directly as a key-value store, where the value is string. Or, in more sophisticated way, the value can be a data structure, for example hash or list. Assuming we have the second case and under the key "H", there is a hash. Items can be added to the hash and removed. Eventually, the hash can be empty, and it can be re-populated again.

I have found out that if we remove the last item from the data structure, our hash "H", Redis removes it from current keys, for some reason.

Example:

HSET "H" "key1" "value1"
HSET "H" "key2" "value2"
HDEL "H" "key1"
 <-- Here, "H" is in the list of current keys, whereby HLEN returns 1
HDEL "H" "key2"
 <-- Here, for some reason, "H" is not shown among existing keys, 
     not even as an empty hash (HLEN 0)
HSET "H" "key3" "value3"
 <-- Hash is back in the list of keys

My question is: Is it possible to configure Redis so it does still keep showing the value (empty hash, in our example) of the given key ("H", in our example) as an empty non-trivial data structure?

like image 910
Pavel S. Avatar asked Jan 22 '14 12:01

Pavel S.


People also ask

Can a Redis key be empty?

Keys. Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like "foo" to the content of a JPEG file. The empty string is also a valid key.

Does Redis delete empty sets?

Redis Doesn't Store Empty Sets Or Hashes (And Will Delete Empty Sets And Hashes)

Does Redis overwrite keys?

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.

What is nil Redis?

In applications that utilize redis a very common task that needs to be performed is checking to see if a key exists or not. Now technically you could just use the get command and if it returns an element then it means that the key exists and if it returns nil then it means that key did not exist.


2 Answers

Short answer: No

Redis 'creates the hash' when first item is inserted and 'removes the hash' when last item is removed. I'm using Redis 2.8 and there is no option to 'let an empty hash be'.

Addendum: Same is true for Redis 6 as well.

like image 177
Manu Manjunath Avatar answered Oct 12 '22 22:10

Manu Manjunath


Manu is right. You have no way of doing that.

But if you explain why you would want to do it, then we might help you better. As you know, in Redis you can set an attribute on a Hash even if it doesn't exist previously, so you don't not need to first create the Hash and then set the attributes. With that on mind, there is no need to keep an empty Hash that would just be wasting memory.

What is your use case?

update: after reading your use case, I am improving the answer.

For your problem of "volatile" hashes, you can do something easy. After you run your KEYS (or SCAN) command, you can create a SET containing all the names of the hashes that existed in this iteration. You can call this something like "last_seen_keys". What you want to do now is, after you call KEYS, you create a set that you call "current_keys". Now you just run a diff between the two sets, so you can see which keys were present in the last pass and not in this one. You can set your values in statsd to zero for those keys. After that, you delete the "last_seen_keys" SET and you rename the "current_keys" SET to "last_seen_keys". That should do the trick

like image 37
Javier Ramirez Avatar answered Oct 12 '22 22:10

Javier Ramirez