ealeng

Episode 33 of 55 · Stream processing

Same job, two stream processors: catching up, they disagreed on 526 of 1,000 counts

One job, clicks per ad per minute with 10 seconds allowed for late clicks, run in the lab's own stream processor and in Quix Streams, a real framework, both reading the same clicks from Kafka. Read as a backlog at full speed, they disagreed on 526 of 1,000 numbers: 53.2% of ours exactly right against 17.5% of Quix's. Live, the two totals were one click apart. The cause was what each counts as late: one clock for the whole stream, or one clock per ad. How a backlog, a watermark's definition and the order of events each change the answer, and what the lab did not test.

Lab 14 · Stream Processing8 min read
Video transcript10:42

0:00Cold open

0:01

I wrote one stream job twice: clicks per ad, per minute, with ten seconds allowed for late clicks. Once in my own processor, once in Quix Streams, a real framework, both reading the same stream of clicks in Kafka.

0:16

Reading a backlog, as fast as they could, they disagreed on five hundred and twenty-six of the thousand numbers.

0:25

And neither matched the right answer: mine got about half exactly right, Quix about one in six.

0:33

Why? And then: clicks per user, and different users per ad, counted in a few kilobytes.

0:41Intro

0:42

Hands-on system design, part thirty-three.

0:46

Today: what “late” means to two processors, and two sketches that count in kilobytes.

0:53Meet Lucía

0:54

I’m Lucía, a platform engineer.

0:57

The stream: three hundred thousand generated ad clicks, on a hundred ads, over ten minutes, from about fifty thousand users, replayed through Kafka.

1:08

It’s messy on purpose: over three thousand duplicates, and three percent of clicks arrive one to ten minutes late. The answer is a thousand numbers, one per ad per minute; a batch job, run afterwards, gives the right ones.

1:23

Think of a sorting office. Each minute, for each ad, is a tray. The watermark is the office’s clock: here, the newest click seen, minus ten seconds. A tray is sealed when that clock passes the tray’s end, so ten seconds after its minute ends. A late click is a letter for a tray that’s already sealed: it’s dropped. And the mail arrives in several bags at once: in Kafka, those are partitions.

1:5001 · What you need

1:51

To run it yourself: Docker for Kafka, Postgres and the two processors. The three hundred and eighty-five quick tests and the sketches run without Docker.

2:02

No cloud, and nothing to pay for.

2:0702 · Same job, two answers

2:09

Both read the whole stream, already in Kafka, as fast as they could, like a job catching up after an outage. I compared each with the batch answer.

2:19

Mine: fifty-three percent of the thousand numbers exactly right, and one point eight percent of all clicks missing from the counts.

2:29

Quix: seventeen and a half percent exactly right, and two point seven percent of clicks missing.

2:37

Why so few exactly right? Both drop clicks that arrive after their tray is sealed. About nine thousand clicks are a minute or more late, and they land in nine hundred and thirteen of the thousand trays. One dropped click makes a number wrong. Catching up, mine dropped only fifty-five hundred of those nine thousand; that’s why it got more right.

2:5903 · Two meanings of late

3:01

The difference is what each calls late. Kafka splits the topic into partitions, the mail bags, read in parallel, and all of one ad’s clicks go into the same one.

3:12

Mine keeps one global watermark: the newest click in the slowest partition, minus ten seconds. No tray is sealed until the slowest mail bag has passed it.

3:24

Quix, by default, keeps one per key, here per ad: that ad’s own newest click, minus ten seconds. Each ad’s clock moves at its own pace, so a click can be late for one ad and on time for another.

3:38

Quix calls the ten seconds grace. Both seal a tray when their clock passes its end. While the clicks arrive in order, the two clocks move together. When one mail bag is read far ahead of another, they split.

3:5404 · Live, or catching up

3:55

Then I ran both live, reading the clicks as they arrived, replayed at ten times real speed.

4:03

