Streaming Algorithms: Sampling, Sketches and Compression in One Pass
Some data never stops arriving: network traffic, click streams, sensor readings, logs. A streaming algorithm processes it one item at a time, usually in a single pass, without ever holding the whole dataset in memory. The price is usually a little accuracy, traded for bounded memory and the ability to answer in real time.
Sampling and counting
- Reservoir sampling picks a uniform random sample of k items from a stream of unknown length. It keeps a reservoir of k items and replaces them with decreasing probability as new items arrive.
- Count-Min Sketch estimates how often each item has appeared. It hashes every item into several rows of counters and reports the smallest count, which can overestimate but never underestimates.
- Lossy Counting approximates item frequencies within an error bound you choose, which in turn fixes how much memory it needs.
- Misra-Gries and SpaceSaving find the heavy hitters (the most frequent items) by keeping counters for only a small set of candidates.
Distinct elements and membership
- Flajolet-Martin estimates the number of distinct items from the bit patterns of their hashes.
- HyperLogLog refines the same idea and estimates cardinalities in the billions with a few kilobytes of memory.
- Bloom filters answer "have I seen this before?" in very little space. They can return false positives, never false negatives.
Running statistics
- Moving averages, simple or exponential, summarize the most recent window of values.
- Welford's algorithm keeps a running mean and variance in one pass, and stays numerically stable where the textbook formula does not.
- t-digest estimates quantiles and histograms, keeping small clusters near the extreme quantiles so that tail estimates such as the 99th percentile stay accurate.
- Sliding-window algorithms only ever consider the most recent n items or the last t seconds, which is what most real-time dashboards want.
Learning from a stream
- Hoeffding trees and their best-known implementation, VFDT (Very Fast Decision Tree), grow a decision tree incrementally. They use the Hoeffding bound to decide when they have seen enough examples to split a node with statistical confidence.
- Online k-means clusters points as they arrive, nudging each centroid toward the points assigned to it.
- ADWIN (adaptive windowing) detects concept drift, a change in the stream's distribution, by keeping a window that grows while the data is stable and shrinks when it is not.
Compression on the fly
Compression is itself a streaming problem: the encoder sees the data once, in order, with a bounded buffer.
- LZ77 and LZ78 (Lempel-Ziv) replace repeats with references back into a sliding window. Most modern compressors are descended from them.
- DEFLATE combines LZ77 with Huffman coding; it is the format inside gzip and ZIP.
- Snappy and LZO favour speed over ratio, for real-time systems and databases.
- Zstandard (zstd) gets close to the best ratios while staying fast, with a wide range of levels.
- Brotli compresses better than gzip and is widely used for web content.
- Delta encoding stores the difference between successive values, which is very effective for time series.
- Run-length encoding collapses repeated values and remains useful for some kinds of data.
Encryption on the fly
- AES in a streaming mode such as CTR encrypts a stream block by block without padding.
- ChaCha20 is a stream cipher that is fast in software, usually paired with Poly1305 for authentication.
- AES-GCM and ChaCha20-Poly1305 are AEAD modes: they encrypt and authenticate at once, and should be the default.
- Salsa20 and XSalsa20 are ChaCha's predecessors, still found in libsodium-based code.
- RC4 is a fast, simple stream cipher that is broken and should not be used.
- TLS is the protocol that carries most of this in practice.
Further out, format-preserving encryption keeps ciphertext in the same shape as the input, incremental cryptography updates ciphertext without redoing all of it, and homomorphic encryption computes on encrypted data, though it is still far too slow for most streams.
Choosing
Every one of these is a trade between accuracy, memory and speed. The questions that decide it are how much error you can live with, how much latency you can afford, whether you will ever need random access into the stream, and how much compute both ends of the pipe have.