Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis data structure design for sorting time-based values

I'm performing some analysis on a data stream and publishing the results on a Redis channel. Consumers subscribe to these channels and get real-time data feeds. All historical data analysis results are lost.

Now I want to add the ability to store historical data in Redis so that consumers can query this historical data (mainly by time). Since the analysis results are partitioned by time what would be the a good design to store the results in Redis?

like image 395
Soumya Simanta Avatar asked Jun 17 '13 17:06

Soumya Simanta


People also ask

Is Redis a time series database?

Redis has been used for storing and analyzing time series data since its creation.

What does Redis use to sort the elements of a sorted set?

A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include: Leaderboards.

Can we sort data in Redis?

Sorting in Redis is similar to sorting in other languages: we want to take a sequence of items and order them according to some comparison between elements. SORT allows us to sort LIST s, SET s, and ZSET s according to data in the LIST / SET / ZSET data stored in STRING keys, or even data stored in HASH es.

Which data structure is used in Redis?

HyperLogLog. Redis HyperLogLog is a probabilistic data structure used to count unique values (set cardinality) at a constant memory size. You can add and count a large number of unique items with memory efficiency, and merge two or more HyperLogLog data structures into one.


1 Answers

Use redis sorted sets.

Sorted sets store data based on "scores", so in your case, just use a time stamp in millis; the data will be sorted automatically, allowing you to retrieve historical items using start/end date ranges, here's an example...

Add items to a sorted set...

zadd historical <timestamp> <dataValue>

..add some sample data..

 zadd historical 1 data1
 zadd historical 2 data2
 zadd historical 3 data3
 zadd historical 4 data4
 zadd historical 5 data5
 zadd historical 6 data6
 zadd historical 7 data7

..retrieve a subset of items using start/end range...

 zrangebyscore historical 2 5

..returns...

1) "data2"
2) "data3"
3) "data4"
4) "data5"

So, in your case, if you want to retrieve all historical items for the last day, just do this...

zrangebyscore historical <currentTimeInMillis - 86400000> <currentTimeInMillis> 
like image 78
raffian Avatar answered Oct 27 '22 20:10

raffian