Modelling metrics
For doing metrics in SurrealDB you can choose one or combine:
Pre-computed table views
Live Queries
Drop tables
Custom events
Pre-computed table views
Our pre-computed table views are most similar to event-based, incrementally updating, materialised views. Practically, this means our downsampled metrics will always be up to date as it incrementally updates in near real-time when we add more records to the sensor_readings table.
For real-time visualisation of our metrics we can then use Live Queries to stream real-time updates to our client, such as a BI dashboard or embedded analytics code.
Drop tables
Drop tables are pretty unique tables that drop all writes once they have been written. A view can be defined on a table whether it is a DROP table or not, but DROP is recommended if you have no reason to directly query the table that provides the data for the aggregated view.
These tables can be very useful in a time series context if you want to capture very high-frequency data but only care about storing the aggregated downsampled metrics. They are typically used in combination with either the table views or custom events, such that the metrics are calculated then the underlying data is automatically dropped.
When combining drop tables, table views and live queries, you have a very easy-to-set up, event-based and real-time solution from capturing events, creating metrics, dropping stale data and live selects for visualisation.
Custom events
If you have something even more bespoke in mind, you can even create your own event triggers based on when a record is created, updated or deleted. You can include any valid SurrealQL inside the event.
For example, we can create a simple real-time anomaly detection and notification solution using just SurrealQL events and functions in 5 steps.
Define an event to trigger when a record is added to the sensor_readings table.
Get the desired time range you want to track.
Calculate both the upper and lower threshold for an outlier, using the standard Q1 - 1.5 IQR formula for the low outliers and Q3 + 1.5 IQR formula for the high outliers.
Check if the current temperature is a low or high outlier.
Send an http::post request with the outlier details.
Async events
If you need an event to execute in the background out of the main transaction in which they are called, you can use an async event. This can be useful for events that take a certain amount of time to process. The tradeoff is that your read data will not be consistent until the event or events have finished processing. This is what is known as eventual consistency.
SurrealDB vs specialised time series databases
There are many specialised time series databases out there, so where does SurrealDB fit in?
The advantages SurrealDB has over specialised time series databases are:
That you can combine our time series functionality with the rest of our multi-model database features. For example, doing full-text search and vector search on your log data.
No need to learn another query language just for time series. SurrealDB has a unified query language for all its features.
Connect and enrich your metrics easily, instead of having them being siloed in a separate system. You can have all your data in one place with zero ETL for your various use cases. Whether you’re doing transactional, analytical, ML and AI applications, SurrealDB covers a lot of the use cases a modern application needs.
The advantages specialised time series databases have over SurrealDB currently are:
More advanced time series features such as custom data retention policies and better data compression.
Whether you pick SurrealDB for your time series use cases depends mostly on whether you are looking to lower your total system complexity or if you are looking for another specialised solution.
For raw event storage and complex record ID patterns, see IoT and telemetry patterns.