How many distinct users visited your website yesterday? On a single machine you might sort the log, or keep a hash set. But what if the log is split across a hundred servers, and the full set of user IDs won't fit in memory on any of them?
HyperLogLog (Flajolet et al., 2007) is a probabilistic data structure that estimates the number of distinct elements — the cardinality — of a multiset using only a few kilobytes of memory, with a typical error under 2%. It does this by hashing each item and tracking, in each of registers, the maximum number of leading zeros seen so far.
The insight that makes it beloved in distributed systems is its merge property: to combine two independent HyperLogLog sketches built on different data shards, you simply take the element-wise maximum of their register arrays. The result is identical to a sketch built on the union of both datasets. No communication is needed beyond swapping a tiny array; no re-scanning of data; no shared state.
That single max per register is what lets engineers count distinct events across fleets of machines in real time — and it follows directly from the mathematics of how the registers are filled.
Comments
Loading comments...