Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Username or ID for keys in Redis

Tags:

redis

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

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

Note that I would wish to lookup users by name, therefore requiring a second set of key-values to relate usernames to IDs.

Given that every username is unique and has to be alphanumeric is there any particular reason why it may be beneficial to use the ID system.

The main reason I ask this is because I primarily used IDs as they decreased lookup times in my database, especially when the database was normalized in some way. But Redis doesn't gain this benefit and so I wondered what other reasons there may be to use user IDs instead of just the usernames.

Thanks for any help,

Pluckerpluck

p.s. Due to the way Redis handles hashes I am very actually unsure about the memory differences between the two methods so information on this may be good, though I may go and test this myself later.

like image 790
Pluckerpluck Avatar asked Apr 11 '12 16:04

Pluckerpluck


People also ask

Is username a primary key?

The username of the user table is the Primary Key. The email column could have probably also been a unique identifier, so it can be considered as an alternate key here. A surrogate key is a value, usually without a business meaning — generated with the sole purpose of acting as a unique identifier.

How do I find my Redis key?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

What is a key in Redis?

Redis Strings Redis Keys are Ideal for Keeping Track of Lots of Strings. For the (simple string) key-value data type Redis allows you to set, get, delete, and increment arbitrary string <key, value> pairs. You can only have 1 value at a time for each key (i.e. set is destructive assignment). You can also delete keys.

Are Keys unique in Redis?

Redis is an open-source, in-memory key-value data store. A key-value data store is a type of NoSQL database in which keys serve as unique identifiers for their associated values. Any given Redis instance includes a number of databases, each of which can hold many different keys of a variety of data types.


1 Answers

Does not matter in the use case you defined, but using integer user ids has other advantages.

Sooner or later, you would want to reference user ids in other objects. For example, "Friends of user pluckerpluck". To model that, you'd have two alternatives -

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

VS

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

The former approach uses a regular Hash table to store the elements, and uses a lot of memory. The latter approach uses an integer array and is extremely memory efficient.

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

# 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
like image 84
Sripathi Krishnan Avatar answered Oct 01 '22 18:10

Sripathi Krishnan