Live, mine was only seventeen and a half percent exactly right, the same as Quix catching up, with two point seven percent of clicks missing. My fifty-three percent came from catching up: my answer changed with how it read.

4:18

Quix’s didn’t: exactly the same total, live and catching up. And live, the two totals were one click apart.

4:27

Most likely, catching up, my partitions were read unevenly, and the slowest held my one clock back, so trays stayed open longer and fewer clicks counted as late. An ad’s clock in Quix depends only on its own clicks, always in the same order, in one partition. So fast or slow, Quix counted two hundred and ninety-two thousand, four hundred and seventy-seven.

4:5205 · Counting in kilobytes

4:54

Now, counting, in one process, offline, on the same recorded clicks. Clicks per user, counted exactly, need about fifty thousand ids with their counts: about seven hundred and eighty kilobytes as sixty-four-bit numbers. And different users per ad: the biggest ad alone needs about two hundred and ninety kilobytes of sixty-four-bit ids.

5:17

A sketch gives an approximate answer in a small amount of memory that doesn’t grow, however many items arrive. In the sorting office: a small tally board, where many names share each box.

5:3106 · Count-Min in code

5:33

The first sketch, Count-Min, is a grid of counters: a few rows, each a fixed width. You pick epsilon, the error you allow as a share of all clicks, and delta, the chance of going over it. To allow one percent, make each row two hundred and seventy-two boxes wide: e, about two point seven, divided by one percent. One chance in a hundred gives five rows.

5:56

To add a click, each row hashes the user, a fixed scramble of the id, each row its own, to pick one box, and adds one there. Many users share each box: those are collisions.

6:09

To estimate a user, take the smallest of their boxes: the one with the fewest strangers in it. Collisions only add, so it never counts too low.

6:2007 · Count-Min results

6:22

At epsilon one percent, the sketch takes five kilobytes. The average user made six clicks. What does the sketch say for them?

6:32

About a thousand and ten. On average, a user’s count came out about a thousand too high.

6:39

Three hundred thousand clicks over two hundred and seventy-two boxes is over a thousand per box, so even the smallest is full of other users. That’s within what the sketch promised: it’s working, not broken.

6:54

At a hundredth of that epsilon, the overcount averaged two, and fifty-eight percent of users came out exact. But that takes five hundred and thirty-one kilobytes. Exact counts, stored more tightly, could be smaller than that.

7:1108 · The busiest ads

7:12

Count-Min is good at heavy hitters: the few items with the most traffic. Every count gets about the same extra, which swamps a count of six, but not one of seventy thousand, the biggest ad’s clicks. So I switched from users to ads, and asked for the ten most clicked ads in five-minute trays, one starting every minute: six of them.

7:34

Two hundred and fifty-six bytes: seventy percent of the top ten right.

7:40

One kilobyte: ninety-eight percent. Four and a quarter kilobytes: the exact top ten, every time. With only a hundred ads, exact counts would fit in under two kilobytes, so here it’s a demo: the sketch pays off with millions of items.

7:5709 · HyperLogLog in code

7:59

For how many different users, there’s HyperLogLog. Each user id is hashed to sixty-four random-looking bits; the first twelve pick one of four thousand and ninety-six slots, called registers.

8:12

In the rest of the bits, it counts the zeros at the start. A run of ten zeros turns up about once in a thousand different ids.

8:22

Each register keeps only the longest run it has seen. A long run means many different ids passed through; the registers, averaged, give the count. The same user hashes the same way every time, so repeat clicks by one user change nothing.

8:40

Two sketches merge by taking the larger value in each register. The result is the sketch you’d get from both streams together.

8:5010 · HyperLogLog results

8:52

With four thousand and ninety-six registers, one byte each, the sketch takes four kilobytes. For all fifty thousand users, it came out a quarter of a percent low.

9:03

The expected error, called the standard error, is about one point six percent here. Per ad, with a hundred separate sketches, the typical error was one point one percent, and ninety-nine of the hundred ads were off by less than three and a quarter percent: twice the expected error.

9:23

