I have an app (nodejs / express) that needs to find a routing rule to apply based on time of day and day of week.
So for example, I have the following business rules:
- on Mondays and Tuesdays between 09:00 GMT and 12:00 GMT, I need route object ABC to "location x".
- on Tuesdays between 13:00 and 13:30 I need to route ABC to "location y".
( For the purposes of this discussion, it really doesn't matter what object ABC is. )
I am debating between two options, as far as how I should design my keys in my REDIS database
Option 1
Make the day information a part of the object data, like this:
HMSET routing_rules:objectABC_09:00_12:00 days 'mon tues' location X
HMSET routing_rules:objectABC_13:00_13:30 days 'tues' location Y
Advantage of this method - When it's time to update the days list I can simply do this:
HMSET routing_rules:objectABC_09:00_12:00 days 'mon tues thu'
The downside here is that in order to find the right rule, I have to make two queries.... First do a SCAN command to find the right time range... and then if there's a match... do another query to find the days value.
Option 2
Include the day of week information as a part of the key
HMSET routing_rules:objectABC_09:00_12:00_mt location X
HMSET routing_rules:objectABC_13:00_13:30_t location Y
I would use a naming convention like
m = monday
t = tuesday
w = wed
r = thursday
etc.
The advantage to option 2 is that in order to find the right routing rule based on current time and day, I only need to run one SCAN command (we can assume that my SCAN command will return all results in one shot)
But the downside to option 2 is that when I need to add a new day to the key, I think i need to delete the key and value... and then recreate it. Is this correct?
And as of right now, the only way I know how to delete is by doing an HDEL for each value in the object, and then the key is deleted. So for example, I've been doing something like this:
127.0.0.1:6379> HDEL routing_rules:objectABC_00:00_00:00 days 'mon tues' location x
where I have to list all the values in the object to delete the entire key / value pair. In this example, it's not so bad because I only have two values for this key - the location and the days fields. But if there was a lot more data, it'd be a little cumbersome. And I'm not sure if there are other considerations to factor in besides the number of fields associated with this key.
If you have any suggestions on how to design this key for optimal performance and maintenance, I'm all ears. The way I see it, there is no way to avoid running the scan at least once. But this is my first redis database attempt so I apologize in advance for remedial questions / noob mistakes.
EDIT 1
Assuming that I have enough memory and assuming that I only have to save one field / value per key, let's say I create my keys like this:
SET routing_rules:objectABC_09:00_12:00_m X
SET routing_rules:objectABC_09:00_12:00_t X
SET routing_rules:objectABC_13:00_13:30_t Y
And now a request comes in for object ABC and it's Monday at UTC 11. Because my keys represent start times and end times (aka a range ), I don't see how I can find the right key / value pair without doing a scan.
Am I missing something?