Username or key id in Redis

I was wondering what the benefits of using user IDs were, rather than just using a unique username for keys.

  • user: USERNAME => {password: HASH, age: 45}
  • user: 0 => {username: USERNAME, password: HASH, age: 45}

Please note that I would like to search for users by name, so a second set of key values ​​is required to associate user names with identifiers.

Given that each username is unique and must be alphanumeric, is there any special reason why it might be useful to use an identification system.

The main reason I ask about this is because I mostly used identifiers, as they reduced the search time in my database, especially when the database was normalized somehow. But Redis does not get this benefit, and so I wondered what else could be the reasons for using user IDs, not just usernames.

Thanks for any help

Plückerpluck

ps Due to the way Redis handles hashes, I'm not really sure about the memory differences between the two methods, so this information may be good, although I can go and check it later.

+6
source share
1 answer

It doesn’t matter in the case you are using, but using whole user IDs has other advantages.

Sooner or later you will need to refer to user IDs in other objects. For example, "Friends of pluckerpluck." To simulate this, you will have two alternatives -

friendsof:pluckerpluck -> SET{tom, dick, harry}

VS

friendsof:1 --> SET{2, 3, 4}

The previous approach uses a regular Hash table to store items and uses a lot of memory. The latter approach uses an integer array and is extremely memory efficient.

This special encoding for integers is called IntSet. You can control the behavior of Redis using the set-max-intset-entries property

 # Sets have a special encoding in just one case: when a set is composed # of just strings that happens to be integers in radix 10 in the range # of 64 bit signed integers. # The following configuration setting sets the limit in the size of the # set in order to use this special memory saving encoding. set-max-intset-entries 512 
+13
source

Source: https://habr.com/ru/post/912953/


All Articles