T-Digest Design Proposal #2542
Replies: 3 comments 11 replies
-
Currently the data structures in kvrocks is NOT composable, which means you cannot just construct a Redis list and state that it's a part of your new data structure. All rocksdb keys inside one object (in redis level) should have the same user key and different sub keys. So maybe you need to clearly describe the format of all sub keys in your data structure. |
Beta Was this translation helpful? Give feedback.
-
The design is great. It's the first time I know the t-digest here
Would you mind describe the buffer more specifically? Any I'm curious why buffer has a capacity in kvrocks since it's not a in-memory structure |
Beta Was this translation helpful? Give feedback.
-
Hi @PragmaTwice , @mapleFU , Sorry to bother you again. I have updated the proposal with some improvement. I plan to create a tracking issue for all t-digest commands for further development if the proposal is ready to be implemented. Best Regards, |
Beta Was this translation helpful? Give feedback.
-
Introduction
Redis Stack has supported a new probabilistic data structure t-digest.
In #2423, we plan to support t-digest as well.
Basic about T-Digest
The original paper is Computing Extremely Accurate Quantiles Using t-Digests [1].
Thanks to the blog T-Digest in Python [7] and the slide [8], I have a better understanding of t-digest.
The main idea of t-digest is to divide the data into small bins and store the mean and count of each bin aka centroids.
This action compressed ranges of data into a single centroids with just mean and weight. The mean is the average of the data in the bin and the weight is the number of data compressed in the bin.
This behavior is called as sketch. Sketch is necessary when we need to deal with plenty of data.
We use these centroids with interpolation to estimate the quantile of the data.
We lost some precisions of the original data and get the ability to easily merge them and calculate the quantile.
We use the potential function
k
to control the distribution of the bins.Inside the function we provide a$\delta$ to control the error of the distribution.
metadata
In metadata, we should store the compression$(1/\delta)$ , total_weight, minimum and maximum of t-digest.
For temporary input buffer, we should have a buffer left space for merging trigger.
When one item is pushed,
buffer_free
will be decreased. Whenbuffer_free
is zero, we should merge the buffer to the centroids and reset thebuffer_free
.centroids
Each centroid should be sorted by mean and have a weight. The mean and weight are both double precision numbers.
So, if the we try to keep the order of mean, we should design a way to serialize double and keep its order same before serialization.
Here is one encoding format of duckdb [9] to keep the original order of double and convert to a 8-byte uint64.
Here is the subkey of centroid.
'centroid' is an enum for the centroid subkey.
'mean' is a serialized double with order.
temparory buffer
Temp buffer is just a list of doubles, we should control the limit of whole buffer's size and should acuiqre a lock when try to merge it to the centroids.
Since we have a way to serialize the double to a ordered one, we can use same way to encode the temparory double buffer.
To avoid collisions with same value, we should add an unique identifier for each item. It can be a uint64 millisecond timestamp for simplicity.
'buffer' is a enum for buffer subtype.
'value' is serialized double for data to be inserted with order.
'id' is a unique identifier for collision.
concurrency safety
The pushing to temporary list action should have acquire the lock for updating the metadata.
The merge action should acquire a lock. It includes the buffer merging and merging with other digests.
When we try to calculate the perncetile or CDF, we should merge the buffer and make a snapshot.
Then the calculation should have no lock.
References
[1]Computing Extremely Accurate Quantiles Using t-Digests
[2]https://github.com/tdunning/t-digest
[3]https://issues.apache.org/jira/browse/ARROW-11367
[4]apache/arrow#9310
[5]https://github.com/apache/arrow/blob/main/cpp/src/arrow/util/tdigest.cc
[6]https://redis.io/docs/latest/develop/data-types/probabilistic/t-digest/
[7]T-Digest in Python
[8]https://blog.bcmeng.com/pdf/TDigest.pdf
[9]https://github.com/duckdb/duckdb
Beta Was this translation helpful? Give feedback.
All reactions