redissyntaxkey-value-store

What is the purpose of colons within Redis keys


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.


Solution

  • 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 strings, so colons were used:

    SET user:bob:email bob@example.com
    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 bob@example.com
     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.