0

Say I want to store user preferences...something simple like this:

{
    "favoriteColor": "green",
    "bestFriends": [
        "Tom",
        "Jenny",
        "Horton"
    ]
}

What's the best, most performant way to store this in redis cache (optimized for reads)?

Imagine UserId = 123

NOTE: Below I'm using the Redis documentation's way of representing the various structures. See here.

  1. Simple, flat, key/value pairs right in the root?

    user-123-favoriteColor = green (this is a STRING type)
    user-123-bestFriends = 1) "bestFriends" (SET TYPE) 2) "Tom" 3) "Jenny" 4) "Horton"

  2. Hierarchical structure (hash of values)

    user-123 = 1) "favoriteColor" (STRING type) 2) "green" 3) "bestFriends" (SET TYPE) 4) "Tom" 5) "Jenny" 6) "Horton"

And a related question...is there any reason not to store user preferences in redis vs the domain sql database?

And one more related question...is it a bad idea to store all users under one root key called "users"?

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
richard
  • 12,263
  • 23
  • 95
  • 151

2 Answers2

1

Hierarchical structure should be preferred.
This answer gives a lot of explanation and helped me.

Community
  • 1
  • 1
Alex.Li
  • 540
  • 1
  • 4
  • 12
0

From the horses mouth:

Use hashes when possible
Small hashes are encoded in a very small space, so you should try representing your data using hashes every time it is possible. For instance if you have objects representing users in a web application, instead of using different keys for name, surname, email, password, use a single hash with all the required fields. If you want to know more about this, read the next section.

So the answer is yes, use hashes where possible.

The answer here is a good way to do it.

Community
  • 1
  • 1
richard
  • 12,263
  • 23
  • 95
  • 151