Does redis provide support for secondary index on string data type?
I would like to know how i can store the data in redis along with seconday key.
Example let say i want to store the entity with key value as ‘id:<some_string>’ and it’s payload as a compressed json string. but sometimes i need to search the payload with another value in json which might also be unique.
Like, sql query i can able to query with other field in JSON apart from key?
Also, I would like to know while setting value in redis using primary key, how can i set the secondary index value for it.
2
Answers
"sometimes i need to search the payload with another value in json"
You can’t, Redis isn’t built for this. Redis is fast, mostly for key-value storage. You’ll need a relational database if you have a lot of fields in json to index. Yes, you can use redissearch.io, though you should note its free version won’t allow you use on platforms like AWS or Azure, you’ll have to deploy your own Redis, and it doesn’t support redis-cluster. So it would be better if you just use relational database or Elasticsearch.
However, if you only need one or two extra index to find the value(json payload in your case), it can be achieved efficiently.
Store your json as redis STRING
nothing unusual here.
Or better, as redis HASH
(HASH saves some memory over STRING)
Then store your second indexes in redis HASH
So it basically maps your second_index to your primary key. You maintain the second index hash while you update your main value STRING/HASH. Do the same for your third, forth index.
Note the multiple operations are not atomic, if it’s important for you, you should write them in Lua scrip.
JRediSearch a client for the Redis module RediSearch, provides an extension to Jedis that allows to easily define secondary indexes in Redis with a simple query command.
e.g.