In two sentences
Every event has two times: when it happened (event time) and when your pipeline received it (processing time). A phone offline for an hour, a retry, a slow partner: events arrive late, and a daily total computed once, right after midnight, stays wrong forever unless the pipeline goes back and recomputes the days late events belong to.
See it
| event_id | event_ts (happened) | ingested_at (arrived) | late by |
|---|---|---|---|
| 1 | 04-01 10:00 | 04-01 10:00 | 0 |
| 2 | 04-01 23:58 | 04-02 00:40 | 42 min |
| 3 | 04-01 21:00 | 04-03 09:00 | 36 h |
| 4 | 04-02 08:00 | 04-02 08:00 | 0 |
The job computing 04-01 runs at 00:30 on 04-02. It sees 1 event for 04-01; the truth is 3. Events 2 and 3 belong to 04-01 but arrived after its run.
The next run (04-03 00:30) picks up everything that arrived since the last one: events 2 and 4. They touch two days, 04-01 and 04-02, so both are recomputed. Event 3 arrives after that run and will reopen 04-01 again the day after.
In SQL
Pick new rows by processing time (what arrived since the last run), recompute by event time (the days those rows belong to):
MERGE INTO daily_events t
USING (
SELECT CAST(event_ts AS date) AS event_date, count(*) AS events
FROM events
WHERE CAST(event_ts AS date) IN (
SELECT DISTINCT CAST(event_ts AS date)
FROM events
WHERE ingested_at > TIMESTAMP '2026-04-02 00:30' -- the previous run
AND ingested_at <= TIMESTAMP '2026-04-03 00:30' -- this run
)
GROUP BY CAST(event_ts AS date)
) s
ON t.event_date = s.event_date
WHEN MATCHED THEN UPDATE SET events = s.events
WHEN NOT MATCHED THEN INSERT (event_date, events) VALUES (s.event_date, s.events);Each touched day is recomputed from all its rows, not incremented, so re-running the job gives the same answer.
How late is your data? Measure it before choosing a strategy:
SELECT approx_percentile(date_diff('minute', event_ts, ingested_at), 0.99) AS p99_minutes_late
FROM events;When to use it
| situation | approach |
|---|---|
| a few late events, cheap aggregates | recompute every day touched by newly arrived rows (above) |
| lateness is bounded and known (p99 under 2 days) | fixed lookback: recompute the last N days every run |
| streaming, state must stay bounded | watermark: accept events up to an allowed lateness, route later ones to a corrections table |
| finance, legal reporting | close periods: after a cut-off, late events become adjustments in the next period, never edits to the past |
Best practices
- Store both times on every row, and the batch or offset it came from. You can't reason about lateness you didn't record.
- Partition raw data by arrival, aggregates by event time. Arrival makes "what's new" cheap; event time is what people report on.
- Make the recompute idempotent: replace whole days (or MERGE full recomputed values), never add deltas.
- Measure lateness and alert on it. A partner suddenly two days late is an incident, not a data quirk.
- Tell readers which days are still open. A dashboard can mark the last N days as "may still change".
Common mistakes
- Selecting new rows by event time.
WHERE event_ts > last_runnever picks up event 2: its event time is before the last run. Select byingested_at. - Aggregating only the new rows and adding them. Adding 04-01's late events to yesterday's total works once; a re-run adds them twice.
- A lookback shorter than real lateness. Recomputing 1 day back when partners are 3 days late keeps the totals wrong, quietly.
- Using
max(event_ts)as the cursor. One event from the future (a wrong device clock) moves the cursor past everything still to come.
What an interviewer asks next
- "Your daily active users drop every day and recover the next morning. Why?" Late events: each day is computed before all its events arrive. Recompute touched days, or show recent days as provisional.
- "How do watermarks bound state in streaming?" State for a window is kept until the watermark passes the window's end plus the allowed lateness, then dropped; later events go to a side output.
Go further
Akidau's Streaming 101 and 102 are the classic explanation of event time, processing time and watermarks; the ideas apply to batch as much as to streaming.
Practise it
Fix the Incremental Model That Loses Late Updates, then Incremental Daily Aggregates with Late Data and Compaction (hard), then Micro-Batch Deduplication with a Watermark (hard).