Four times the registers halves the expected error: sixteen kilobytes, under one percent.

9:30

And the hundred per-ad sketches, merged, gave exactly the one-sketch count. So you can count per hour or per server, and combine them later.

9:41Back to Lucía

9:43

So, three lessons.

9:46

Two jobs aren’t the same job until they agree on what late means. Live, one clock and one per ad gave almost the same answer. Reading a backlog, they disagreed on more than half the numbers. Keep a batch answer as the reference.

10:02

Count-Min finds the heavy hitters in a few kilobytes, and pays off with millions of items, but don’t ask it about small counts.

10:11

HyperLogLog counts different things in kilobytes, with an error you can predict, and its sketches merge.

10:19

Next time: pushing each update to many connected users as it happens, and the two classic ways to fan it out.

10:28

For today: define late first.Lucía: Define late first.

10:31Summary

10:33

Two clocks that split on a backlog; Count-Min for the heavy hitters; HyperLogLog for counting different things.

Key takeaways

  • ✓Reading a backlog at full speed, our processor got 53.2% of its 1,000 per-ad, per-minute counts exactly right and Quix Streams got 17.5%, and the two disagreed on 526 of the 1,000 counts, with neither matching the batch answer.
  • ✓The two processors defined late differently: ours used one watermark for the whole stream, the slowest partition's newest click minus 10 seconds, while Quix's default keeps one per ad, so a click could be late for one ad and on time for another.
  • ✓Live, replayed at 10 times real speed, the same processor was 17.5% exact, the same as Quix catching up, and the two totals were one click apart: 292,476 clicks counted by ours and 292,477 by Quix.
  • ✓Quix counted 292,477 clicks both live and catching up, because its per-ad clock depends only on each ad's own clicks, in the order they arrive within one partition. Our total moved from 292,476 live to 295,062 catching up.
  • ✓Both processors dropped clicks that arrived after their minute was sealed, so both missed clicks against the batch answer: 1.84% of the 300,594 clicks for ours and 2.70% for Quix, when catching up.
  • ✓Correcting late clicks instead of dropping them made all 1,000 counts exact, at the cost of 8,118 corrections: results that changed up to 10 minutes after they were first published.

Two processors read the same clicks, from the same Kafka topic, and computed the same thing: clicks per ad per minute, with 10 seconds allowed for late clicks. One was the lab's own processor, written from scratch. The other was Quix Streams, a real Python stream framework. Read as a backlog, at full speed, they disagreed on 526 of the 1,000 counts. Neither matched the batch answer, a DuckDB job that reads the finished stream and gives the reference the lab grades everything against. This chapter takes the disagreement apart: why a backlog exposes it, and what each job means by "late." The examples come from Lab 14 (Stream Processing) of the system design labs, scripts 01_windows_late.py (live and corrections runs), 02_checkpoints.py (kill tests) and 03_framework.py (the two processors, catching up). The stream is 300,594 generated ad clicks, on 100 ads, over 10 minutes, from 49,888 users, replayed into Kafka with 3,170 duplicates and 3% of clicks arriving 1 to 10 minutes late.

The setup is the same laptop as the earlier chapters: an Apple M4 Max with Docker Desktop and 16 virtual CPUs. Each configuration is one recorded run. The machine was busy (load averages of about 7 to 12), so read the shapes more than the last digit. The second half of the video covers two counting sketches, Count-Min and HyperLogLog, which are not part of this chapter.

Why a backlog splits two processors

Kafka splits a topic into partitions: ordered slices that are read in parallel. The lab's topic has six. The replay keys each click by its ad, so all of one ad's clicks land in the same partition, in order.

Every stream job needs a watermark: its claim that "I have probably seen everything before time T." A window (here, one minute for one ad) is sealed, its count emitted, when the watermark passes the window's end. A click that arrives after its window is sealed is late. A late click is dropped, or, with corrections, added later.

