Telemetry ingestion#

Telemetry ingestion moves measurements from where they’re made to where they’re stored, as they’re made. This page covers the steps between the two, and how batching trades delay for fewer requests.

Download this page as a notebook to run its code in JupyterLab. Running the notebooks explains how to set it up.

From source to store#

A telemetry pipeline has four steps:

  1. Read a value from its source: a bus, an instrument or a DAQ. Communications covers the links.

  2. Timestamp it, ideally where it was measured. A value timestamped on arrival carries the link’s delay with it.

  3. Batch it with other values, because sending one request for each value is slow.

  4. Send the batch over the network to the store.

Each step can fall behind. Network performance covers what happens when a step falls behind.

Batching#

The Nominal Python client doesn’t send each value as you write it. It collects values into batches, and sends a batch when it reaches batch_size points or when it’s max_wait old. The defaults are 250,000 points and 0.25 s.[1] Batching has two effects: a larger batch means fewer requests for the same data, and a longer wait means a value can arrive later.

This code works out both for two streams, from the batching rule above. A stream of 20 channels at 1 kHz is 20,000 points a second. One of 200 channels at 10 kHz is 2 million points a second:

import pandas as pd

batch_size = 250_000  # points: the client's default


def batching(points_per_s, max_wait):
    """A batch is sent when it fills, or when it's max_wait old: whichever comes first."""
    seconds_to_fill = batch_size / points_per_s
    interval = min(max_wait, seconds_to_fill)  # s between requests
    return {
        "points per second": points_per_s,
        "max_wait (s)": max_wait,
        "requests per second": 1 / interval,
        "points per request": points_per_s * interval,
        "longest wait for a value (s)": interval,
    }


pd.DataFrame(
    [
        batching(rate, wait)
        for rate in (20_000, 2_000_000)
        for wait in (0.05, 0.25, 1.0)
    ]
).round(2)
points per second max_wait (s) requests per second points per request longest wait for a value (s)
0 20000 0.05 20.0 1000.0 0.05
1 20000 0.25 4.0 5000.0 0.25
2 20000 1.00 1.0 20000.0 1.00
3 2000000 0.05 20.0 100000.0 0.05
4 2000000 0.25 8.0 250000.0 0.12
5 2000000 1.00 8.0 250000.0 0.12

For the slower stream, no batch ever fills, so max_wait alone sets the request rate and the delay. For the faster stream, batches fill first, so each value waits for the batch to fill, and max_wait only matters when the stream slows. To get values sooner, use a smaller max_wait, and accept more and smaller requests.

When the network fails#

The Nominal Python client streams through nominal-streaming, a library designed to hold data in memory only for a limited time, to write to disk if the network fails, and to apply backpressure when the network’s throughput is saturated.[2] To keep data through an outage, pass file_fallback, the path to an .avro file, and the client writes batches that fail to upload to that file.[1]

In Nominal#

This code streams a source into a dataset, with a shorter max_wait and a fallback file:

from datetime import datetime, timedelta, timezone

from nominal.core import NominalClient

client = NominalClient.from_profile("default")
dataset = client.get_dataset("<dataset RID>")

with dataset.get_write_stream(
    max_wait=timedelta(seconds=0.1),  # send each batch within 0.1 s
    file_fallback="unsent.avro",  # keep batches that fail to upload
) as stream:
    for channel, value in read_source():  # your source
        stream.enqueue(channel, datetime.now(timezone.utc), value)

Writing a value queues it, and doesn’t upload it. If uploads fall behind, a write can block until there’s room, so a slow network can slow the program that writes the data.[3] Keep these writes out of any loop that has to keep time. Network performance explains why.

Further reading#

References#