How should I store JSON in redis?
Asked Answered
O

4

45

I have JSON (<1k) to store in Redis through node.js. What are the pros and cons of storing it as an object or string? Are there other options I missed? All processing will ultimately happen on the client side, so converting into an object is not necessary.

SET

var images = JSON.parse(data);          // data is already JSON, is this needed?
callback(images);                       // sends result to the user
r.set('images:' + req.query, images);   // saving the object

GET

callback(images);
Overcoat answered 24/1, 2012 at 12:42 Comment(0)
B
52

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. Pros of this approach is that it allows you to get/set only part of the original object and it might also consume less memory compared to plain strings.

Bicameral answered 24/1, 2012 at 13:9 Comment(4)
Thanks! Looks like the con of the hash structure is that retrieval is much slower, since you would have to iterate through the each field to reconstruct the original.Overcoat
You can retrieve the entire hash with HGETALL command and although it's time complexity is O(n) I think the performance would be visibly affected only with large amounts of field/value pairs.Bicameral
The link for riendlier hash commands github.com/mranney/node_redis#friendlier-hash-commandsIndiana
and what happens if i strip all the json keys and store the values as a csv plain string, wouldnt that be the fastest approach, example instead of storing {name: "John", age: 42} in stringified format, i store "John,42"Dielu
Z
11

The RedisJSON module allows storing, updating and fetching JSON values from Redis keys.

It adds a new JSON datatype and commands to Redis which can be accessed like any other Redis native commands.

e.g.

> JSON.SET user1 $ '{"name":"Ron Dan", "lastSeen":1478476800, "loggedOut": true}'
OK

> JSON.GET user1 $.name
"[\"Ron Dan\"]"

> JSON.NUMINCRBY user1 $.lastSeen 100
"[1478476900]"
Zounds answered 8/11, 2021 at 10:1 Comment(0)
C
5

You could use this library redis-json

Sample usage example:

import Redis from 'ioredis';
import JSONCache from 'redis-json';

const redis = new Redis();

const jsonCache = new JSONCache(redis)

const user = {
  name: "test",
  age: 21,
  gender: "male"
}
await jsonCache.set('123', user)

const result = await jsonCache.get('123')

This library stores the json in Hash set.

It also supports retreival of a single or a set of keys, i.e.

await jsonCache.get('123', "age", "name")
Comptometer answered 25/4, 2020 at 12:18 Comment(4)
seems like something is off (node:63245) UnhandledPromiseRejectionWarning: TypeError: redis_json_1.default is not a constructor Shekinah
I have been messing around with redis-json for 3 days now and now dumped it. Could not get rid of the redis_json_1.default is not a constructor problem. The library is also way to slow with huge json (5MB airport list) because it flattens the data. You can use the redis object to directly store and get data. Just write await redis.set('key',JSON.stringify(data)) to store data and await redis.get('key') to retrieve the json string again. That simple.Magniloquent
is there a way to filter on a JSON field? eg {gender: 'male' }Delusive
Not at the moment and I don't think that going to happen anytime in the future too because redis is merely a key-value store and not a full fledge DB.Comptometer
S
1

This is how i stored json in redis cache

 Models.Student.findAll({
        where: {
            id : req.params.id
        }
    }).then(doc => {
        client.setex(req.params.id, 3600, doc);
        res.send(200, doc);
    }).catch(err =>{
        console.log(err);
        res.send(400, err);
    });
Sievert answered 10/8, 2020 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.