Latency

Latency is an essential metric to monitor in real-time applications.

This page explains how latency is measured in Tinybird, and how to monitor and visualize the latency of your API Endpoints when data is being retrieved.

What is latency?

Latency is the time it takes for a request to travel from the client to the server and back; the time it takes for a request to be sent and received. Latency is usually measured in seconds or milliseconds (ms). The lower the latency, the faster the response time.

How latency is measured

When measuring latency in an end-to-end application, you consider data ingestion, data transformation, and data retrieval. In Tinybird, thanks to the exceptional speed of ClickHouse ingestion and real-time Materialized Views, the data freshness is guaranteed.

Putting this all together: In Tinybird, latency is measured as the time it takes for a request to be sent, processed in ClickHouse (very fast), and the response to be sent back to the client. When calling an API Endpoint, you can check this metric defined as elapsed in the statistics object of the response:

Statistics object within an example Tinybird API Endpoint call
{
  "meta": [ ... ],
  "data": [ ... ],
  "rows": 10,
  "statistics": {
    "elapsed": 0.001706275,
    "rows_read": 10,
    "bytes_read": 180
  }
}

How to monitor latency

To monitor the latency of your API Endpoints, use the pipe_stats_rt and pipe_stats Service Data Sources:

  • pipe_stats_rt consists of the real-time statistics of your API Endpoints, and has a duration field that encapsulates the latency time in seconds.
  • pipe_stats contains the aggregated statistics of your API Endpoints by date, and presents a avg_duration_state field which is the average duration of the API Endpoint by day in seconds.

Because the avg_duration_state field is an intermediate state, you'd need to merge it when querying the Data Source using something like avgMerge.

More for details on building Pipes and Endpoints that monitor the performance of your API Endpoints using the pipe_stats_rt and pipe_stats Data Sources, follow the API Endpoint performance guide.

How to visualize latency

Visualizing the latency of your API Endpoints can make it easier to see the at-a-glance overview. Tinybird has built-in tools to help you do this: Time Series is a quick option for output that lives internally in your Workspace, and Charts give you the option to embed in an external application.

Time Series

In your Workspace, you can create a Time Series to visualize the latency of your API Endpoints over time. You just need to point to pipe_stats_rt and select duration and start_datetime, or point to or pipe_stats and select avgMerge(avg_duration_state) and date.

Example Tinybird Time Series showing pipe_stats_analysis

Charts

If you want to expose your latency metrics in your own application, you can use a Tinybird-generated Chart to expose the results of an Endpoint that queries the pipe_stats_rt or pipe_stats Data Source. Then, you can embed the Chart into your application by using the iframe code.

Next steps