0

I need to store some user and document state information in a json-like object. For example:

{
    "name": "Henry",
    "company": "Disney",
    "is_recommended": true,
    "plan_type" "free",
    etc.
}

This information is fetched from the database and stored in memory in the session when the user logs in or changes any user information.

I have some experience with redis and I find myself comfortable with using that, but I was wondering if the above could be done in redis without jumping through too many hoops. For example, here are some queries I would need to run:

update items set plan_type="Paid" where company = "Disney";

Do you think doing the above would be possible in redis, or should I try using something else (my thought was mongodb) to accomplish the above?

99% of the usage would be reading data, however 1% would be updating data in bulk fashion, and it'd need to be done instantaneously.

A similar question was asked six years ago -- What's the most efficient document-oriented database engine to store thousands of medium sized documents? -- but I'm sure much has changed in both redis and mongodb since then...

David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

1

You can build a secondary index for the company field with a SET or LIST:

SADD company:Disney userid1
SADD company:Disney userid2
SADD company:OtherCompany userid3

When you need to update the data, do the following steps:

  1. Search the company index to get user ids: SMEMBERS company:Disney
  2. Search the user index to get the user attribute: for each user do: GET userid
  3. Update the attribute
  4. Update the user index: for each user do: SET userid new-attributes

This the built-in way to achieve the goal, it needs more work, and a little complex.

However, as @Not_a_Golfer mentioned in the comment, Redis has a module called RediSearch to do the work for you. If you are playing with Redis 4.0 or above, you can try it.

for_stack
  • 21,012
  • 4
  • 35
  • 48