Manage subscriptions in InfluxDB

InfluxDB subscriptions are local or remote endpoints to which all data written to InfluxDB is copied. Subscriptions are primarily used with Kapacitor, but any endpoint able to accept UDP, HTTP, or HTTPS connections can subscribe to InfluxDB and receive a copy of all data as it is written.

How subscriptions work

As data is written to InfluxDB, writes are duplicated to subscriber endpoints via HTTP, HTTPS, or UDP in line protocol. the InfluxDB subscriber service creates multiple “writers” (goroutines) which send writes to the subscription endpoints.

The number of writer goroutines is defined by the write-concurrency configuration.

As writes occur in InfluxDB, each subscription writer sends the written data to the specified subscription endpoints. However, with a high write-concurrency (multiple writers) and a high ingest rate, nanosecond differences in writer processes and the transport layer can result in writes being received out of order.

Important information about high write loads

While setting the subscriber write-concurrency to greater than 1 does increase your subscriber write throughput, it can result in out-of-order writes under high ingest rates. Setting write-concurrency to 1 ensures writes are passed to subscriber endpoints sequentially, but can create a bottleneck under high ingest rates.

What write-concurrency should be set to depends on your specific workload and need for in-order writes to your subscription endpoint.

InfluxQL subscription statements

Use the following InfluxQL statements to manage subscriptions:

CREATE SUBSCRIPTION
SHOW SUBSCRIPTIONS
DROP SUBSCRIPTION

Create subscriptions

Create subscriptions using the CREATE SUBSCRIPTION InfluxQL statement. Specify the subscription name, the database name and retention policy to subscribe to, and the URL of the host to which data written to InfluxDB should be copied.

  1. -- Pattern:
  2. CREATE SUBSCRIPTION "<subscription_name>" ON "<db_name>"."<retention_policy>" DESTINATIONS <ALL|ANY> "<subscription_endpoint_host>"
  3. -- Examples:
  4. -- Create a SUBSCRIPTION on database 'mydb' and retention policy 'autogen' that sends data to 'example.com:9090' via HTTP.
  5. CREATE SUBSCRIPTION "sub0" ON "mydb"."autogen" DESTINATIONS ALL 'http://example.com:9090'
  6. -- Create a SUBSCRIPTION on database 'mydb' and retention policy 'autogen' that round-robins the data to 'h1.example.com:9090' and 'h2.example.com:9090' via UDP.
  7. CREATE SUBSCRIPTION "sub0" ON "mydb"."autogen" DESTINATIONS ANY 'udp://h1.example.com:9090', 'udp://h2.example.com:9090'

In case authentication is enabled on the subscriber host, adapt the URL to contain the credentials.

  1. -- Create a SUBSCRIPTION on database 'mydb' and retention policy 'autogen' that sends data to another InfluxDB on 'example.com:8086' via HTTP. Authentication is enabled on the subscription host (user: subscriber, pass: secret).
  2. CREATE SUBSCRIPTION "sub0" ON "mydb"."autogen" DESTINATIONS ALL 'http://subscriber:secret@example.com:8086'

SHOW SUBSCRIPTIONS outputs all subscriber URL in plain text, including those with authentication credentials. Any user with the privileges to run SHOW SUBSCRIPTIONS is able to see these credentials.

Sending subscription data to multiple hosts

The CREATE SUBSCRIPTION statement allows you to specify multiple hosts as endpoints for the subscription. In your DESTINATIONS clause, you can pass multiple host strings separated by commas. Using ALL or ANY in the DESTINATIONS clause determines how InfluxDB writes data to each endpoint:

ALL: Writes data to all specified hosts.

ANY: Round-robins writes between specified hosts.

Subscriptions with multiple hosts

  1. -- Write all data to multiple hosts
  2. CREATE SUBSCRIPTION "mysub" ON "mydb"."autogen" DESTINATIONS ALL 'http://host1.example.com:9090', 'http://host2.example.com:9090'
  3. -- Round-robin writes between multiple hosts
  4. CREATE SUBSCRIPTION "mysub" ON "mydb"."autogen" DESTINATIONS ANY 'http://host1.example.com:9090', 'http://host2.example.com:9090'

