Core Concepts

Batch vs Stream Processing

Choose a reporting schedule, assign late events to the right window, and recover a counting job without inflating the report.

Free~19 min

Batch processing produces a result from a fixed input for each run. Stream processing updates results as events arrive. Decide when users need the result and what should happen when an event arrives late.

Decide when the result must appear

The URL Shortener captures clicks on a best-effort basis. For code spring, we can report yesterday's captured clicks each morning or publish five-minute counts. In the dashboard example, each count is due within 30 seconds of its window closing under normal delays.

At 01:00, a scheduled job can read a snapshot of the captured clicks from yesterday, group them by short code, and publish one result per code. The input is bounded, meaning this run has an end. A rerun can use the same snapshot. A later run can intentionally include new arrivals as a correction.

A stream job keeps reading as new records arrive. The input is unbounded because there is no planned last click. The job updates its running counts and can publish while it keeps reading. This avoids waiting for tomorrow's job. It also means a dashboard count can be provisional while some events are still in transit.

A job that runs every minute over a fixed slice is a small scheduled batch. A job aligned to each five-minute boundary can meet our 30-second target when captured input is ready and reading, counting, and publishing fit within those 30 seconds. Choose continuous processing if the dashboard also needs corrections promptly as delayed clicks arrive. A worker that continuously reads a topic and writes every 100 rows is still processing a stream. It batches its output to reduce the cost of database writes.

Flink can process finite data in either batch or streaming execution mode. Batch mode can use the end of input to finish its work. Streaming mode can emit incremental updates even on a finite input. The dashboard decision is about how the report gets new results as data arrives. Flink batch and streaming execution modes

Put each click in the right window

The redirect service records when the click happened. That is its event time. The job looks at the clock again when it handles the record. That is processing time. If a click at 10:04:40 reaches the job at 10:05:40, we want it in the 10:00 report rather than counting it as new activity at 10:05.

For spring, we keep a count for each fixed five-minute window. The first window includes 10:00 and ends just before 10:05. The one that follows includes 10:05. These adjacent intervals are tumbling windows. A rolling five-minute count published every minute would use overlapping sliding windows, so a click can contribute to several outputs. A session window groups activity separated by less than a chosen idle gap, which can fit a visit rather than a campaign's time buckets. Flink window types

Consider four captured clicks for the first window. The job sees them in the order shown.

ClickEvent timeTime the job sees itWhat the job does
A10:04:1010:04:11Counts A in the 10:00 window.
B10:03:5010:04:55Counts B in the same window. B happened earlier than A and arrived later.
C10:04:4010:05:40Revises the published window from 2 to 3 while the job still has its count.
D10:04:2010:08:10Leaves the live count at 3. The raw archive still holds D for a later rebuild.

Before C arrives, the job receives a watermark at event time 10:05. A watermark tells the job how far event time has progressed and which older timestamps it expects to have seen. In this example the watermark reaches 10:05 at 10:05:25 on the job's clock. The job publishes 2 for the completed interval. The report can still change if a click shows up behind the watermark.

The job keeps the count until the watermark reaches 10:07 in this example. C arrives during that correction period. D arrives after cleanup, so a separate archive-based rebuild can include it. Treat the 30-second target as a freshness goal for first publication. The two minutes of extra state are for corrections and advance with the watermark, which may lag the wall clock. Tracking the fraction of reports published late and the number of clicks arriving after cleanup helps us tune both rules. Flink watermarks and late elements · Flink window lateness

With several inputs, a Flink operator uses the minimum watermark among active inputs. A quiet partition can keep the report waiting even if the other partitions are current. Flink can mark an inactive input idle so it stops holding back that minimum. When an idle input resumes, older records may arrive late. Measure both the watermark delay and the resulting late data before changing the idle threshold. Flink parallel watermarks and idle inputs

Replay after a crash without counting twice

A Kafka topic can retain captured clicks. Each partition is a log, and an offset identifies a position within it. Reading in partition order still gives us A before B if they were appended in that order. We need the event timestamps to put them in the intended window. Replay is possible while the source retains the necessary records. Apache Kafka design

A Flink job can save a checkpoint with its running window counts and the corresponding source positions. In a one-partition example, the job has published 2 and its latest completed checkpoint records that the next offset is 420. It reads C at 420, publishes 3, and crashes before another checkpoint completes. On recovery, the job reloads count 2 and starts at offset 420. It reads C again and emits count 3 again. Flink checkpointing requirements and recovery

The output destination is the sink. Its failure behavior matters separately. A database that applied count = count + 1 before the crash applies the increment a second time on replay. For this report, the job can write the full count under (short_code, window_start). If it sets the same row to 3 twice, the final value is still 3. This write is idempotent for the repeated update. If earlier and later report revisions can reach the sink out of order, use a version order that survives retries and prevent older revisions from overwriting newer ones.

A transactional sink can instead keep output hidden until the protocol commits it with the checkpoint. Check the guarantees of the specific connector and what readers can see. A correct final row can coexist with temporary older values during replay in a sink that simply overwrites rows. A checkpoint by itself does not protect an unrelated external effect such as sending an alert. Flink connector fault-tolerance guarantees

Rebuild and reconcile the report

Keep captured raw events in an archive if a repair or historical report must reach further back than the topic retains. Use a backfill, a bounded job over past input, to recompute an affected range. For the spring example, a later rebuild whose archived input includes D can produce a corrected count of 4. Record which input snapshot and calculation rules produced the result. If the producer can submit one captured click twice, keep a stable event ID and decide where to deduplicate it. Replay safety for a result row and deduplication of separate source records address different failures.

If the stream and backfill both own the same row, whichever writes last can erase the other's work. For our reporting product we can keep the recent dashboard and daily report separate, or publish a validated backfill into a closed range after the live writer stops changing it. A dashboard can show when its data was last refreshed and whether it still accepts corrections. The raw archive and all downstream guarantees apply to events we captured. Redirects that never reached the capture path still need to be accounted for separately.

See also

  • Kafka - retain partitioned records and replay from a saved position.
  • Flink - run event-time windows and recover operator state with checkpoints.
  • Idempotency - make repeated effects safe at the storage boundary.
  • Object Storage (S3) - keep raw input for historical rebuilds.
  • Messaging and Work Queues - distinguish a retained log from work delivery.

Used in

  • URL Shortener - the redirect captures clicks without waiting for analytics. Its consumer batches writes while continuing to read the stream.
  • Batched Inference Service - workers group arrivals into small GPU batches within a continuously running service. Compare that operation-level batching with a reporting job over fixed input.

Done reading? Mark it so it sticks in your dashboard.

Discussion