Radish set against hash

In many Redis textbooks (such as this one ), data is stored in a set, but with several values โ€‹โ€‹combined into a string (that is, a user account can be stored in a set as two entries: "user: 1000: username" and "user: 1000: password ").

However, Redis also has hashes. It seems like it makes sense to have a hash of "user: 1000" that contains the entry "username" and the entry "password". Instead of concatenating strings to access a specific value, you simply access them directly in the hash.

So why is it not used so much? Is this just old textbooks? Or do Redis hashes have performance issues?

+49
redis
Nov 26
source share
2 answers

Redis hashes are good for storing more complex data, as you suggest in your question. I use them just for this - to store objects with several attributes that need to be cached (in particular, inventory data for a specific product on an e-commerce site). Of course, I could use a concatenated string, but this adds unnecessary complexity to my client code, and updating a single field is not possible.

Perhaps you are right - study guides may simply be due to the fact that hashes were introduced. They were clearly designed to store object representations: http://oldblog.antirez.com/post/redis-weekly-update-1.html

I believe that one of the problems would be the number of commands that Redis should serve when a new element is inserted (n is the number of commands, where n is the number of fields in the Hash) compared to the simple String SET command. I did not find that this is a problem yet on a service that hits Redis about 1 million times a day. Using the right data structure is more important to me than a slight performance impact.

(Also see my comment regarding Redis Sets vs. Redis Strings - I think your question relates to strings, but correct me if I am wrong!)

+32
Nov 26
source share

Hashes are one of the most efficient storage methods in Redis, even if they allow you to recommend them for use whenever possible.

http://redis.io/topics/memory-optimization

Use hashes if possible

Small hashes are encoded in a very small space, so you should try to present your data with hashes whenever possible. For example, if you have objects representing users in a web application, instead of using different keys for your first name, last name, email, password, use one hash with all the necessary fields.

+28
Jul 01 '14 at 8:17
source share



All Articles