Subscription protocols

Subscriptions can use HTTP, HTTPS, or UDP transport protocols. Which to use is determined by the protocol expected by the subscription endpoint. If creating a Kapacitor subscription, this is defined by the subscription-protocol option in the [[influxdb]] section of your kapacitor.conf.

kapacitor.conf

  1. [[influxdb]]
  2. # ...
  3. subscription-protocol = "http"
  4. # ...

For information regarding HTTPS connections and secure communication between InfluxDB and Kapacitor, view the Kapacitor security documentation.

Show subscriptions

The SHOW SUBSCRIPTIONS InfluxQL statement returns a list of all subscriptions registered in InfluxDB.

  1. SHOW SUBSCRIPTIONS

Example output:

  1. name: _internal
  2. retention_policy name mode destinations
  3. ---------------- ---- ---- ------------
  4. monitor kapacitor-39545771-7b64-4692-ab8f-1796c07f3314 ANY [http://localhost:9092]

Remove subscriptions

Remove or drop subscriptions using the DROP SUBSCRIPTION InfluxQL statement.

  1. -- Pattern:
  2. DROP SUBSCRIPTION "<subscription_name>" ON "<db_name>"."<retention_policy>"
  3. -- Example:
  4. DROP SUBSCRIPTION "sub0" ON "mydb"."autogen"

Drop all subscriptions

In some cases, it may be necessary to remove all subscriptions. Run the following bash script that utilizes the influx CLI, loops through all subscriptions, and removes them. This script depends on the $INFLUXUSER and $INFLUXPASS environment variables. If these are not set, export them as part of the script.

  1. # Environment variable exports:
  2. # Uncomment these if INFLUXUSER and INFLUXPASS are not already globally set.
  3. # export INFLUXUSER=influxdb-username
  4. # export INFLUXPASS=influxdb-password
  5. IFS=$'\n'; for i in $(influx -format csv -username $INFLUXUSER -password $INFLUXPASS -database _internal -execute 'show subscriptions' | tail -n +2 | grep -v name); do influx -format csv -username $INFLUXUSER -password $INFLUXPASS -database _internal -execute "drop subscription \"$(echo "$i" | cut -f 3 -d ',')\" ON \"$(echo "$i" | cut -f 1 -d ',')\".\"$(echo "$i" | cut -f 2 -d ',')\""; done

Configure InfluxDB subscriptions

InfluxDB subscription configuration options are available in the [subscriber] section of the influxdb.conf. In order to use subcriptions, the enabled option in the [subscriber] section must be set to true. Below is an example influxdb.conf subscriber configuration:

  1. [subscriber]
  2. enabled = true
  3. http-timeout = "30s"
  4. insecure-skip-verify = false
  5. ca-certs = ""
  6. write-concurrency = 40
  7. write-buffer-size = 1000

Descriptions of [subscriber] configuration options are available in the Configuring InfluxDB documentation.

Troubleshooting

Inaccessible or decommissioned subscription endpoints

Unless a subscription is dropped, InfluxDB assumes the endpoint should always receive data and will continue to attempt to send data. If an endpoint host is inaccessible or has been decommissioned, you will see errors similar to the following:

  1. # Some message content omitted (...) for the sake of brevity
  2. "Post http://x.y.z.a:9092/write?consistency=...: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)" ... service=subscriber
  3. "Post http://x.y.z.a:9092/write?consistency=...: dial tcp x.y.z.a:9092: getsockopt: connection refused" ... service=subscriber
  4. "Post http://x.y.z.a:9092/write?consistency=...: dial tcp 172.31.36.5:9092: getsockopt: no route to host" ... service=subscriber

In some cases, this may be caused by a networking error or something similar preventing a successful connection to the subscription endpoint. In other cases, it’s because the subscription endpoint no longer exists and the subscription hasn’t been dropped from InfluxDB.

Because InfluxDB does not know if a subscription endpoint will or will not become accessible again, subscriptions are not automatically dropped when an endpoint becomes inaccessible. If a subscription endpoint is removed, you must manually drop the subscription from InfluxDB.