2

I am storing user information from nodejs app in the form of

SET user_<userid> {id:"asdad", .....}

I have sets of users organized by updates and such. Sometimes I need to retrieve large amount for users and send them to a client (lets say 100 users)

Currently I use MGET key1, key2, .... then once I get them back I parse the json and return the result.

Would it better for me to store the users in hashes? To retrieve multiple users I could use multi together with HMGET so I would use 100 HMGETs and then get back the user data.

Big advantage I see for the HMGET is that if I need only some of my user fields I can retrieve partial objects instead of full objects.

yojimbo87
  • 65,684
  • 25
  • 123
  • 131
Dmytro Yashkir
  • 1,225
  • 12
  • 12

2 Answers2

4

It would definitely be better to use a hash to store your users, but not by splitting their values up to different hash fields. Use a hash like a key value store of its own.

HMSET users <userid> {"your":"json"}

This is way more memory efficient than using a top level key for every user, as Redis does not have to save metadata (like expire) for the keys. You still have support for HMGET/SET and some others.

Details can be found at: http://redis.io/topics/memory-optimization

Concerning your vs. question I could not find benchmarks, so you will have to benchmark the performance for yourself. Nevertheless using MULTI will get you into troubles when you have to shard your data on two redis instances. MULTI commands currently can't span accross multiple instances.

ProTom
  • 1,194
  • 7
  • 7
  • Good call on the optimization thanks. I also found this question [link](http://stackoverflow.com/questions/4970727/node-js-hiredis-and-the-multi-command) it seems that I do not really need to use multi at all, I can just send all of the requests and then collect the result. – Dmytro Yashkir Dec 10 '11 at 21:07
1

Would it better for me to store the users in hashes?

Storing users in hashes gives you better flexibility in terms of retrieving only specific fields as you mentioned and also no need to parse strings to objects. I asked similar question before.

Community
  • 1
  • 1
yojimbo87
  • 65,684
  • 25
  • 123
  • 131
  • This is what I am thinking of doing, after doing more analysis of the queries I need to do some of the more larger/common ones require retrieval of a small portion of the actual object. – Dmytro Yashkir Dec 10 '11 at 21:09