Where the two processors differ is the watermark:

  • Ours keeps one watermark for the whole stream: the newest click in the slowest partition, minus the 10-second delay. No window can be sealed until the slowest partition has moved past it.
  • Quix Streams, by default, keeps one per key, here per ad: that ad's newest click, minus its 10-second grace period. Each ad's clock moves at its own pace.

While clicks arrive live, in step, the two clocks move together, and the two jobs nearly agree. A backlog breaks that. When the partitions are read as fast as the machine allows, one partition can run ahead of another. Ours then waits for the slowest partition before it seals anything, so more clicks arrive while their minute is still open and count. Quix's clock for an ad depends only on that ad's own clicks, so how fast the partitions are read does not change it.

The lab did not record each partition's read position, so the reason the slow partition held our clock back is the most likely explanation, not a measured one.

The workflow and the recorded results

The lab ran each configuration against the same replay:

  1. Catching up (03_framework.py). The full stream was put into Kafka first. Then our processor (delay 10 seconds, no checkpoints) and Quix Streams (grace 10 seconds, exactly-once, and at-least-once as a second run) each read the whole topic as fast as they could. Each answer was graded cell by cell against batch.
  2. Live (01_windows_late.py). The stream was replayed at 10 times real speed, and our processor with the same 10-second delay read it as it arrived. The lab labels this run "event time, delay 10 s."
  3. Live Quix (02_checkpoints.py). A Quix Streams run that was not killed. The kill test compares a killed Quix run with this one, not with batch, so its exactness against batch was not measured in that script.

The results, for the grid of 1,000 counts (100 ads by 10 minutes):

Run Job Cells exactly right Clicks not counted Clicks counted
Catching up Ours 53.2% (532) 1.84% (5,532 late clicks dropped) 295,062
Catching up Quix Streams, exactly-once 17.5% (175) 2.70% 292,477
Catching up Quix Streams, at-least-once 17.5% (175) 2.70% 292,477
Live, 10 s delay Ours 17.5% (175) 2.70% (8,118 dropped) 292,476
Live Quix Streams not measured against batch 2.70% 292,477

Catching up, the two jobs disagreed on 526 of the 1,000 cells. Exactly-once and at-least-once (a restart may repeat work, but never skips it) gave the same answer for Quix, because nothing crashed in this run.

Ours was the more accurate of the two catching up, because it dropped 5,532 late clicks, while Quix missed 8,117 clicks. Live, it dropped 8,118 and was 17.5% exact, the same as Quix catching up. The same processor, on the same stream, scored 53.2% or 17.5% depending on how quickly it read.

Try it · Same job, two processorsLab 14 · 01_windows_late.py, 02_checkpoints.py, 03_framework.py · recorded runs, one each

Both read the whole topic in Kafka as fast as they could, after an outage (03_framework.py).

OursWatermark: one for the stream (slowest partition)
Cells exactly right
532 of 1,000 · 53.2%
Clicks not counted5,532 of 300,594 · 1.84%
Dropped as late5,532
Quix StreamsWatermark: one per ad (its own newest click)
Cells exactly right
175 of 1,000 · 17.5%
Clicks not counted8,117 of 300,594 · 2.70%
Dropped as latenot recorded in this run

Readouts

Ours, catching up
53.2%
Ours, live
17.5%
Quix, catching up
17.5%
Quix, live: clicks counted
292,477

Live, the two totals were 1 click apart (292,476 and 292,477). Catching up, the two jobs disagreed on 526 of 1,000 cells. Quix counted 292,477 both ways.

Quix's behaviour is the reverse. Its total was 292,477 clicks in every run, catching up and live. Its exactly-once mode (each click counted once, even across restarts) also survived 20 kills with the same total as an uninterrupted run, so the difference between the two processors is the definition of late, not crash handling.

What the delay buys

The delay sets how long a window waits for late clicks. In the live runs, ours dropped 9,520 clicks with no delay, 8,118 with 10 seconds and 6,784 with 60 seconds. Going from 10 to 60 seconds cut the drops by only about 16% and made every result about 50 seconds later. This stream's lateness has two humps: most clicks arrive within 5 seconds, and the 3% that are late are 1 to 10 minutes late, so no delay short of 10 minutes catches them. The lab's guidance is to choose the delay from the measured lateness, not by feel.

