Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key types supported by Redis

Tags:

redis

What are the different key types supported by Redis? The documentation mentions all the various types (strings, set, hashmap, etc) of values supported by Redis, but I couldn't quiet find the key type information.

like image 218
Anirudh Jayakumar Avatar asked Sep 18 '17 16:09

Anirudh Jayakumar


2 Answers

From redis documentation (Data types intro):

Redis 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. A few other rules about keys:

  • Very long keys are not a good idea. For instance a key of 1024 bytes is a bad idea not only memory-wise, but also because the lookup of the key in the dataset may require several costly key-comparisons. Even when the task at hand is to match the existence of a large value, hashing it (for example with SHA1) is a better idea, especially from the perspective of memory and bandwidth.
  • Very short keys are often not a good idea. There is little point in writing "u1000flw" as a key if you can instead write
    "user:1000:followers". The latter is more readable and the added
    space is minor compared to the space used by the key object itself
    and the value object. While short keys will obviously consume a bit
    less memory, your job is to find the right balance.
  • Try to stick with a schema. For instance "object-type:id" is a good idea, as in "user:1000". Dots or dashes are often used for multi-word fields, as in "comment:1234:reply.to" or "comment:1234:reply-to".
  • The maximum allowed key size is 512 MB.

From my experience any binary sequence typically means a String, but I may not be familiar with languages where you can achieve this by using other data types.

like image 153
Igor Milla Avatar answered Sep 18 '22 17:09

Igor Milla


Keys in Redis are all strings, so it doesn't really matter what kind of value you pass into a client. Under-the-hood the RESP protocol is used and it will pass the value as a string to the engine.

Example:

ZADD some_key 1 some_value

some_key is always a string, even if you pass 3 as key, it is handled as a string. This is true for every client.

like image 27
tuned Avatar answered Sep 20 '22 17:09

tuned