skip to Main Content

I am quite new with Redis and cache memory and I am bit confused when searching on the official documentation and previous stackoverflow responses .
My use case is very simple : I have to use redis to store a pair of key/value. The key is named keyId and the value is named rawValue. So far, the primary key is just keyId. I was able to query easily a particular rawValue from a keyId. Now I need to query a keyId from a rawValue. I was trying to see how to create a new index (or secondary index) like with DynamoDB but the solution of Secondary Indexes in Redis seems to be complicated for my simple use case since there is the notion of hash and I am afraid to develop something complicated for nothing. Futhermore, according to this stackoverflow comment I have understood that he recommends to create a new pair of key/value where in my case key=rawValue and value=keyId. In this case, it would be very easy but maybe in terms of memory is not optimal as indexes.

Do you think this is the best approach for my use case or do I have to use indexes ? If I have to use indexes, can you recommand the best way to do it ?

2

Answers


  1. Recommended approach is to create a secondary index by storing a reverse mapping where the rawValue becomes the key, and the corresponding keyId becomes the value. While it may seem less memory-efficient, it’s a straightforward and practical solution for your scenario.

    Storing keyId to rawValue (original data):

    SET keyId1 rawValue1
    SET keyId2 rawValue2
    SET keyId3 rawValue3
    

    Storing rawValue to keyId (reverse mapping):
    You can store the reverse mapping as a simple key-value pair, where the key is the rawValue, and the value is the corresponding keyId.

    SET rawValue1 keyId1
    SET rawValue2 keyId2
    SET rawValue3 keyId3
    

    Redis is designed to handle these types of data structures efficiently, and the extra memory overhead is often a reasonable trade-off for simplicity and ease of use.

    Login or Signup to reply.
  2. There is a simpler and faster approach using Redis Search – (The best option to start it is using the Redis Stack – https://redis.io/docs/install/install-stack/)

    Once installed you can use the Redis Insight tutorials (already in the Stack) or follow this Quick Start (https://redis.io/docs/get-started/document-database/). Here some simple 3 steps to follow in the case shared:

    1. Create an Index
    FT.CREATE idx ON HASH PREFIX 1 "mykeys:" SCHEMA keyid TAG
    
    1. Load the documents
    HSET myKeys:1 keyid "rawValue1"
    HSET myKeys:2 keyid "rawValue2"
    HSET myKeys:3 keyid "rawValue3"
    
    1. Query it:
    FT.SEARCH idx *
    1) "3"
    2) "myKeys:3"
    3) 1) "keyid"
       2) "rawValue3"
    4) "myKeys:1"
    5) 1) "keyid"
       2) "rawValue1"
    6) "myKeys:2"
    7) 1) "keyid"
       2) "rawValue2"
    
    

    Exact Match:

    FT.SEARCH idx '@keyid:{rawValue1}'
    1) "1"
    2) "myKeys:1"
    3) 1) "keyid"
       2) "rawValue1"
    

    Wildcards:

    FT.SEARCH idx '@keyid:{rawValue*}'
    1) "3"
    2) "myKeys:3"
    3) 1) "keyid"
       2) "rawValue3"
    4) "myKeys:1"
    5) 1) "keyid"
       2) "rawValue1"
    6) "myKeys:2"
    7) 1) "keyid"
       2) "rawValue2"
    

    Multiple TAGs with OR "|" or NOR "-"

    FT.SEARCH idx '@keyid:{rawValue1} | @keyid:{rawValue2}' DIALECT 2
    
    1) "2"
    2) "myKeys:1"
    3) 1) "keyid"
       2) "rawValue1"
    4) "myKeys:2"
    5) 1) "keyid"
       2) "rawValue2"
    
    FT.SEARCH idx '-(@keyid:{rawValue1} | @keyid:{rawValue2})' DIALECT 2
    
    1) "1"
    2) "myKeys:3"
    3) 1) "keyid"
       2) "rawValue3"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search