Redis is an amazing tool to cache data. It supports different data types to help us cache different kinds of data.
Following are the the data types supported as of Redis 7.0
- Strings
- Hashes
- Lists
- Sets
- Sorted Sets
- Timeseries
In this article, we shall focus mainly on caching timeseries data in redis.
We can cache timeseries data in the following ways:
1. Using the Redis-Timeseries extention
Save data using
ADD KEY Timestamp Record
where, Key is the name of the timeseries.
Timestamp is the field used for sorting the elements.
Record is the field representing the value at the given timestamp.Fetch records using
range
command.RANGE KEY FROM_TS TO_TS
where from_ts and to_ts represent the upper bound and lower bound of the timestamp in the search space.
NOTE: the record field is of type decimal. It supports only numbers.
Therefore, it is very helpful for saving single value records and not lists or maps.
Example: stock values, moisture in soil etc.
However it is not possible to save lists or tuples.
-
In that case, we can make use of Sorted Sets.
2. Sorted Sets
ZADD KEY Timestamp RECORD
.Here, zadd is used to save data in sorted sets. Key is the series name. Timestamp is the field used for sorting. Record can be of type string. Hence we can save json strings in the record field.
To fetch data from sorted sets, use command
ZRANGE FROM_TS TO_TS BYSCORE=TRUE
.Use
by_score
to get data based on timestamp and not index.
Docker Image For Redis Timeseries
docker pull redislabs/redistimeseries
Link: https://hub.docker.com/r/redislabs/redistimeseries
Thank you for reading the blog. Please suggest improvements and like the blog.
Aniketh Deshpande
Top comments (0)