Stream Processing: Managing Temporal Complexity

Stream processing is the continuous computation of unbounded datasets. Unlike batch processing, which operates on a static snapshot, stream processing must handle data that arrives late, out-of-order, or over inconsistent network conditions.

1. The Challenge of Time

To achieve correctness, we must distinguish between:

In distributed systems, the gap between these two is non-deterministic. We rely on Event Time for accurate business logic (e.g., "how many clicks happened between 2:00 and 2:05?").

2. Watermarking: Taming Lateness

A Watermark is a control signal that tells the system: "I am confident that no more events with a timestamp earlier than T will arrive."

2.1 How Watermarks Work

As events flow through the system, the engine tracks the maximum observed event time (T_{max}). It emits a watermark at T_{watermark} = T_{max} - \text{slack}.

3. Windowing Mechanics

Windowing allows us to group unbounded streams into finite chunks for aggregation.

3.1 Tumbling Windows (Fixed, Non-overlapping)

Tumbling windows partition the stream into discrete, equal-sized segments.

3.2 Sliding Windows (Overlapping)

Sliding windows have a fixed length but "slide" by a specific interval (the slide).

3.3 Session Windows (Activity-based)

Session windows do not have a fixed size. They are defined by a "gap" of inactivity.

4. State and Fault Tolerance

Stream processors (like Apache Flink) must maintain State (e.g., the current sum in a window). To ensure "Exactly-Once" semantics, the system periodically takes Checkpoints—consistent snapshots of the distributed state. In the event of a failure, the system rolls back to the last checkpoint and replays the stream from the corresponding offset in the message broker (e.g., Kafka).