When to Use Flux vs Python

Navigate to:

(Update: InfluxDB 3.0 moved away from Flux and a built-in task engine. Users can use external tools, like Python-based Quix, to create tasks in InfluxDB 3.0.)

If you’re new to InfluxDB you might wonder, “Why does InfluxDB have its own query and scripting language (aka Flux)?” You might also be thinking, “InfluxDB has client libraries. Why and when should I use the Python client library and when should I use Flux?” In this post we’ll discuss when developers should use Flux and when they should use Python for developing their IoT applications.

The advantages of using Python with InfluxDB

If you’re a Python developer, this section might be obvious to you. Python is a popular language, so you’ll have plenty of resources, support, and contribution opportunities with any time series application or solution you build on top of InfluxDB. Additionally, Python has over 137,000 Python libraries that you can leverage to accomplish a variety of time series tasks like data preparation, cleaning, analysis, forecasting, classification, anomaly detection, etc.

I recommend using Python if you’re looking to perform the following time series tasks:

  • Complex data processing

  • Forecasting

  • Anomaly Detection

When it comes to the sheer volume and sophistication of available tools, Python has no competition. In these situations, Python is the obvious choice compared to Flux. You’ll want to think of InfluxDB as your time series data lake, query data out with the InfluxDB Python client library, perform your data analysis or machine learning tasks with Python, and write the data back out to InfluxDB.

For a collection of examples of this type of work, I encourage you to check out the following repo. It includes examples of how to use the following popular machine learning, data transformation, and data analysis Python libraries in conjunction with the Python Client Library:

  • Pandas

  • Tensorflow

  • Keras

  • ADTK

  • Prophet

  • Kats

  • ScikitLearn

  • And more!

If you’re looking to build a time series application on top of InfluxDB, I recommend using Python for that as well. In fact, the Developer Relations team at InfluxData did just that with the Plant Buddy demo app.

Plant Buddy IoT Demo App - Chart

As this architecture drawing of Plant Buddy shows, it’s a Python Flask IoT demo application that monitors the health of plants. It uses InfluxDB as the storage backend for the time series data.

This demo app helps users monitor their house plants and live up to their best plant parenting potential. Plant Buddy uses InfluxDB to store time series data, like humidity and temperature, about the user’s plants. It uses the Python client library to query the data from InfluxDB. I encourage you to try out this application for yourself. Check out the repo here.

Another advantage to using Python to build your IoT application with InfluxDB as the storage backend is that you can use MicroPython on your microcontrollers at the edge and maintain language consistency. However, I recommend using Flux for time series data management and alerting within the InfluxDB ecosystem.

The advantages of using Flux with InfluxDB

Flux is the functional query and scripting language for InfluxDB. We developed it to provide InfluxDB users with more functionality than its predecessor, InfluxQL. InfluxQL is an SQL-like language that allows users to perform basic queries. Flux allows you to build data pipelines to query, analyze, transform and action on your data. Flux also processes the data server-side, which provides better performance over using a client library.

Another benefit of Flux running at a database level is that it allows us to perform these tasks without requiring a third party application to do it. If you’re an InfluxDB Cloud user that means you don’t have to worry about hosting, maintaining, or scaling those tasks either. To summarize, Flux is an all-in-one solution used across the entire platform that allows you to:

  • Query for your time series data.

  • Write times series data to InfluxDB.

  • Manage your time series data lifecycle.

  • Alert on your data.

  • Analyze, clean, and transform your data.

The InfluxDB processing engine runs tasks. Tasks are Flux scripts executed on a user-defined schedule. In fact, we use Flux to process and downsample the plant sensor data at the edge for the Plant Buddy demo application. Then we use Edge Data Replication, an InfluxDB feature for replicating data to other OSS instances, to consolidate that data to a cloud instance of InfluxDB.

While you don’t have access to all of the thousands of packages and functions for time series analysis that Python provides, you’re still left with considerable power when it comes to fundamental time series analysis. Flux has the following functions and packages:

  • Transformations Functions for Statistical Time Series Analysis

  • Covariance, standard deviation, quantile, spread, skew, etc.

  • Transformations Functions for Dynamic Statistical and Fundamental Time Series Analysis

  • Derivative, moving averages, double and triple exponential smoothing, etc.

  • Technical Momentum Indicators for Financial Analysis

  • Chande momentum oscillator, Kaufman’s adaptive moving average, etc.

  • Functions for working with and manipulating timestamps:

  • Truncating time, shifting time, working with timezones, hour selection, adding/subtracting durations, etc.

Flux also has the following packages, which enable users to manage and process their time series data:

  • Math package for math applications.

  • Geopackage for geotemporal data.

  • SQL package to query and write data to and from SQL databases.

  • HTTP package for making get and post requests. You can fully manage your InfluxDB instances with Flux, the HTTP package, and the InfluxDB API.

  • JSON package for working with and parsing json.

Conclusion

I hope this blog post inspires you to take advantage of both the Python client library and Flux when it comes to building IoT applications on top of InfluxDB. If you’re looking to tackle a problem in either language, I encourage you to reach out using our community site or Slack channel. I’d love to hear about what you’re trying to achieve and what features you’d like the task system in InfluxDB to have.

Finally, If you’re developing a cool IoT application on top of InfluxDB, we’d love to hear about it, so make sure to share it on social using #InfluxDB! Additionally, feel free to reach out to me directly in our community Slack channel to share your thoughts, concerns, or questions. I’d love to get your feedback and help you with any problems you run into!