Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis performance, store json object as a string

Tags:

node.js

redis

I need to save a User model, something like:

{ "nickname": "alan",
  "email": ...,
  "password":...,
  ...} // and a couple of other fields

Today, I use a Set: users
In this Set, I have a member like user:alan
In this member I have the hash above

This is working fine but I was just wondering if instead of the above approach that could make sense to use the following one:

Still use users Set (to easily get the users (members) list)
In this set only use a key / value storage like:

key: alan value : the stringify version of the above user hash

Retrieving a record would then be easier (I will then have to Parse it with JSON).

I'm very new to redis and I am not sure what could be the best. What do you think ?

like image 280
Luc Avatar asked Apr 20 '11 12:04

Luc


People also ask

How would you efficiently store JSON in Redis?

You can store JSON in redis either as a plain string in dedicated key (or member/value of a set/list) or in a hash structure. If you look at node_redis docs into Friendlier hash commands part you'll see that it gives you some useful methods for manipulating JSON based data.

Can Redis hold JSON?

The RedisJSON module provides JSON support for Redis. RedisJSON lets you store, update, and retrieve JSON values in a Redis database, similar to any other Redis data type. RedisJSON also works seamlessly with RediSearch to let you index and query JSON documents.

What is RedisJSON?

RedisJSON is a high-performance NoSQL document store that allows developers to build modern applications. It provides native APIs to ingest, index, query, and run full-text search on JSON documents both on-premises and as a managed service in the cloud.


3 Answers

You can use Redis hashes data structure to store your JSON object fields and values. For example your "users" set can still be used as a list which stores all users and your individual JSON object can be stored into hash like this:

db.hmset("user:id", JSON.stringify(jsonObj));

Now you can get by key all users or only specific one (from which you get/set only specified fields/values). Also these two questions are probably related to your scenario.

EDIT: (sorry I didn't realize that we talked about this earlier)

Retrieving a record would then be easier (I will then have to Parse it with JSON).

This is true, but with hash data structure you can get/set only the field/value which you need to work with. Retrieving entire JSON object can result in decrease of performance (depends on how often you do it) if you only want to change part of the object (other thing is that you will need to stringify/parse the object everytime).

like image 100
yojimbo87 Avatar answered Oct 21 '22 13:10

yojimbo87


One additional merit for JSON over hashes is maintaining type. 123.3 becomes the string "123.3" and depending on library Null/None can accidentally be casted to "null".

Both are a bit tedious as that will require writing a transformer for extracting the strings and converting them back to their expected types.

For space/memory consumption considerations, I've started leaning towards storing just the values as a JSON list ["my_type_version", 123.5, null , ... ] so I didn't have overhead of N * ( sum(len(concat(JSON key names))) which in my case was +60% of Redis's used memory footprint.

like image 36
David Avatar answered Oct 21 '22 13:10

David


bear in mind: Hashes cannot store nested objects, JSON can do it.

like image 20
mzalazar Avatar answered Oct 21 '22 13:10

mzalazar