Apache Spark Streaming 00:00:01 A continuously updated result from bounded batches A payments team wants a running count from Kafka. In the micro-batch execution model, Spark Structured Streaming repeatedly fixes a bounded input range and advances the result a batch at a time. The driver asks Kafka how far the topic has grown. Batch 7 is rows 301 to 500. The plan goes to the checkpoint. Executors read exactly these rows. Row 501 arrives now and must wait. State version 7 is built from version 6 and saved. The sink has the rows, then the driver marks batch 7 as done. Rows arriving after a batch's end offsets belong to a later batch. Checkpointed progress can reproduce a chosen input range even when the live topic keeps growing. 00:00:51 Batch seven reaches the sink twice Assume the database has already received this batch but the driver has not committed its own completion record. To the restarting driver the work remains unfinished. Batch 7 is planned and its rows are in the sink. There is no commit for batch 7. The new driver reads the checkpoint: batch 7 is planned and not committed. Rows 301 to 500 run again. The sink receives them a second time. A replayable input and a checkpoint handle computation recovery. External effects still depend on the sink. In particular, a custom foreachBatch operation needs an explicit plan for repeats. 00:01:35 Make one batch visible once Use the query identity together with the batch identifier as a durable deduplication key. In this example the sink supports a database transaction covering both output rows and the completion marker. The function opens a transaction in the database. Id 7 is not in the table of finished batches. Rows and id are written together. The driver dies before its own commit. The same batch comes again with id 7. It is already there, so nothing is written. If the same batch returns, the committed marker can suppress the repeat. Writing a marker in a separate transaction would leave a failure gap between the marker and the actual rows. 00:02:20 Event time can trail arrival time The team also groups by event-time windows. A watermark puts a bound on how long this aggregation retains old state under the configured policy. The first batch observes a latest event time of ten oh four. With the ten-minute delay used here the illustrated next watermark is nine fifty-four. An out-of-order event at ten oh one can still contribute in this example. A later event at ten thirteen advances the next watermark. The five-minute window remains open long enough to count an event at ten oh four. The advancing watermark subsequently closes it. A later-arriving event at ten oh three targets a window whose state this run has already removed. Once a window is eligible for cleanup, events much later than the watermark may be discarded. Validate how your configured operation handles late records before making promises to report consumers. 00:03:19 When batches outrun the trigger interval A ten-second trigger is an opportunity to schedule work. It cannot make twelve seconds of computation finish in ten seconds. Watch processing time and input lag together. 6 seconds of work, then 4 seconds of rest. Three times the rows arrive. The batch takes longer than the interval. The next batch starts at once and holds even more rows. More cores for the peak, and a row limit that keeps each batch small. Limiting rows per batch makes individual batches manageable. The service still needs sufficient sustained processing capacity to catch up. Pair measured lag with a recovery-rate target.