Search code examples
redisnode-redisredis-cluster

Proper way of caching data in Redis


I'm trying to build a freelance platform with using MongoDB as main database and RedisDB for caching, but really couldn't figure out which way is the proper way of caching. Basically I'll store jwt tokens, verification codes and other stuff with expiration date. On the other hand, let's say I'll store 5 big collection as Gigs, JobOffers, Reviews, Users, Companies. I also want to use query them.

Example Use Case 1

Getting job offers only categorised as "Web Design"

Example Use Case 2

Getting job offers only posted by Company X

Option 1

for these two queries i can create two hashes

hash1

"job-offers:categoryId", jobOfferId, JobOffer

hash2

"job-offers:companyId", jobOfferId, JobOffer

Option 2

Using RedisJson and RedisSearch for querying and holding everything in JSON format

Option 3

Using redisSearch with creating multiple hashes

I couldn't figure out which approach will be best, or is there any other approach which is better than both of them.


Solution

  • Option 1 seems like suitable for your scenario. Binding job offers with category or company ids is the smartest solution.

    You can use HGETALL to get all fields data from your hashset.

    When using redis as a request caching mechanism, please remember that you have to keep redis cache updated consistently if it is generated from sql or no-sql db.