Redis key naming conventions? [closed]

Solution 1:

What are the normal naming convention for keys in redis? I've seen values separated by : but I'm not sure what the normal convention is, or why.

Yes, colon sign : is a convention when naming keys. In this tutorial on redis website is stated: Try to stick with a schema. For instance "object-type:id:field" can be a nice idea, like in "user:1000:password". I like to use dots for multi-words fields, like in "comment:1234:reply.to".

Are you able to query for just the beginning of the key to return all users?

If you mean something like directly querying for all keys which starts with user: there is a keys command for that. This command should be however used only for debugging purpose since it's O(N) because it's searching through all keys stored in database.

More appropriate solution for this problem is to create dedicated key, let's name it users, which will store all the users keys, for example, in list or set data structure.

Solution 2:

We use a colon (:) as namespace separator and a hash (#) for id-parts of keys, e.g.:

logistics:building#23

Solution 3:

A convention seems to be colon (:) but I'm a web developer so I personally prefer slash (/) for the separator. Slash is already so important separator within URLs which are meant to be Uniform Resource Locators so kind of keys for resources. Why to take a different approach with colon (:)? Does it help anything?

Consider this example:

We have an RESTful API for toy objects. There is a one:

http://example.com/api/toy/234 

Where we have it stored? We use Redis and slashes so the key is obvious:

toy/234

This is the unique key for the toy. The key can now be used also on client side:

{
    key: "toy/234",
    color: "red",
    url: function () {
        return API_BASE_URL + this.key;
    }
}

An user requests an object with key toy/666. How to get it from Redis? A Node.js related example:

redis.get(key, function reply_callback(error, toystring) {
    var toy = JSON.parse(toystring);
    ...
}

No need to convert slashes to colons and vice versa. Convenient, don't you think?

Warning: always ensure that user is able to access only things you intended. The raw URL-to-key approach above is able to fetch user/1/password as well, as noted by commentators. This should not be a problem if you use Redis as a public read-only cache.

Solution 4:

I don't know if there really are widespread "best practices" for Redis key naming yet.

I've experimented with using ASCII NUL characters as my separators (since Redis and Python are both 8-bit clean). It looks a little ugly if you're looking at raw keys, but the idea is to hide it behind an abstraction layer. Colon and pipe symbols are obvious alternatives so long as the components of your name space are either guaranteed not to use them or you're willing to encode each component as necessary. However, if you'd be encoding them then you'd want develop the abstraction layer and avoiding viewing raw keys anyway ... which brought me right back to just using \0 in my reasoning.

I'll be interesting in seeing if any other opinions are articulated on this.

Solution 5:

For your usecase it seems to me HSET/HGET would be a better fit. There is also HKEYS command.

All those commands have same complexity as GET/SET/KEYS, so why not use them?

You could have this structure then:

  • users > 00 > value
  • users > 01 > value

or:

  • users:username > 00 > value
  • users:username > 01 > value

Just extract user's ID and use it as a hash key. I personally prefer this approach as it feels nicer and also you can easily query for existing user IDs.