Enterprise Cloud Architecture, Zero Trust Cybersecurity, LLMOps & High-Scale Systems

Data Engineering

Real-Time Streaming ETL with Apache Flink and Kafka: Windowing, Watermarks, and State Backends

November 3, 2026 ยท By writer

technology.efektifpro.com โ€“ While batch processing tools like Spark and dbt excel at daily analytical rollups, modern fraud detection, dynamic pricing, and real-time inventory systems require sub-second event processing. Apache Flink has emerged as the premier stream-processing engine for stateful, low-latency event computations, offering true event-driven micro-pipelining, sophisticated event-time watermarking, and RocksDB state backends that handle terabytes of state per operator.

Core Concept: Event Time vs Processing Time vs Watermarks

Streaming systems handle three distinct time notions. Because network latency and mobile connectivity cause events to arrive out of order, robust stream processing must compute over Event Time (when the event actually occurred at the device) rather than Processing Time (when the server happened to receive it):

  • Event Time: Timestamp embedded inside the message payload.
  • Watermark: A metric asserting that no further events with timestamp < T will arrive, allowing the engine to safely fire window computations.
  • Allowed Lateness: Grace period permitting late-arriving events to update previously closed windows before triggering final state purging.

PyFlink Pipeline: 5-Minute Sliding Window Aggregation

The following PyFlink job consumes transaction events from Kafka, parses timestamps, applies bounded out-of-orderness watermarks, and computes sliding window volume aggregations:

# PyFlink streaming job aggregating transaction metrics in real time
from pyflink.datastream import StreamExecutionEnvironment
from pyflink.table import StreamTableEnvironment, EnvironmentSettings

env = StreamExecutionEnvironment.get_execution_environment()
env.enable_checkpointing(60000)  # Checkpoint state every 60s for exactly-once recovery
t_env = StreamTableEnvironment.create(env)

# Define Kafka source table with event time watermark definition
t_env.execute_sql('''
    CREATE TABLE kafka_transactions (
        transaction_id  STRING,
        account_id      STRING,
        amount          DOUBLE,
        currency        STRING,
        event_time      TIMESTAMP(3),
        WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND
    ) WITH (
        'connector' = 'kafka',
        'topic' = 'financial-transactions',
        'properties.bootstrap.servers' = 'kafka:9092',
        'properties.group.id' = 'flink-fraud-detector',
        'format' = 'json',
        'scan.startup.mode' = 'latest-offset'
    )
''')

# Define sink table writing aggregated metrics to Apache Iceberg / PostgreSQL
t_env.execute_sql('''
    CREATE TABLE high_volume_alerts (
        account_id      STRING,
        window_end      TIMESTAMP(3),
        total_amount    DOUBLE,
        tx_count        BIGINT
    ) WITH (
        'connector' = 'print'  -- Or iceberg, jdbc, elasticsearch
    )
''')

# Run 5-minute sliding window with 1-minute slide
t_env.execute_sql('''
    INSERT INTO high_volume_alerts
    SELECT
        account_id,
        TUMBLE_END(event_time, INTERVAL '5' MINUTE) AS window_end,
        SUM(amount) AS total_amount,
        COUNT(transaction_id) AS tx_count
    FROM kafka_transactions
    GROUP BY
        account_id,
        TUMBLE(event_time, INTERVAL '5' MINUTE)
    HAVING SUM(amount) > 100000
''')

State Backends: Memory vs RocksDB

FeatureHashMap State BackendEmbeddedRocksDBStateBackend
State StorageJVM Heap MemoryDisk (Local NVMe SSD) + Block Cache
Max State CapacityLimited by JVM Heap (GBs)Limited only by disk size (TBs per node)
Access SpeedUltra-fast memory readsFast (with SSD block cache hit)
Recommended ForLow-latency, stateless or small state (<10GB)Production enterprise with large historical windows

Exactly-Once Processing Guarantees

Flink achieves end-to-end exactly-once processing via the Chandy-Lamport distributed snapshotting algorithm. Periodic checkpoints record operator state and Kafka consumer offsets simultaneously. When writing to sinks that support two-phase commit (2PC) like Kafka or Iceberg, transactions are committed only when the Flink checkpoint successfully writes to durable storage (S3/GCS).

๐Ÿท๏ธ Topics: