Processing Time-Series Data with Redis and Apache Kafka
Tags: redis, kafka, database, nosql
Published: 2021-06-23 16:17:58

[RedisTimeSeries](https://oss.redislabs.com/redistimeseries/) is a Redis Module that brings native Time Series data structure to Redis. Time Series solutions which were earlier built on top of Sorted Sets (or Redis Streams) can benefit from `RedisTimeSeries` features such as high volume inserts, low latency reads, flexible query language, down-sampling and much more! 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nctg9q63sbg8xclx7x4.jpeg)

Generally speaking, Time Series data is (relatively) simple. Having said that, we need to factor in other characteristics as well:

- Data velocity: For e.g. think hundreds of metrics from thousands of devices per second
- Volume (Big data): Think data accumulation over months (even years)

Thus, Time Series databases such as `RedisTimeSeries` are just a part of the overall solution. You also need to think about how to *collect* (ingest), *process* and *send* all your data to `RedisTimeSeries`. What you really need is a scalable Data Pipeline that can act as a buffer to decouple producers, consumers.

That's where [Apache Kafka](https://kafka.apache.org/) comes in! In addition to the core broker, it has a rich ecosystem of components, including [Kafka Connect](https://kafka.apache.org/documentation/#connect) (which is a part of the solution architecture presented in this blog post), client libraries in multiple languages, [Kafka Streams](https://kafka.apache.org/documentation/streams/), Mirror Maker etc.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qu8rr9j6bju2lrebnbqq.png)

This blog post provides a practical example of how to use `RedisTimeSeries` with Apache Kafka for analyzing time series data. 

> GitHub repo - https://github.com/abhirockzz/redis-timeseries-kafka

Let's start off by exploring the use case first - please note that it has been kept simple for the purposes of the blog post, but the subsequent sections

## Device monitoring

Imagine there are many locations and each of them has multiple devices and you're tasked with the responsibility to monitor device metrics - for now we will consider `temperature` and `pressure`. We will store these metrics in `RedisTimeSeries` (of course!) and use the following naming convention for the keys - `<metric name>:<location>:<device>`. For e.g. temperature for device `1` in location `5` will be represented as `temp:5:1`. each time series data point will also have the following labels (metadata) - `metric`, `location`, `device`. This is to allow for flexible querying as you will see later in the upcoming sections.

Here are a couple of examples to give you an idea of how you would add data points using the `TS.ADD` command:

```bash
# temperature for device 2 in location 3 along with labels
TS.ADD temp:3:2 * 20 LABELS metric temp location 3 device 2

# pressure for device 2 in location 3
TS.ADD pressure:3:2 * 60 LABELS metric pressure location 3 device 2`
```

## Solution Architecture

Here is what the solution looks like at a high level:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8o81jaitl7uhunnlpg52.png)

To summarise the end-to-end flow:

Simulated device data is sent to a local MQTT broker. this data is picked up by the MQTT Kafka connect source connector and sent to a confluent cloud Kafka cluster in Azure. it is processed by a spring application in Azure Spring Cloud which is finally sent to a Redis database in Azure.

It's time to dive in! Before that, make sure you have the following:

## Pre-reqs

- An Azure account - [you can get one for free here](https://azure.microsoft.com/free/?WT.mc_id=data-17927-abhishgu)
- Install [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?WT.mc_id=data-17927-abhishgu)
- JDK 11 for e.g. [OpenJDK](https://openjdk.java.net/projects/jdk/11/])
- A recent version of [Maven](https://maven.apache.org/download.cgi) and [Git](https://git-scm.com/downloads)

## Set up the infrastructure components

Follow the documentation to [provision Azure Cache for Redis (Enterprise Tier)](https://docs.microsoft.com/azure/azure-cache-for-redis/quickstart-create-redis-enterprise?WT.mc_id=data-17927-abhishgu) which comes with the `RedisTimeSeries` module.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyead6p0cyckht5smzb8.png)

Provision [Confluent Cloud cluster on Azure Marketplace](https://docs.microsoft.com/azure/partner-solutions/apache-kafka-confluent-cloud/create?WT.mc_id=data-17927-abhishgu) and also create a Kafka topic (e.g. `mqtt.device-stats`)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zn292lz6v7pzyw1kfvp6.png)

You can provision an instance of Azure Spring Cloud [using the Azure portal](https://docs.microsoft.com/azure/spring-cloud/quickstart-provision-service-instance?tabs=Azure-portal&pivots=programming-language-java&WT.mc_id=data-17927-abhishgu#provision-an-instance-of-azure-spring-cloud-1) or [use the Azure CLI](https://docs.microsoft.com/cli/azure/ext/spring-cloud/spring-cloud?view=azure-cli-latest&WT.mc_id=data-17927-abhishgu#ext_spring_cloud_az_spring_cloud_create)

```bash
az spring-cloud create -n <name of Azure Spring Cloud service> -g <resource group name> -l <enter location e.g southeastasia>
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qy5dhus2u43yv43g1c6q.png)

Before moving on, make sure to clone the GitHub repo:

```bash
git clone https://github.com/abhirockzz/redis-timeseries-kafka
cd redis-timeseries-kafka
```

## Setup local services

The components include:

- [Mosquitto](https://mosquitto.org/) MQTT broker
- [Grafana](https://grafana.com/) for tracking time series data in dashboard
- Kafka Connect with the [MQTT source connector](https://docs.confluent.io/kafka-connect-mqtt/current/mqtt-source-connector/index.html)

### MQTT broker

I installed and started the `mosquitto` broker locally on Mac.

```bash
brew install mosquitto
brew services start mosquitto
```

You can [follow steps corresponding your OS](https://mosquitto.org/download/]) or feel free to use the [this Docker image](https://hub.docker.com/_/eclipse-mosquitto).

### Grafana

I installed and started Grafana locally on Mac.

```bash
brew install grafana
brew services start grafana
```

You can do the same for your OS or feel free to use this [Docker image](https://hub.docker.com/r/grafana/grafana).

```bash
docker run -d -p 3000:3000 --name=grafana -e "GF_INSTALL_PLUGINS=redis-datasource" grafana/grafana
```

### Kafka Connect

> You should be able to find the `connect-distributed.properties` file in the repo that you just cloned. Replace the values for properties such as `bootstrap.servers`, `sasl.jaas.config` etc.

First, [download and unzip Apache Kafka](https://kafka.apache.org/downloads) locally.

**Start a local Kafka Connect cluster**

```bash
export KAFKA_INSTALL_DIR=<kafka installation directory e.g. /home/foo/kafka_2.12-2.5.0>
$KAFKA_INSTALL_DIR/bin/connect-distributed.sh connect-distributed.properties
```

To [install MQTT source connector manually](https://www.confluent.io/hub/confluentinc/kafka-connect-mqtt):

- Download the connector/plugin ZIP file [from this link](https://www.confluent.io/hub/confluentinc/kafka-connect-mqtt), and,
- Extract it into one of the directories that is listed on the Connect worker's `plugin.path` configuration properties

> If you're using Confluent Platform locally, simply use the CLI: `confluent-hub install confluentinc/kafka-connect-mqtt:latest`

**Create MQTT source connector instance**

Make sure to check the `mqtt-source-config.json` file: make sure you enter the right topic name for `kafka.topic` and leave the `mqtt.topics` unchanged.

```bash
curl -X POST -H 'Content-Type: application/json' http://localhost:8083/connectors -d @mqtt-source-config.json

# wait for a minute before checking the connector status
curl http://localhost:8083/connectors/mqtt-source/status
```

## Deploy the device data processor app

Build the application JAR file:

```bash
cd consumer
export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home

mvn clean package
```

Create an Azure Spring Cloud application and deploy the JAR file:

```bash
az spring-cloud app create -n device-data-processor -s <name of Azure Spring Cloud instance> -g <name of resource group> --runtime-version Java_11

az spring-cloud app deploy -n device-data-processor -s <name of Azure Spring Cloud instance> -g <name of resource group> --jar-path target/device-data-processor-0.0.1-SNAPSHOT.jar
```

## Start simulated device data generator

Use script to send data to local MQTT broker. You can use the script in the GitHub repo you just cloned:

```bash
./gen-timeseries-data.sh
```

> All it does is use the `mosquitto_pub` CLI command to send data

Data is sent to the `device-stats` MQTT topic (this is *not* the Kafka topic). You can double check by using the CLI subscriber:

```bash
mosquitto_sub -h localhost -t device-stats
```

**To validate the end-to-end pipeline**

Check the Kafka topic in the Confluent Cloud portal. You should also check the logs for the device data processor app in Azure Spring Cloud:

```bash
az spring-cloud app logs -f -n device-data-processor -s <name of Azure Spring Cloud instance> -g <name of resource group>
```

## Enjoy Grafana dashboards!

Browse to the Grafana UI at `localhost:3000`.

The Redis Data Source plugin for Grafana works with any Redis database, including Azure Cache for Redis. Follow the [instructions in this blog post](https://abhishek1987.medium.com/an-easy-to-use-monitoring-solution-for-redis-5a8a73d56129) to configure a data source.

![](https://miro.medium.com/max/1750/0*VcY0fkq8cd-9bozc.png)

Import the dashboards in the `grafana_dashboards` folder in the GitHub repo you had cloned.

> Refer to the [Grafana documentation](https://grafana.com/docs/grafana/latest/dashboards/export-import/#importing-a-dashboard) if you need assistance on how to import dashboards.

For instance here is a dashboard that shows the average pressure (over 30 seconds) for device 5 in location 1 (uses `TS.MRANGE`)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jasrfzqwsjuaqmt57sc.png)

Here is another dashboard, that shows the maximum temperature (over 15 seconds) for multiple devices in location 3 (again, thanks to `TS.MRANGE`).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shraogkbvtnzh3cmpdvu.png)

## So, you want to run some RedisTimeSeries commands!

Crank up the `redis-cli` and connect to the Azure Cache for Redis instance:

```bash
redis-cli -h <azure redis hostname e.g. redisdb.southeastasia.redisenterprise.cache.azure.net> -p 10000 -a <azure redis access key> --tls
```

Start with simple queries:

```bash
# pressure in device 5 for location 1
TS.GET pressure:1:5

# temperature in device 5 for location 4
TS.GET temp:4:5
```

Filter by location and get temperature and pressure for *all* devices:

```bash
TS.MGET WITHLABELS FILTER location=3
```

Extract temp and pressure for all devices in one or more locations within a specific time range:

```bash
TS.MRANGE - + WITHLABELS FILTER location=3
TS.MRANGE - + WITHLABELS FILTER location=(3,5)
```

> `- +` refers to everything from beginning up until the latest timestamp, but you could be more specific

`MRANGE` is what we need! We can get back multiple time series and use filter.

We can also filter by a specific device in a location and further drill down by either temperature or pressure:

```bash
TS.MRANGE - + WITHLABELS FILTER location=3 device=2
TS.MRANGE - + WITHLABELS FILTER location=3 device=2 metric=temp
```

All these can be combined with aggregations.

```bash
TS.MRANGE - + WITHLABELS FILTER location=3 metric=temp

# all the temp data points are not useful. how about an average (or max) instead of every temp data points?
TS.MRANGE - + WITHLABELS AGGREGATION avg 10000 FILTER location=3 metric=temp
TS.MRANGE - + WITHLABELS AGGREGATION max 10000 FILTER location=3 metric=temp
```

> It's also possible to create a rule to do this aggregation and store in a different time series

Once you're done, don't forget to delete resources to avoid unwanted costs.

## Delete resources

Delete resources:

- [Follow the steps in the documentation](https://docs.microsoft.com/azure/partner-solutions/apache-kafka-confluent-cloud/manage?WT.mc_id=data-17927-abhishgu#delete-confluent-organization) to delete the Confluent Cloud cluster - all you need is to delete the Confluent organisation.
- Similarly, you should [delete the Azure Cache for Redis instance](https://docs.microsoft.com/azure/azure-cache-for-redis/cache-go-get-started?WT.mc_id=data-17927-abhishgu#clean-up-resources) as well.

On your local machine:

- Stop the Kafka Connect cluster
- Stop the mosquitto broker (e.g. `brew services stop mosquitto`)
- Stop Grafana service (e.g. `brew services stop grafana`)

We explored a data pipeline to ingest, process and query time series data using Redis and Kafka. When you think about next steps and move towards a production grade solution, you should consider a few more things.

## Additional considerations

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ln5p024p1pinr20p4ymy.png)

**Optimising `RedisTimeSeries`**

- [Retention policy](https://oss.redislabs.com/redistimeseries/configuration/#retention_policy): Think about this since your time series data points *do not* get trimmed/deleted by default.
- Down-sampling/Aggregations [Rules](https://oss.redislabs.com/redistimeseries/commands/#aggregation-compaction-downsampling): You don't want to store data forever, right? Make sure to configure appropriate rules to take care of this (e.g. `TS.CREATERULE temp:1:2 temp:avg:30 AGGREGATION avg 30000`)
- Duplicate data policy: How would you like to handle duplicate samples? Make sure that the default policy (`BLOCK`) is indeed what you need. If not, consider [other options](https://oss.redislabs.com/redistimeseries/configuration/#duplicate_policy).

> This is not an exhaustive list. For other configuration options, please refer to the [`RedisTimeSeries` documentation](https://oss.redislabs.com/redistimeseries/configuration/)

**What about long term data retention?**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5otot0lo3g0oenash8b.png)

Data is precious, including time series! You may want to process it further e.g. run Machine learning to extract insights, predictive maintenance etc. For this to be possible, you will need to retain this data for longer time frame and for this to be cost-effective and efficient, you would want to use a scalable Object storage service such [Azure Data Lake Storage Gen2](https://docs.microsoft.com/azure/storage/blobs/data-lake-storage-introduction?WT.mc_id=data-17927-abhishgu) (ADLS Gen2).

There is a connector for that! You could enhance you existing data pipeline by using the fully-managed [Azure Data Lake Storage Gen2 Sink Connector for Confluent Cloud](https://docs.confluent.io/cloud/current/connectors/cc-azure-datalakeGen2-storage-sink.html) to process and store the data in ADLS and then run machine learning using [Azure Synapse Analytics](https://docs.microsoft.com/azure/synapse-analytics/spark/apache-spark-machine-learning-mllib-notebook?WT.mc_id=data-17927-abhishgu#predictive-analysis-example-on-nyc-taxi-data) or [Azure Databricks](https://docs.microsoft.com/azure/databricks/applications/machine-learning/?WT.mc_id=data-17927-abhishgu).

**Scalability**

Your time series data volumes can only move one way – up! It's critical for your solution to be scalable from variety of angles:

- Core infrastructure: Managed services allow teams to focus on the solution rather than setting up and maintaining infrastructure, specially when it comes to complex distributed systems such as databases and streaming platforms such as Redis and Kafka.
- Kafka Connect: As far as the data pipeline is concerned, you're in good hands since Kafka Connect platform is inherently stateless and horizontally scalable. You've a lot of options in terms of how you want to architect and size your Kafka Connect worker clusters.
- Custom applications: As was the case in this solution, we built a custom application to process data in Kafka topics. Fortunately, the same scalability characteristics apply to them as well. In terms of horizontal scale - it is limited only by number of Kafka topic partitions you have.

> Integration: It's not just Grafana! `RedisTimeSeries` also integrates with [Prometheus](https://github.com/RedisTimeSeries/prometheus-redistimeseries-adapter) and [Telegraf](https://github.com/RedisTimeSeries/telegraf). However, there is no Kafka connector at the time this blog post was written - this would a great add-on!

## Conclusion

Sure, you can use Redis for (almost) everything, including time series workloads! Be sure to think about the end-to-end architecture for data pipeline and integration from time series data sources, all the way to Redis and beyond.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdup1h1m343d5m4h3f7i.png)