ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Real-Time Data Pipelines with Apache Kafka and Flink
Chapter 1: Stream Processing Architectures and Semantics
Evolution of Distributed Logs
Lambda versus Kappa Architecture
Processing Guarantees and Semantics
Event Time versus Processing Time
Hands-on Practical: Designing a Kappa Pipeline
Chapter 2: Advanced Kafka Producer and Consumer Internals
Replication Protocols and ISRs
Idempotent Producers and Transactions
Custom Partitioning Strategies
Consumer Group Rebalancing protocols
Hands-on Practical: Implementing Transactional Writes
Chapter 3: Flink State Management and Checkpointing
State Backends: HashMap versus RocksDB
Asynchronous Barrier Snapshots
Incremental Checkpointing
State Schema Evolution
Hands-on Practical: State Migration with Savepoints
Chapter 4: Advanced Windowing and Time Attributes
Watermark Generation Strategies
Handling Late Data and Side Outputs
Custom Window Triggers and Evictors
Session Windows and Gap Analysis
Hands-on Practical: Building a Custom Trigger
Chapter 5: Low-Level Operations with ProcessFunctions
The ProcessFunction Hierarchy
Timer Services and Event Scheduling
Broadcast State Pattern
Async I/O for External Lookups
Hands-on Practical: Dynamic Rule Evaluation
Chapter 6: Production Deployment and Reliability
Serialization with Avro and Protobuf
Schema Registry Integration
Kafka Connect for Sink and Source
Failure Recovery Strategies
Hands-on Practical: Schema Evolution in Flight
Chapter 7: Performance Tuning and Monitoring
Identifying Backpressure
Memory Management and Slot Allocation
Tuning RocksDB Performance
Kafka Consumer Lag Analysis
Hands-on Practical: Resolving Skewed Data
Chapter 8: Real-Time AI and Feature Engineering
Online Feature Generation
Model Serving Patterns in Streams
Request-Response over Async Streams
Feature Store Integration
Hands-on Practical: Real-Time Inference Pipeline

Handling Late Data and Side Outputs

Watermarks provide a metric for completeness in stream processing, but they function as a heuristic rather than a guarantee. Strictly enforcing watermark boundaries in distributed systems with variable network latency often leads to data loss. An event occurring at t=100t=100t=100 might arrive at the operator when the watermark has already advanced to W(105)W(105)W(105). If the system rigidly closes windows based solely on watermarks, this late-arriving event is discarded, compromising the accuracy of the computed aggregate.

For production-grade pipelines, specifically those driving financial dashboards or fraud detection systems, dropping data is rarely an option. Flink addresses this by offering a tiered approach to late data: allowing a grace period for late events to update existing windows, and routing data that exceeds this grace period to a secondary channel known as a Side Output.

The Lateness Lifecycle

Understanding how Flink handles lateness requires analyzing the lifecycle of a Window Operator. By default, a window is created when the first element belonging to that time slot arrives. The window remains active until the watermark passes the window's end timestamp. At this precise moment, the configured Trigger fires, the aggregate is emitted, and the window state is purged to free up memory.

When we introduce "Allowed Lateness," we alter the termination condition of the window. The window state is not purged immediately when the watermark passes the window end. Instead, Flink retains the state (in RocksDB or Heap) for a specified duration.

The lifecycle transforms into three distinct phases:

  1. Accumulation: Events arrive before the watermark passes the window end. The window accumulates state normally.
  2. Lateness (Grace Period): The watermark has passed the window end, but has not yet exceeded the allowed lateness threshold. Events arriving during this phase trigger a re-computation of the window.
  3. Purge: The watermark exceeds the window end plus the allowed lateness. The state is destroyed. Any subsequent events are "late" and are either dropped or directed to a side output.

We can visualize the decision logic for an incoming event relative to the current watermark and window configuration below.

Decision tree for handling events based on watermark progression and lateness configuration.

Managing State Updates with Allowed Lateness

Configuring allowed lateness is straightforward in the DataStream API, but the implications on downstream consumers are significant. When late data arrives within the grace period, Flink must emit an updated result for a window that was previously marked as "closed."

OutputTag<Event> lateTag = new OutputTag<Event>("late-data"){};

DataStream<Result> stream = input
    .keyBy(Event::getId)
    .window(TumblingEventTimeWindows.of(Time.minutes(10)))
    .allowedLateness(Time.minutes(5)) // Retain state for 5 minutes past window end
    .sideOutputLateData(lateTag)      // Route extremely late data here
    .process(new AggregateProcessFunction());

