Storing Json object in redis for fast querying

6.8k views Asked by At

I've been using MongoDB to store and query schema-less json documents (~10 mn records). The queries typically involve finding json documents having a matching key-value pair and run into seconds. I was looking at ways to make queries run faster and came across the Redis database. Would it be a good idea to shift to Redis? Is there a better database for my use-case?

Also, could someone please explain how I could store schema-less json objects in Redis (and be able to query them later)?

Thanks!

2

There are 2 answers

0
Itamar Haber On BEST ANSWER

Redis doesn't do JSON, or rather it just treats it as an opaque string, but in some cases (perhaps yours) that's all you need. In such cases, store your document as is and give it a meaningful key name (e.g. the document's id).

Querying is a little, but just so, trickier. When upserting/removing your document, you'll need to maintain an index for each k-v that you'll later want to query. The index maps the values relevant doc ids, so querying by value means first accessing the index and then fetching the actual JSON documents.

0
peak On

UPDATE: As of at least March 21, 2017, RedisLabs supports a module called RedisJSON that adds a JSON datatype.

See e.g. https://oss.redislabs.com/redisjson/