Skip to main content

Event Time & Watermarks

Module Duration: 4-5 hours Focus: Event time processing in Apache Flink Prerequisites: DataStream API, streaming concepts

Introduction

Event time processing is fundamental to building correct streaming applications. Unlike processing time (when events are processed), event time represents when events actually occurred. This distinction is critical for handling out-of-order events, late data, and producing deterministic results.

Time Semantics

Three Notions of Time

Why Event Time Matters

Watermarks

Watermarks are timestamps that flow through the stream, indicating “all events with timestamp < T have arrived.”

Watermark Semantics

Watermark Strategies

Flink 1.11+ introduced the WatermarkStrategy API:

Watermark Generation Patterns

Periodic Watermark Generator

Generated at fixed intervals (default: 200ms).

Punctuated Watermark Generator

Emits watermarks based on special marker events.

Advanced Watermark Strategies

Per-Partition Watermarks (Kafka)

Custom Watermark with Multiple Timestamps

Timestamp Extractors

Built-in Timestamp Assigners

Timestamp Extraction from Different Sources

Allowed Lateness

Handle events that arrive after the watermark has passed.

Complete Late Data Handling Example

Watermark Propagation

Multi-Input Operators

Watermark Alignment

Real-World Use Cases

Use Case 1: IoT Sensor Data with Clock Drift

Use Case 2: Log Aggregation from Distributed Systems

Use Case 3: Financial Transaction Processing

Debugging Watermarks

Watermark Monitoring

Watermark Metrics

Best Practices

1. Choose Appropriate Watermark Strategy

2. Handle Idle Sources

3. Set Appropriate Allowed Lateness

4. Monitor Watermark Lag

5. Test with Out-of-Order Data

Performance Optimization

1. Watermark Interval Tuning

2. Parallelism and Watermarks

3. Avoid Frequent Watermark Emissions

Exercises

Exercise 1: Implement Adaptive Watermark Generator

Create a watermark generator that adapts to observed out-of-orderness.

Exercise 2: Handle Multi-Source Watermarks

Implement proper watermark handling for multiple sources with different characteristics.

Exercise 3: Debug Watermark Issues

Given a job with late data, identify and fix the watermark configuration.

Summary

In this module, you learned:
  • The three notions of time in stream processing
  • Why event time is critical for correct results
  • Watermark semantics and generation strategies
  • Periodic vs punctuated watermark generators
  • Timestamp extraction from various sources
  • Handling late data with allowed lateness
  • Watermark propagation in dataflow graphs
  • Real-world use cases (IoT, logs, finance)
  • Debugging and monitoring watermarks
  • Performance optimization techniques
  • Best practices for production systems

Next Steps

Module 4: Windows & Time Operations

Learn how to implement sophisticated windowing logic for time-based aggregations

Additional Resources