Time Series data can be used to measure anything from remote sensor readings to stock market feeds. Working with time series data in .NET is a snap with Redis and NRedisTimeSeries. In this tutorial, we'll explore how to use them together.
Start out by creating a project with the command:
Next, inside the
TimeSeriesDemoApp directory, run the command:
The next step is to get a RedisTimeSeries database up and running. The easiest way to do that for development purposes is to use Docker:
If you are well past getting started and want to get something into your production, your best bet is to run it in Redis Enterprise.
Program.cs file, in here, create a new ConnectionMultiplexer using a connection string (which will vary based on what deployment you're using). Then, for our basic Docker setup, you'll just run:
Now that you've gotten a handle to Redis, your next step is to initialize a time series. This will be a bit of a toy example. We are going to start off by just creating a time series called
sensor, we will set its retention period to 1 minute, and we just give it an
id label of
Next, we'll create a task that will run a consumer in the background. Every second it will send a random integer between 1 and 50 into our time series.
With the Producer created, we'll create a consumer loop that will do the opposite. Every second it will pull the most recent item in the time series off and print it out.
Now that we produce and consume data run the app with
dotnet run. This will run a continuous loop in the time series as it continually produces and consumes data points.
Now what we've done so far is produce a time series of random integer data for our .NET app to consume. What if we wanted to do something a bit more interesting with it, though? Let's say we wanted to calculate a moving average every 5 seconds. We can do that with ease using Redis TimeSeries.
Let's run min, max, and average every 5 seconds on our Time Series. Redis will do this passively in the background after we set up some keys to store them in and set up the rules.
With the rules established, we can consume the relevant time series to get the results. When we were creating the time series for our aggregations, we added a label to all of them:
new TimeSeriesLabel("aggregation-for", "sensor-1"). We essentially told Redis that this time series would be an aggregation for
sensor-1. We can then use that label to find just the time series aggregations of
sensor-1. With this in mind, we can grab all the sensor aggregations in one command to Redis using
With all these sets, you can now just update the
Task.WhenAll call at the end to include the new consumer task:
When we run the application with
dotnet run, you will see that the application will also print out the average, min, and max for the last 5 seconds of the time series, in addition to the regular ticks of the time series.