Understand cardiovascular aggregation in Elasticsearch
in Note with 0 comment
Understand cardiovascular aggregation in Elasticsearch
in Note with 0 comment

preface

We have many business scenarios that use de counting. In order to do this better, we need to have a deep understanding of the tools used. Cardinality aggregation is a single value metric aggregation method provided by Elasticsearch to calculate the approximate count of non repeated values. The calculation result is approximate because the algorithm based on HyperLogLog++is used, so you only need to understand the HyperLogLog++algorithm in depth.

Algorithm principle

The HyperLogLog++algorithm is an algorithm used to approximate the cardinality problem (that is, to calculate the number of different elements in a set). It is an improved version of the HyperLogLog algorithm. The main characteristics of this algorithm are higher accuracy and faster calculation speed.

The HyperLogLog++algorithm is based on the following two main ideas:

  1. Random hash function: map each element to a random hash value, and use the prefix of the hash value as the bucket number;
  2. Counter: assign a counter to each bucket to record the number of elements in the bucket.

Algorithm performance

To calculate an accurate count, you need to import the value into the hash set and return the size of the set. When processing high cardinality sets and/or large values, it cannot be expanded, because the required memory usage and the need to transfer each shard set between nodes will consume too much cluster resources.

Cardinal aggregation is based on the HyperLogLog++algorithm, which counts based on numerical hashes. The algorithm has the following characteristics:

  1. Configurable precision determines how to sacrifice memory for precision;
  2. The accuracy of low cardinality sets is very high;
  3. Fixed memory usage: regardless of whether there are billions or billions of unique values in the collection, memory usage only depends on the accuracy of configuration.

Assuming the accuracy threshold is c, the implementation method we use requires about c * 8 bytes of memory.

The following chart shows the error variation above and below the precision threshold:

 2021-10-27T02:15:44.png

For the three thresholds in the figure, the cardinality count before the configured threshold is accurate. Although there are errors, they are very close to the true value. In fact, accuracy depends on the data set. Generally speaking, most data sets show good accuracy. We also need to note that even if the threshold is as low as 100, the error is very low when counting millions of data (1-6% error is shown in the figure).

The HyperLogLog++algorithm depends on the number of pre order zeros of hashed values, and the accurate distribution of hashed data sets will also affect the accuracy of cardinality aggregation.

ES source code

Project address: https://github.com/elastic/elasticsearch

Code location:

Source code of third-party library

Mainly Golang:

matters needing attention

Cardinality aggregation uses an approximation algorithm. precision_threshold determines the precision of the value.

The overhead of cardinality aggregation on memory can be calculated using the following method:
If the precision_threshold is 10000, the memory cost is 10000 * 8/1024, which is about 80KB.

In general, it is sufficient to use the default value, which is obtained by combining precision and memory overhead.
However, some business scenarios require high accuracy, so the threshold can only be raised. The method of height adjustment is generally close to the average value of the observed value. For example, when using the default value, it is observed that the average de duplication value is about 10000, then we can try to adjust the threshold value to 10000. The side effect is that it may not support high-frequency requests, because it will lead to greater memory overhead, which will crowd out other resources, resulting in long GC, affecting the work of other threads and causing service exceptions.

link

Responses