K-means at Scale with Apache Beam

One clustering problem solved five ways, from bounded-memory Python to stateful Apache Beam pipelines: memory flat at about 270 KiB up to one million points, drift tracked at 0.12 versus 2.30, and every distributed version checked against scikit-learn.

2026 · Shipped

A project for the Big Data course of the M2 MLSD at Université Paris Cité. The same goal, clustering unlabelled points with k-means, is reached under five different constraints: data larger than memory, a distribution that drifts over time, a computation spread over several machines, a state to keep inside a distributed pipeline, and both at once. For each version I measured what it guarantees, what it costs in memory and what limits its parallelism, with tests that check the five implementations agree.

Sequential, bounded memory. A generator reads the CSV one line at a time and the whole state is k centres and k counts. Peak memory stays between 254 and 272 KiB from a thousand to a million points, against 92 MiB when pandas loads the file. The catch is order: on the same points sorted by class, the distance to the scikit-learn centres jumps from 0.04 to 1.85, and three passes bring it back to zero.

Streaming with weighted batches. Only the T latest batches are kept, weighted by rt, and each update starts from the previous partition. On a drifting stream the tracking error stays at 0.12, while the sequential version, whose step size shrinks as 1/n, drifts away to 2.30. The assignment says a larger r gives less weight to old batches, but with rt it is the opposite for r < 1; I followed the formula and documented the gap.

Distributed with Apache Beam. Lloyd's algorithm, with the iterations unrolled inside a single DAG, deterministic hashed keys so that a replayed bundle lands in the same cluster, and a CombineFn over (sum, count) instead of a GroupByKey. The centres match scikit-learn started from the same initialisation to within 4 × 10-15. Broadcasting the centres as a matrix, batching the points and emitting at most k partial sums per batch is 2.6 times faster on a million points.

Stateful and streaming pipelines. A stateful DoFn runs the sequential update and reproduces the Python version exactly on a single key, which also shows that its result depends on an arrival order Beam never guarantees. Splitting it over K′ keys parallelises the step at a measured cost in accuracy. The streaming version uses TestStream, fixed windows and a state holding the last T batches sorted by id: it matches the Python streaming results on all 60 batches, ignores replayed batches and lets the watermark drop late points.

The Problem

When data no longer fits in memory, drifts over time or has to be split across machines, the textbook k-means stops working, and each workaround trades accuracy, memory or parallelism in a different way.

The Solution

Five implementations of the same algorithm, from a bounded-memory Python pass to stateful and streaming Apache Beam pipelines, each measured on the same data and checked against scikit-learn and against each other.

Results at a glance: constant peak memory up to one million points, and drift tracking error
Results at a glance: constant peak memory up to one million points, and drift tracking error
The five implementations: memory and parallelism
The five implementations: memory and parallelism
Distributed Lloyd in Beam: unrolled loop and optimised assignment
Distributed Lloyd in Beam: unrolled loop and optimised assignment
Tracking a drifting stream: streaming versus sequential
Tracking a drifting stream: streaming versus sequential
Peak memory and time of the sequential version, up to one million points
Peak memory and time of the sequential version, up to one million points
Stateful DoFn: one key, or K′ keys then a merge
Stateful DoFn: one key, or K′ keys then a merge
Streaming pipeline: TestStream, windows and a state of T batches
Streaming pipeline: TestStream, windows and a state of T batches
Effect of data order on the sequential algorithm
Effect of data order on the sequential algorithm
Year
2026
Role
Sole author: implementations, Beam pipelines, experiments, diagrams, report and slides (M2 coursework).
Tech Stack

Apache Beam · Python · Big Data · Streaming · k-means · scikit-learn · NumPy · Distributed Computing