0

Let's say I have to store the following object in my cache-

{
    student_id: "student123",
    school_id: "xyz123",
    class_id: "class123"
} 

How do I design my Redis data structure in a way where I can retrieve the object by any of the ids? I tried doing a HSET command: HSET student123 school_id xyz123 class_id class123 but this creates a hash for the specific student_id. I also want to make sure that the search is in O(1). Thanks in advance!

To clarify, if I have to search by school_id, how would i go about that?

BeerBiceps
  • 11
  • 3

1 Answers1

2

You need to use multiple keys indexes to get O(1) in your queries.

Consider using other data structures as well. Take a look at Secondary indexing with Redis, how to have relations many to many in redis and this other article on many to many.

Say, using sets, you add the {student123, xyz456, class789} entry as:

SADD student:student123 "xyz456 class789"
SADD school:xyz456 "student123 class789"
SADD class:class789 "xyz456 student123"

You may think "this will increase my memory usage a lot". It does indeed. It is the usual trade-off between memory and processing. Relational databases also do this when creating indexes. But Redis will give you sub-millisecond performance, and Redis uses multiple tricks to optimize memory usage, like ziplists, see https://redis.io/topics/memory-optimization.

What mix of data structures is best depends on the specifics of your use-case.

Consider removing the prefix in your keys if they are constant, just be consistent on the order you place them in the value.

SADD student:123 "456 789"

Keep in mind that sets and sorted sets allow unique members only. If you use one sorted set for students using student ID as score: ZADD students 123 "456 789", and then add another student at the same school-class with ZADD students 235 "456 789" this actually updated the score for "456 789", it didn't add a new value.

LeoMurillo
  • 6,048
  • 1
  • 19
  • 34