user2869520
user2869520

Reputation: 333

What is the right data structure to use in Redis to query based on two attributes of an object

I'm new to Redis and would like to get advice from the experts on my use case. I have an event object that has start_time_of_event and end_time_of_event as two of the attributes(there are plenty more attributes in the object). I have millions of these events and need to store them in Redis so that I can query them at runtime. I was hoping to use the timestamp as key and the event object as the value. While querying, I need to get the list of events where start_time_of_event <= current_time and end_time_of_event>=1 week from now. Not sure if ZRANGE, LRANGE or any other data structure in Redis supports using multiple(complex) keys. Any suggestions on what's the best way to achieve the above use case using Redis?

Thanks a lot in advance.

Upvotes: 1

Views: 514

Answers (2)

Siscia
Siscia

Reputation: 1491

Another option would be to use zeeSQL to store those informations inside a secondary index (an SQL table).

Once those data are in a table, you can query them using standard SQL.

It would be just a simple SQL query, without the need of maintaing the Redis structures.

Upvotes: 0

sonus21
sonus21

Reputation: 5398

You should store events in two of Redis ZSETs, for one set score should be start_time_of_event and for another, the score would be end_time_of_event

Let's call them start_events and end_events respectively.

Add:

ZADD start_events start_time event
ZADD end_events end_time event

Search

-- randomly generate a destination id, search the events 
-- and store in that based on the start time

ZRANGESTORE random-start-time-dst start_events 0 current_time

-- randomly generate a destination id for end-time search, post filter 
-- store results in that destination

ZRANGESTORE random-end-time-dst end_events current_time+7.weeks -1

-- Take the intersection of these two sets
ZINTER INT_MAX random-start-time-dst random-end-time-dst 

-- Delete the newly created collections to  free up the memory 
DEL random-start-time-dst
DEL random-end-time-dst

Upvotes: 2

Related Questions