Measure API Endpoint 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.
Latency is an essential metric to monitor in real-time applications. Read on to learn how latency is measured in Tinybird, and how to monitor and visualize the latency of your API Endpoints when data is being retrieved.
How latency is measured¶
When measuring latency in an end-to-end application, you consider data ingestion, data transformation, and data retrieval. In Tinybird, latency is measured as the time it takes for a request to be sent, 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 } }
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 aduration
field that encapsulates the latency time in seconds.pipe_stats
contains the aggregated statistics of your API Endpoints by date, and presents aavg_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
.
Dor 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.
Visualize latency¶
Tinybird has built-in tools to help you visualize the latency of your API Endpoints: Time Series live internally in your Workspace, while 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. 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
.
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¶
- Optimize even further by monitoring your ingestion.
- Read this blog on Monitoring global API latency.