InfluxDB - What, When, Why
Tags: database, javascript, influxdb
Published: 2022-12-09 22:26:17

InfluxDB is an open-source time series database. A time series database is a type of database that is optimized for storing and analyzing time-stamped data. Time-stamped data is data that has a time component, such as a timestamp, associated with it, and is often used to track events or changes over time.

InfluxDB is designed to be highly scalable and performant, and is often used for storing and analyzing large volumes of time-stamped data. It supports a range of data types, such as integers, floating-point numbers, strings, and booleans, and allows users to query and manipulate the data using a SQL-like query language. It is also often used in conjunction with other tools, such as Grafana, to visualize and analyze the data.

## Why should we use InfluxDB ?

There are several reasons why you might want to use InfluxDB, including:

- Scalability and performance. InfluxDB is designed to be highly scalable and performant, which makes it well-suited for storing and analyzing large volumes of time-stamped data. It can handle high-throughput workloads, and provides fast and efficient querying and data manipulation capabilities.
- Flexibility and customization. InfluxDB allows users to define their own data schemas and data types, which allows for greater flexibility and customization. This means that users can tailor the database to the specific needs and requirements of their application or use case.
- Ease of use. InfluxDB has a user-friendly and intuitive interface, which makes it easy to use and learn, even for users with limited technical expertise. It also provides a range of tools and features that make it easy to import, export, and manipulate data, and to perform complex queries and analysis.
- Wide adoption and support. InfluxDB is an open-source project that is widely used and supported by a large and active community. This means that users can access a wealth of documentation, tutorials, and support resources, as well as a range of plugins and integrations with other tools and technologies.

## But when should we not use ?

- If you don't need to store and analyze time-stamped data. InfluxDB is a time series database, which means that it is optimized for storing and analyzing time-stamped data. If you don't have any time-stamped data, or if you don't need to analyze it, then InfluxDB may not be the best choice for your use case.
- If you need a relational database. InfluxDB is a non-relational database, which means that it doesn't support the same data modeling and querying capabilities as a relational database, such as MySQL or PostgreSQL. If you need to store and manipulate data using complex relationships and foreign keys, then a relational database may be a better option.
- If you need full ACID compliance. InfluxDB is a distributed database, which means that it provides eventual consistency, but not full ACID (Atomicity, Consistency, Isolation, Durability) compliance. If you need full ACID compliance, then you may want to consider a different database, such as Apache Cassandra or MongoDB.
- If you have limited resources. InfluxDB is a resource-intensive database, which means that it requires a significant amount of memory, CPU, and storage to operate efficiently. If you have limited resources, or if you need to run the database on a small or low-powered device, then InfluxDB may not be the best choice.

## Code Example

```js
const Influx = require('influx');

// Create an InfluxDB client
const influx = new Influx.InfluxDB({
  host: 'localhost',
  database: 'mydb',
  schema: [
    {
      measurement: 'temperature',
      fields: {
        value: Influx.FieldType.FLOAT
      },
      tags: [
        'location'
      ]
    }
  ]
});

// Write a point to the database
influx.writePoints([
  {
    measurement: 'temperature',
    tags: { location: 'office' },
    fields: { value: 22.1 },
    timestamp: new Date()
  }
]).catch(err => {
  console.error(`Error writing points: ${err.message}`);
});

// Query the database
influx.query(`
  SELECT * FROM temperature
`).then(results => {
  console.log(`Results:`);
  console.log(results);
}).catch(err => {
  console.error(`Error querying the database: ${err.message}`);
});
```

This example shows how you can use the InfluxDB client to connect to a database, write a point to the database, and query the database to retrieve the stored data. It also shows how you can define the schema of the database, which specifies the measurements, fields, and tags that are used to store the data.