Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Influxdb | 25,112 | 632 | 1,037 | 5 days ago | 639 | August 18, 2022 | 1,728 | mit | Go | |
Scalable datastore for metrics, events, and real-time analytics | ||||||||||
Tdengine | 21,065 | 1 | 14 hours ago | 12 | April 14, 2022 | 873 | agpl-3.0 | C | ||
TDengine is an open source, high-performance, cloud native time-series database optimized for Internet of Things (IoT), Connected Cars, Industrial IoT and DevOps. | ||||||||||
Victoriametrics | 8,213 | 4 | 20 hours ago | 174 | September 08, 2022 | 633 | apache-2.0 | Go | ||
VictoriaMetrics: fast, cost-effective monitoring solution and time series database | ||||||||||
Lindb | 2,602 | 6 days ago | 10 | July 31, 2019 | 7 | apache-2.0 | Go | |||
LinDB is a scalable, high performance, high availability distributed time series database. | ||||||||||
Oban | 2,521 | 2 | 10 | 3 days ago | 61 | September 23, 2022 | 8 | apache-2.0 | Elixir | |
💎 Robust job processing in Elixir, backed by modern PostgreSQL or SQLite3 | ||||||||||
Kairosdb | 1,689 | 15 days ago | 124 | apache-2.0 | Java | |||||
Fast scalable time series database | ||||||||||
Pgwatch2 | 1,487 | 4 days ago | 35 | April 25, 2022 | 2 | bsd-3-clause | PLpgSQL | |||
PostgreSQL metrics monitor/dashboard | ||||||||||
Filodb | 1,374 | 10 days ago | 27 | apache-2.0 | Scala | |||||
Distributed Prometheus time series database | ||||||||||
Whisper | 1,189 | a month ago | 5 | March 12, 2019 | 2 | apache-2.0 | Python | |||
Whisper is a file-based time-series database format for Graphite. | ||||||||||
Pome | 1,083 | 3 years ago | February 27, 2016 | 17 | apache-2.0 | Go | ||||
A Postgres Metrics Dashboard |
tstorage
is a lightweight local on-disk storage engine for time-series data with a straightforward API.
Especially ingestion is massively optimized as it provides goroutine safe capabilities of write into and read from TSDB that partitions data points by time.
I'm working on a couple of tools that handle a tremendous amount of time-series data, such as Ali and Gosivy. Especially Ali, I had been facing a problem of increasing heap consumption over time as it's a load testing tool that aims to perform real-time analysis. I little poked around a fast TSDB library that offers simple APIs but eventually nothing works as well as I'd like, that's why I settled on writing this package myself.
To see how much tstorage
has helped improve Ali's performance, see the release notes here.
Currently, tstorage
requires Go version 1.16 or greater
By default, tstorage.Storage
works as an in-memory database.
The below example illustrates how to insert a row into the memory and immediately select it.
package main
import (
"fmt"
"github.com/nakabonne/tstorage"
)
func main() {
storage, _ := tstorage.NewStorage(
tstorage.WithTimestampPrecision(tstorage.Seconds),
)
defer storage.Close()
_ = storage.InsertRows([]tstorage.Row{
{
Metric: "metric1",
DataPoint: tstorage.DataPoint{Timestamp: 1600000000, Value: 0.1},
},
})
points, _ := storage.Select("metric1", nil, 1600000000, 1600000001)
for _, p := range points {
fmt.Printf("timestamp: %v, value: %v\n", p.Timestamp, p.Value)
// => timestamp: 1600000000, value: 0.1
}
}
To make time-series data persistent on disk, specify the path to directory that stores time-series data through WithDataPath option.
storage, _ := tstorage.NewStorage(
tstorage.WithDataPath("./data"),
)
defer storage.Close()
In tstorage, you can identify a metric with combination of metric name and optional labels. Here is an example of insertion a labeled metric to the disk.
metric := "mem_alloc_bytes"
labels := []tstorage.Label{
{Name: "host", Value: "host-1"},
}
_ = storage.InsertRows([]tstorage.Row{
{
Metric: metric,
Labels: labels,
DataPoint: tstorage.DataPoint{Timestamp: 1600000000, Value: 0.1},
},
})
points, _ := storage.Select(metric, labels, 1600000000, 1600000001)
For more examples see the documentation.
Benchmark tests were made using Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz with 16GB of RAM on macOS 10.15.7
$ go version
go version go1.16.2 darwin/amd64
$ go test -benchtime=4s -benchmem -bench=. .
goos: darwin
goarch: amd64
pkg: github.com/nakabonne/tstorage
cpu: Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
BenchmarkStorage_InsertRows-8 14135685 305.9 ns/op 174 B/op 2 allocs/op
BenchmarkStorage_SelectAmongThousandPoints-8 20548806 222.4 ns/op 56 B/op 2 allocs/op
BenchmarkStorage_SelectAmongMillionPoints-8 16185709 292.2 ns/op 56 B/op 1 allocs/op
PASS
ok github.com/nakabonne/tstorage 16.501s
Time-series database has specific characteristics in its workload. In terms of write operations, a time-series database has to ingest a tremendous amount of data points ordered by time. Time-series data is immutable, mostly an append-only workload with delete operations performed in batches on less recent data. In terms of read operations, in most cases, we want to retrieve multiple data points by specifying its time range, also, most recent first: query the recent data in real-time. Besides, time-series data is already indexed in time order.
Based on these characteristics, tstorage
adopts a linear data model structure that partitions data points by time, totally different from the B-trees or LSM trees based storage engines.
Each partition acts as a fully independent database containing all data points for its time range.
Read Write
V
max: 1600010800
> Memory Partition
min: 1600007201
max: 1600007200
> Memory Partition
min: 1600003601
max: 1600003600
> Disk Partition
min: 1600000000
Key benefits:
The memory partition is writable and stores data points in heap. The head partition is always memory partition. Its next one is also memory partition to accept out-of-order data points. It stores data points in an ordered Slice, which offers excellent cache hit ratio compared to linked lists unless it gets updated way too often (like delete, add elements at random locations).
All incoming data is written to a write-ahead log (WAL) right before inserting into a memory partition to prevent data loss.
The old memory partitions get compacted and persisted to the directory prefixed with p-
, under the directory specified with the WithDataPath option.
Here is the macro layout of disk partitions:
$ tree ./data
./data
p-1600000001-1600003600
data
meta.json
p-1600003601-1600007200
data
meta.json
p-1600007201-1600010800
data
meta.json
As you can see each partition holds two files: meta.json
and data
.
The data
is compressed, read-only and is memory-mapped with mmap(2) that maps a kernel address space to a user address space.
Therefore, what it has to store in heap is only partition's metadata. Just looking at meta.json
gives us a good picture of what it stores:
$ cat ./data/p-1600000001-1600003600/meta.json
{
"minTimestamp": 1600000001,
"maxTimestamp": 1600003600,
"numDataPoints": 7200,
"metrics": {
"metric-1": {
"name": "metric-1",
"offset": 0,
"minTimestamp": 1600000001,
"maxTimestamp": 1600003600,
"numDataPoints": 3600
},
"metric-2": {
"name": "metric-2",
"offset": 36014,
"minTimestamp": 1600000001,
"maxTimestamp": 1600003600,
"numDataPoints": 3600
}
}
}
Each metric has its own file offset of the beginning. Data point slice for each metric is compressed separately, so all we have to do when reading is to seek, and read the points off.
What data points get out-of-order in real-world applications is not uncommon because of network latency or clock synchronization issues; tstorage
basically doesn't discard them.
If out-of-order data points are within the range of the head memory partition, they get temporarily buffered and merged at flush time.
Sometimes we should handle data points that cross a partition boundary. That is the reason why tstorage
keeps more than one partition writable.
Want to know more details on tstorage internal? If so see the blog post: Write a time-series database engine from scratch.
This package is implemented based on tons of existing ideas. What I especially got inspired by are:
A big "thank you!" goes out to all of them.