Case study demo

Ad click aggregation

Count a stream of ad clicks in time windows, and see what late and repeated clicks do to the totals.

An interactive demo of how this system behaves. It runs in your browser.

The whole system

How the parts fit together

clicktotalsBrowser, clicks an adBrowserclicks an adClick API, checks, then acceptsClick APIchecks, then acceptsClick log, every click, in orderClick logevery click, in orderStream aggregator, counts per minuteStream aggregatorcounts per minuteRaw clicks, kept for recountsRaw clickskept for recountsTotals DB, clicks per ad, minuteTotals DBclicks per ad, minuteAdvertiser view, reads the totalsAdvertiser viewreads the totals
  • Browser — clicks an ad — healthy
  • Click API — checks, then accepts — healthy
  • Click log — every click, in order — healthy
  • Stream aggregator — counts per minute — healthy
  • Raw clicks — kept for recounts — healthy
  • Totals DB — clicks per ad, minute — healthy
  • Advertiser view — reads the totals — healthy
  • Request/response
  • Asynchronous

Every click is written to a log first, so none is lost. A stream job adds the clicks up per minute, and the raw log lets the totals be counted again.

The demo below lets you try one part of this system.

The part you can try here is the lab Stream windows & late events. It is the same live model.

Lab · Architecture patterns

Stream windows & late events

Phones send ad clicks, each stamped with the time it happened. A counter adds them up in 10-second windows. Make phones go offline, let retries send clicks twice, and choose how long the counter waits before a window's count is final.

Try it

Lab time 3.0 s
Phones, 4 clicks a secondPhones4 clicks a second10% offlineEvent stream, clicks, any orderEvent streamclicks, any orderWindow counter, windows of 10 sWindow counterwindows of 10 swatermark 0.7 sResults, 0 windowsResults0 windows
  • Phones — 4 clicks a second — healthy — 10% offline
  • Event stream — clicks, any order — healthy
  • Window counter — windows of 10 s — healthy — watermark 0.7 s
  • Results — 0 windows — healthy
  • A click (tagged late or again when it is)
  • A window's final count
Clicks arrived
3
Dropped late
0
Counted twice
0
Windows exact
0 of 0
Result delay
—

Numbers add up:3 arrived = 3 counted + 0 dropped late + 0 late retry copies + 0 duplicates removedThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

The counter
2 s

How long past a window's end the counter waits for stragglers.

Remember click ids and ignore a second copy.

The phones
10%

They send clicks 5 to 20 s late.

0%
4

Recent windows

WindowCountedReally happenedDropped lateStatus
0–10 s3…—open

Window times are event times: when the click happened, not when it arrived. "Really happened" is shown once a window closes.

What just happened

  1. 0.0sInfo: Windows of 10 s. Late clicks are allowed for 2 s.
What this model simplifies
  • Time is slowed so you can watch each click. Windows here are 10 seconds of event time.
  • The watermark only moves when a click arrives. Real streams also move it on a timer, so a quiet stream still closes windows.
  • A late click for a closed window is dropped. Real systems can instead send a corrected result, or route late data to a separate store.
  • Windows are tumbling: back to back, with no overlap. Sliding and session windows work the same way with lateness.
  • Here "allowed lateness" is how far the watermark trails the newest click (Flink calls it bounded out-of-orderness). In Flink, allowed lateness is a second wait after the watermark, during which a late click updates a result already sent.