Ok, lets separate our tasks:
- We need an index which contain all of our robots, so we can iterate over them
- Probably we will need to store some generic information about our robot
- We need to store geo-history for every robot
- We need to cleanup old data every X
1) Lets make ZSET which contain robot ID and his SCORE will be last-activity-timestamp, in future we will be able to delete non-active robots using this index.
ZADD ZSET:ROBOTS <timestamp> robot:17
or event better just 17
without robot:
because of redis will store integers as 4 bytes in the RAM.
2) Lets store our robot generic info in HSET
HSET HSET:ROBOT:17 name "Best robot ever #17" model "Terminator T-800"
3) Generally we can use several ways to store it, for example we can take regular ZSET using multi dimensional indexes technique (https://redis.io/topics/indexes#multi-dimensional-indexes), but it very complicated to understand, so lets use simpler redis GEO
GEOADD GEO:ROBOT:17 13.361389 38.115556 "<timestamp>:<message-data>"
Internally GEO use regular ZSET, so we can easily iterate over it by ZRANGE or ZRANGEBYSCORE commands.
And of course we can use GEO commands like GEORADIUS for our needs.
4) The cleanup process. I suggest to clean-up by time, but you can make it in same way by number of entries, just use ZRANGE
instead ZRANGEBYSCORE
Lets find all of our non active robots that was non active at least a week.
ZRANGEBYSCORE ZSET:ROBOTS -inf <timestamp-of-week-before>
Now we need to iterate over those ID's and remove un-needed HSET, GEO keys and remove it from our index
ZREM ZSET:ROBOTS 17
DEL HSET:ROBOT:17
DEL GEO:ROBOT:17
Now we need to remove only old GEO-history entries, as I said above GEO in redis is a regular ZSET under the hood, so lets use ZRANGE
ZRANGE GEO:ROBOT:17 0 -1
We will get list of entries, but it will be sorted strange because of GEO, each score
will be GEO location
.
Our entries formatted as ":" so we can use split(':')
and compare timestamp, if it to old we remove it. For example our timestamp is 12345678
and message is hello
ZDEL GEO:ROBOT:17 1234567:hello
P.S. I highly recommend you to read this awesome article about ZSET's in redis https://redis.io/topics/indexes
In short: Redis sorting items not only by score but by key name too, this means that entries with same score will be sorted alphabetical, which is very useful!
ZADD key 0 ccc 0 bbb 0 aaa
ZRANGE key 0 -1
will return you sorted set:
- "aaa"
- "bbb"
- "ccc"