Building the foundation
In this section, we will demonstrate how to create a simple time series library using Redis Strings. This library will be able to save an event that happened at a given timestamp with a method called insert. It will also provide a method called fetch to fetch values within a range of timestamps.
Later on, we will make this library memory-efficient using Hashes instead of Strings, and also add a feature to store and search for unique events in a given timestamp range using Sorted Sets and HyperLogLogs.
The solution supports multiple granularities: day, hour, minute, and second. Every time an event happens in the system, an increment is stored for that specific time in multiple granularities.
For instance, if an event happens on date 01/01/2015 at 00:00:00 (represented by the timestamp 1420070400), the following Redis keys will be incremented (one key per granularity):
- events:1sec:1420070400
- events:1min:1420070400
- events:1hour:1420070400
- events:1day:1420070400
All events...