What is the purpose of colons within Redis keys

21,577

Solution 1

The colons have been in earlier redis versions as a concept for storing namespaced data. In early versions redis supported only strings, if you wanted to store the email and the age of 'bob' you had to store it all as a string, so colons were used:

SET user:bob:email [email protected]
SET user:bob:age 31

They had no special handling or performance characteristics in redis, the only purpose was namespacing the data to find it again. Nowadays you can use hashes to store most of the coloned keys:

 HSET user:bob email [email protected]
 HSET user:bob age 31

You don't have to name the hash "user:bob" we could name it "bob", but namespacing it with the user-prefix we instantly know which information this hash should/could have.

Solution 2

Colons are a way to structure the keys. They are not interpreted by redis in any way. You can also use any other delimiter you like or none at all. I personally prefer /, which makes my keys look like file system paths. They have no influence on performance but you should not make them excessively long since redis has to keep all keys in memory.

A good key structure is important to leverage the power of the sort command, which is redis' answers to SQL's join.

GET user:bob:color   -> 'blue'
GET user:alice:color -> 'red'

SMEMBERS user:peter:friends -> alice, bob

SORT user:peter:friends BY NOSORT GET user:*:color   -> 'blue', 'red'

You can see that the key structure enables SORT to lookup the user's colors by referencing the structured keys.

Share:
21,577

Related videos on Youtube

Ryan
Author by

Ryan

Updated on August 01, 2020

Comments

  • Ryan
    Ryan over 3 years

    I'm learning how to use Redis for a project of mine. One thing I haven't got my head around is what exactly the colons are used for in the names of keys.

    I have seen names of key such as these:

    users:bob
    color:blue
    item:bag
    

    Does the colon separate keys into categories and make finding the keys faster? If so can you use multiple colons when naming keys to break them down into sub categories? Lastly do they have anything to do with defining different databases within the Redis server?

    I have read through documentation and done numerous Google searches on the matter but oddly I can't find anything discussing this.

  • Saurabh Hirani
    Saurabh Hirani almost 9 years
    Using / is a great suggestion!
  • CivFan
    CivFan over 8 years
    I wonder why : was chosen when . seems more common in various languages for this kind of thing.
  • Tobias P.
    Tobias P. over 8 years
    Maybe to be exactly different from the point-operator which is used in most languages for object access
  • Pavan Kumar
    Pavan Kumar about 5 years
    While trying my hands on Redis, realized that UI tools like redis commander (github.com/joeferner/redis-commander) shows a better visualization when keys are structured with a colon rather than a slash. Thus my vote for colon for its historical significance.