Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis set vs hash

Tags:

redis

In many Redis tutorials (such as this one), data is stored in a set, but with multiple values combined together in a string (i.e. a user account might be stored in the set as two entries, "user:1000:username" and "user:1000:password").

However, Redis also has hashes. It seems that it would make more sense to have a "user:1000" hash, which contains a "username" entry and a "password" entry. Rather than concatenating strings to access a particular value, you just access them directly in the hash.

So why isn't it used as much? Are these just old tutorials? Or do Redis hashes have performance issues?

like image 261
Nairou Avatar asked Nov 26 '12 00:11

Nairou


People also ask

What is difference between HSET and set in Redis?

Redis comes with two different built-in functions for setting key-value pairs in hash: hset — Set field(key) in the hash with a value. If the hash does not exist, a new hash will be created. If the field (key) already exists in an existing hash, it will be overwritten by the new value.

What is a Redis set?

Sets. Redis Sets are an unordered collection of Strings. It is possible to add, remove, and test for existence of members in O(1) (constant time regardless of the number of elements contained inside the Set). Redis Sets have the desirable property of not allowing repeated members.

What is a hash in Redis?

A Redis hash is a data type that represents a mapping between a string field and a string value. Hashes can hold many field-value pairs and are designed to not take up much space, making them ideal for representing data objects.

Is Redis hash ordered?

NO, Redis hash doesn't maintain the order. Since it's a hash, it's unordered.


1 Answers

Redis hashes are good for storing more complex data, like you suggest in your question. I use them for exactly that - to store objects with multiple attributes that need to be cached (specifically, inventory data for a particular product on an e-commerce site). Sure, I could use a concatenated string - but that adds unneeded complexity to my client code, and updating an individual field is not possible.

You may be right - the tutorials may simply be from before Hashes were introduced. They were clearly designed for storing Object representations: http://oldblog.antirez.com/post/redis-weekly-update-1.html

I suppose one concern would be the number of commands Redis must service when a new item is inserted (n number of commands, where n is the number of fields in the Hash) when compared to a simple String SET command. I haven't found this to be a problem yet on a service which hits Redis about 1 million times per day. Using the right data structure to me is more important than a negligible performance impact.

(Also, please see my comment regarding Redis Sets vs. Redis Strings - I think your question is referring to Strings but correct me if I'm wrong!)

like image 111
Mike G Avatar answered Sep 21 '22 07:09

Mike G