In this configuration, if a window covers [12:00,12:10)[12:00, 12:10)[12:00,12:10), the primary result is emitted when the watermark passes 12:1012:1012:10. However, the state remains in the backend. If an event with timestamp 12:0812:0812:08 arrives when the watermark is at 12:1212:1212:12, Flink retrieves the window state, updates the aggregate, and fires the trigger again.

This results in a stream containing multiple results for the same window. The first result is an approximation; subsequent results are refinements. This behavior fundamentally changes the requirements for the sink operator. The downstream database or consumer must be idempotent or capable of handling "upserts" (updates/inserts).

If the downstream system expects an append-only stream (like writing to a standard Kafka topic or HDFS file), this configuration will result in duplicate or conflicting data. You must design the pipeline such that the downstream system treats the window start time as a primary key, allowing the new value to overwrite the old one.

Side Outputs for Extremes

Allowed lateness incurs a cost: memory. Retaining state for windows that have closed increases the footprint of your state backend. In high-throughput scenarios with RocksDB, keeping windows open for hours or days is often impractical.

For events that arrive after the allowed lateness period has expired, often caused by long-term network partitions or mobile devices coming back online after days, Side Outputs provide a mechanism to capture data without maintaining window state.

Side outputs capture the raw event stream of dropped data. This data effectively bypasses the main windowing logic. A common architectural pattern involves routing this side output to a "cold storage" path, such as an S3 bucket or a separate Kafka topic. A batch process can later merge this late data with the real-time aggregates to achieve eventual consistency.

Accessing the side output stream requires using the getSideOutput method on the result of the windowed operation:

SingleOutputStreamOperator<Result> mainStream = ...; // Window operation

DataStream<Event> lateStream = mainStream.getSideOutput(lateTag);

lateStream.addSink(new FlinkKafkaProducer<>(
    "late-events-topic",
    new EventSchema(),
    properties
));

Using an OutputTag is necessary because generic types are erased at runtime in Java. The anonymous inner class {} in the OutputTag declaration ensures that type information is retained, allowing Flink to correctly serialize and route the side output events.

Impact on Watermark Progression

It is critically important to understand that handling late data does not halt or revert the watermark. The watermark is a monotonically increasing value. Processing a late event with timestamp t=100t=100t=100 when the watermark is W(200)W(200)W(200) does not move the watermark back to 100.

This separation ensures that the pipeline continues to process real-time data with low latency, even while it accommodates stragglers. The trade-off is purely in state size and the complexity of downstream handling.

Implementation Strategy: Accumulating vs. Retracting

When triggers fire multiple times due to late data, the ProcessWindowFunction or WindowFunction determines strictly what is emitted. However, if you are using Flink SQL or the Table API, the system manages this through internal changelogs.

In the DataStream API, the developer controls the emission strategy. You generally have two choices:

  1. Accumulating: The window state stores the running total. Each firing emits the new total. (e.g., First emission: 10, Late update emission: 11). This requires the sink to perform an overwrite.
  2. Delta/Retracting: The operator emits the difference. This is complex to implement manually in DataStream API but is standard in Flink SQL. (e.g., First emission: +10. Late update: -10, +11).

For most DataStream applications involving KeyedWindows, the Accumulating strategy combined with an upsert-capable sink (like Redis, Cassandra, or JDBC with upsert support) is the standard pattern for reconciling late data.

The chart below compares the timeline of events for a standard window versus one with allowed lateness.

Progression of watermarks relative to window closure and late event arrival.

The configuration of allowedLateness and sideOutputLateData provides a safety net. It allows the pipeline to prioritize low latency for the majority of data while maintaining a mechanism to handle consistency for delayed information eventually. By directing the furthest outliers to side outputs, we protect the real-time state from indefinite growth while ensuring no data is strictly lost to the ether.

  • Event Time and Watermarks, The Apache Flink Community, 2023 (The Apache Flink Community) - Official documentation explaining Flink's event time processing model, including watermarks and their role in stream completeness.
  • Windows, The Apache Flink Community, 2023 - Detailed official documentation on Flink's windowing mechanisms, specifically covering allowed lateness and side outputs for handling late data.
  • Streaming Systems: The What, Where, When, and How of Large-Scale Data Processing, Tyler Akidau, Slava Chernyak, Reuven Lax, 2018 (O'Reilly Media) - A foundational text on stream processing, providing theoretical and practical understanding of event time, watermarks, and consistency models essential for handling late data in distributed systems.

© 2026 ApX Machine Learning