Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing object properties in redis

Lets say I have an object (User) which consists of a few properties (ID, Name, Surename, Age). Which way is better to store this object in redis?

  • store each property value in dedicated key, for example user:{id}:id, user:{id}:name, user:{id}:surename, user:{id}:age
  • store whole User object as JSON string in one key, for example user:{id}:json (value of the key will be something like this: {"ID": 123, "Name": "Johny", "Surename": "Bravo", "Age": 22})
like image 903
yojimbo87 Avatar asked Mar 09 '11 21:03

yojimbo87


2 Answers

According to these two sources probably the optimal solution would be to use hashes because of memory consumption when using dedicated keys and long string in scenario with JSON as key value.

like image 110
yojimbo87 Avatar answered Nov 20 '22 07:11

yojimbo87


From official Redis

Use hashes when possible

Small hashes are encoded in a very small space

When you haven't to much fields in it.

Every time an hash will exceed the number of elements or element size specified it will be converted into a real hash table, and the memory saving will be lost.

like image 1
Aurel Avatar answered Nov 20 '22 08:11

Aurel