Corrections avoid the choice. The corrections run, with the same 10-second delay, made all 1,000 counts exact, at the cost of 8,118 corrections, one for each click that arrived after its minute had been published. A result for a minute could change up to 10 minutes after it first appeared, so any consumer of the counts has to replace a result by its window, not append it.

Caveats

  • One run per configuration. Each row is a single recorded run on one laptop. The live and catching-up runs used different runs of the replay, so compare shapes, not last digits.
  • The live runs shared a replay. Six copies of our processor read the same live replay with different settings, so the live figures come from a concurrent setup, not a lone job.
  • Quix's live exactness was not measured. Its 292,477 counted clicks are recorded, and the share of clicks missing follows from that count, but its cell-by-cell match against batch was not run live.
  • Quix's dropped count was not recorded in the catching-up run. The 8,117 missing clicks come from its error total.
  • The explanation is a likely one. The uneven read speed of the partitions was not measured. The video says "most likely," and so does this chapter.
  • Not covered: speed (ours read the topic in 2.7 seconds and Quix in 12.0, but that is not what this chapter is about), the checkpoint kill test beyond the one Quix run, Count-Min and HyperLogLog, and any real cluster or production load.

The checklist

  1. Write down what "late" means before the job runs: the delay, and whether the clock is per stream or per key.
  2. Keep a batch answer and grade the stream against it, cell by cell, not by its total.
  3. Compare a live run with a catch-up run of the same stream. If the totals differ, the job depends on timing.
  4. Prefer per-key clocks or corrections when results must be reproducible. Corrections gave 100% exact cells here.
  5. Check which watermark a framework uses before comparing its numbers with another system's.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.Catching up, our processor and Quix Streams disagreed on 526 of 1,000 per-ad, per-minute counts, though both ran the same job. What is the most likely reason?

  2. 2.Live, our processor counted 292,476 clicks; catching up, it counted 295,062. Why did its total change, when Quix's did not?

  3. 3.The lab made every count exact by correcting late clicks instead of dropping them. What does that cost?

Check your understanding

Try each question before opening its answer.

Ours counted 295,062 clicks catching up and 292,476 live. Why did its total change with how it read, while Quix's did not?

Ours uses one watermark for the whole stream: the newest click in the slowest partition, minus 10 seconds. Read unevenly, that partition lags, the clock lags with it, and more clicks count as on time. Quix's clock is one per ad and depends only on that ad's clicks, in order, so the reading speed does not change it. The lab did not record partition positions, so this is the most likely cause, not a measured one.

Neither job matched batch. Why keep batch as the reference, and what did it show?

Batch reads the finished stream and gives the answer that has every click, so it is the only way to see how many clicks each job missed. Comparing the two jobs with each other shows only that they differ. Batch shows the shortfall: catching up, ours missed 1.84% of the 300,594 clicks, and Quix missed 2.70%.

Quix's exactly-once mode is meant for crashes. Did it change which clicks count as late?

No. In the kill test, Quix's exactly-once run survived 20 kills with the same count as an uninterrupted run: 292,477 clicks and 1,000 of 1,000 cells. Exactly-once keeps each click counted once across restarts. It does not change Quix's per-ad clock, so it cannot change which clicks are late.

The lab reports 53.2% for our processor. What should you check before quoting that as its accuracy?

That it is one catching-up run on one laptop, against one batch answer. The same processor, on the same stream, scored 17.5% live, so its accuracy depends on how fast it read. Quote the live and catching-up figures together, and say which one you measured.

Next up: Chapter 34, polling, server-sent events and WebSockets. This chapter's stream jobs wrote counts that a dashboard reads. The next one covers the other half: pushing each update to many connected users as it happens, and the two classic ways to fan it out.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.