# Strike gold (and avoid drowning) with streaming algorithms

- Speaker: [Guy Smoilovsky](/wiki/speakers/guy-smoilovsky.md)
- Event: [Reversim Summit 2019](/wiki/years/2019.md)
- Video: https://www.youtube.com/watch?v=0HNr_cB8GEs
- Transcript: [/transcripts/2019/03-0HNr_cB8GEs.md](/transcripts/2019/03-0HNr_cB8GEs.md)
- Language: Hebrew · Type: talk · Level: intermediate
- Topics: [Data Engineering](/wiki/topics/data-engineering.md), [Data Science](/wiki/topics/data-science.md), [Databases](/wiki/topics/databases.md), [Distributed Systems](/wiki/topics/distributed-systems.md)

> Written by an LLM from machine transcripts. It can contain errors. Check the transcript before you quote it.

## Summary

This talk explores the practical application of streaming algorithms to solve heavy data scaling challenges in real-world systems. The speaker explains how to bypass memory limitations and expensive disk operations by trading absolute accuracy for probabilistic solutions. Through a case study, the audience learns how to implement the HyperLogLog algorithm via a custom plugin to drastically reduce query times from minutes to milliseconds.

## Key points

- [1:23](https://www.youtube.com/watch?v=0HNr_cB8GEs&t=83s) Streaming algorithms process massive data streams sequentially with limited, finite memory without knowing the order of arrival.
- [3:38](https://www.youtube.com/watch?v=0HNr_cB8GEs&t=218s) Mathematically, calculating exact unique counts or cardinalities in a stream using limited memory is impossible.
- [5:46](https://www.youtube.com/watch?v=0HNr_cB8GEs&t=346s) By compromising on exactness, probabilistic algorithms like HyperLogLog can estimate unique counts within a very small margin of error.
- [11:11](https://www.youtube.com/watch?v=0HNr_cB8GEs&t=671s) A real-world analytics platform faced massive performance bottlenecks and slow query times over terabytes of distributed data.
- [14:25](https://www.youtube.com/watch?v=0HNr_cB8GEs&t=865s) Pre-aggregating data into fixed five-minute time batches successfully solved scaling issues for standard metrics like averages.
- [16:32](https://www.youtube.com/watch?v=0HNr_cB8GEs&t=992s) Calculating unique visitors across pre-aggregated time windows causes double counting errors, requiring streaming algorithm sketches instead.
- [19:45](https://www.youtube.com/watch?v=0HNr_cB8GEs&t=1185s) The team wrote a custom plugin to merge partial HyperLogLog states directly, dropping query execution time to half a second.

## Takeaways

- Consider probabilistic data structures when dealing with massive streams of data where exact counts are unnecessary.
- Leverage pre-aggregation and sketch combining techniques to optimize slow distributed database queries.
- Be mindful of garbage collection overhead when parsing input streams into memory-intensive data structures in Java.

## Questions this talk answers

- How can you calculate unique visitors across multiple pre-aggregated time intervals without double counting?
- Why do traditional data structures like HashMaps fail for cardinality estimation in streaming environments?
- How do distributed databases like Elasticsearch compute approximate unique counts under the hood?

## Technologies

Elasticsearch, Cassandra, Java

## Tags

big data, cardinality estimation, data aggregation, distributed systems, performance optimization, streaming algorithms
