Comprehensive guide to Redis. Part 3.

A time series is an ordered sequence of values that are made over a time interval. You can use time series in statistics, communications, and social networks. I this tutorial we are going to create a simple stock time series Node.js library using Redis Strings. This library records events per second, minute, hour, and day.

This library will be able to save an event at a given timestamp with an insert method and fetch values within a range of timestamps with a fetch method. The library we are going to create provides multiple granularities: second, minute, hour, and day. For example, if an event happens on date 8/11/2015 at 00:00:00(timestamp 1446940800), the following Redis keys will be incremented:
– event:1sec:1446940800
– event:1min:1446940800
– event:1hour:1446940800
– event:1day:1446940800

Create a file stock-timeseries.js with the following code:

Now create a file called using-stock-timeseries.js, which will illustrate how to use our library. This file inserts stock quotes for a TimeSeries called “GAZPROM”, and then fetches values from a different granularities. Before inserting data, we remove all existing keys.

Now run your Redis server via redis-server. Then run node using-stock-timeseries.js. You should see the following output:


That’s all for today 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *