Skip to content
Scribblings of an Introvert
Go back

Apache DataSketches: A Guide to Sketch Types

Edit page

A quick overview of cardinality, percentiles, frequency, sampling, and membership sketches, with their use cases and trade-offs.

In the previous article, we discussed about how to use Apache DataSketch in Scala and looked at the sketchprobe tool to view the contents of a sketch in readable format.

This article gives a summary and overview of different sketch types. Consider this as a datasketch cheatsheet to get an overview.

There are over 20 types of sketches across 5 families. The sketches in the same family solves more of less the same problems. In other words, the sketches in a family answers one type of question.

1. Cardinality: How many distinct things?

This family gives the estimates about the cardinality or distinct counts of values/keys.

SketchIdeaSpecial abilityTrade-off
HLLKeeps small registers that record how rare the hashes it has seen are.Smallest and fastest. Can be merged (union).Counts only. No direct intersection or difference, and it cannot list items.
ThetaKeeps the k smallest hash values it has seen.Set operations: union, intersection, and difference (A − B).Uses more memory than HLL for the same accuracy.
CPCDoes the same job as HLL, but uses a compressed representation.Better accuracy per byte than HLL (about 40% smaller in some configurations).Slower, and the internal representation is more complex.
TupleTheta-like sketch with an associated summary value attached to each retained key.Tracks distinct items along with associated data, such as total spend per user.The value type is not stored in the serialized sketch; the application must know how to interpret it.

How to choose? If you only need a count, use HLL (or CPC for more compactness). If you need to ask “how many users are in both A and B”, use Theta. If each user also carries a number, use Tuple.

Example use cases:

2. Percentiles: What’s the median or p99?

This family estimates percentiles of a stream of numbers, helping you understand the distribution of your data.

SketchError guaranteeItem typesNotes
Quantiles (classic)Uniform rank error across the distributiondoubleOlder design. The parameter k must be a power of two.
KLLUniform rank error across the distributionfloat, double, long, strings (depending on implementation)More space-efficient than the classic quantiles sketch for comparable accuracy. k does not need to be a power of two.
REQRelative rank accuracy focused on either the high or low tailfloatUseful for tail-focused queries, such as p99.9. You choose which end to prioritize.
t-DigestNo strict, general-purpose rank-error guaranteedoubleA separate sketch design. Often compact and effective at estimating extreme quantiles, but its accuracy is empirical rather than guaranteed.

In short: KLL replaces Quantiles for new code, REQ is for extreme tails, and TDigest is a pragmatic alternative with no formal bound.

Example use case:

3. Frequency: What are the most frequent items?

This family identifies items that occur frequently in a data stream and estimates their frequencies without storing every occurrence.

SketchKeeps the keys?Answers
Frequency (Frequent Items)Yes: it stores the heavy hitters themselves.“What are the top 20 platforms?” with an error bound.
Count-MinNo: it stores only a grid of counters.“How many times did key X appear?” for any queried key. It never underestimates the frequency.

Frequency tells you what is popular(heavy hitters). CountMin can only tell you how often a key you already know appeared.

Example use case:

4. Sampling: Give me a sample of the data

This family maintains a representative sample of a large data stream without storing every record.

SketchWhat it does
ReservoirMaintains a fixed-size, uniformly random sample of items.
VarOptMaintains a sample of weighted items, optimized so that sums over subsets (e.g., “revenue for customers in Germany”) can be estimated accurately.
EBPPSMaintains a weighted sample with exact inclusion probabilities for each item.

Unlike the other groups, these hold real items, so the original data is visible.

Example use case:

5. Existence: Have I seen this value before?

This family checks whether a value has been seen before or belongs to a set, helping detect duplicates efficiently without storing every item.

SketchIdeaSpecial abilityTrade-off
Bloom FilterUses a bit array and multiple hash functions to represent a set.Checks whether an item may have been seen before, with a configurable false-positive rate.Can report false positives, but standard Bloom filters have no false negatives for inserted items. Cannot list stored items.
Xor FilterStores a compact fingerprint for each item using an XOR-based structure.Efficient membership checks with a low false-positive rate and compact storage.Typically built for a fixed set of items; adding new items generally requires rebuilding the filter.

Bloom and Xor filters answer yes or no for one value. They don’t count or estimate anything.

Example use case:

What kind of data is stored in the blob?

GroupWhat the blob stores
HLL, CPCCompact summaries used to estimate distinct counts. Some small HLL sketches may store hashed values in list or set mode. Original values cannot be recovered.
ThetaHashes of retained distinct values, not the original values themselves.
TupleHashes of retained keys, plus associated summary values for each retained key.
KLL, Quantiles, REQA sample of the original numerical values, plus compact summary information used to estimate quantiles. Not the entire dataset.
Frequency (Frequent Items)Candidate item keys and frequency-related counters or estimates. Items are encoded using a serializer (serde).
ReservoirThe actual sampled items, encoded using a serializer (serde).
VarOptThe actual sampled items, their weights, and sampling-related information, encoded using a serializer (serde).
Bloom FilterA bit array representing set membership. Original values cannot be recovered.
XOR FilterA compact fingerprint-based structure for set membership. Original values cannot be recovered.
Count-Min SketchA grid of counters used to estimate item frequencies. Original item identities cannot be recovered from the sketch alone.

Edit page
Share this post:

Previous Post
Fast Approximate Analytics in Scala with Apache DataSketches