Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Timing is Everything: Understanding Event-Time ...

Sharon Xie
September 19, 2024
62

Timing is Everything: Understanding Event-Time Processing in Flink SQL (Current 24)

In the stream processing context, event-time processing means the events are processed based on when the events occurred, rather than when the events are observed (processing-time) in the system. Apache Flink has a powerful framework for event-time processing, which plays a pivotal role in ensuring temporal order and result accuracy.

In this talk, we will introduce Flink event-time semantics and demonstrate how watermarks as a means of handling late-arriving events are generated, propagated, and triggered using Flink SQL. We will explore operators such as window and join that are often used with event time processing, and how different configurations can impact the processing speed, cost and correctness.

Join us for this exploration where event-time theory meets practical SQL implementation, providing you with the tools to make informed decisions for making optimal trade-offs.

Sharon Xie

September 19, 2024
Tweet

Transcript

  1. Timing is Everything Understanding Event-Time Processing in Flink SQL Sharon

    Xie,Head of Product Founding Engineer @ Decodable
  2. What is Apache Flink • Highly scalable stream processing engine

    • Exactly-once processing semantics • Layered APIs: SQL (easy to use) ↔ Java/Python DataStream (expressive) • Support event-driven applications, streaming ETL pipelines, streaming analytics
  3. Time in Flink Event Time • The time at which

    the event happened Processing Time • The time at which the event is observed by Flink
  4. When is event time used? • Decisions or insights based

    on when the event happens ◦ Monitoring and alerting ◦ Time-based compute or analytics
  5. Event time vs Processing Time • Event time is <

    processing time • The lag is arbitrary • Events can be out-of-order
  6. Challenges How do you know when all of the events

    are received for a particular window?
  7. Watermark • Measures the progress of event time • Tracks

    the maximum event time seen • Indicates the completeness of the event time
  8. Create table sensors ( id bigint, `value` integer _time timestamp(3),

    watermark for _time as _time - interval '3' minutes ) WITH ( 'scan.watermark.emit.strategy'='on-event', ... ); Define Watermark
  9. There is a window that ends at 105. When can

    the window close? Quiz - Answer
  10. Idle source/partition • If a partition is idle (no events),

    the watermark will not advance • No result will be produced • Solutions ◦ Configure source idle timeout ▪ set table.exec.source.idle-timeout = 1m ◦ Balance the partitions
  11. Implications • Tradeoff between Correctness and Latency • Latency ◦

    Results of a window is only seen after the window closes • Correctness ◦ Late arriving events are discarded after the window is closed
  12. But…can I have both? • Yes! Flink can process &

    emit “updatesˮ (changelog) • No watermark is needed • Downstream system must support “updatesˮ • Itʼs costly - need to store global state
  13. Quick Summary • Event time is the time when the

    event happens • Flink uses watermark to account for out-of-order events • Watermark strategy allows trade-off between accuracy and latency
  14. Flink SQL Window TVF • TVF  TableValued Function •

    Returns a new relation with all columns of original stream and additional 3 columns: ◦ window_start, window_end, window_time
  15. 1st window_start value Easy calculation: The nearest multiple of window

    size before 1st event time Formula: floor( 1st event time - reference time*) / window size ) * window size + window_offsets * reference time is 19700101T000000.000 https://nightlies.apache.org/flink/flink-docs-release-1.19/docs/dev/table/sql/queries/window-tvf/#wi ndow-offset
  16. Quiz Whatʼs the fixed window start value for the event

    time 101 with window size of 5min?
  17. Quiz - Answer Whatʼs the fixed window start value for

    the event time 101 with window size of 5min? • 100
  18. Window Types - Cumulative Ref: https://nightlies.apache.org/flink/flink-docs-release-1.18/docs/dev/table/sql/queries/window-tvf/ • Similar to tumble

    window, but with early firing at the defined interval • Defined by max window size and window step
  19. Window Types - Session A new window is created when

    the consecutive event time > session gap Ref: https://nightlies.apache.org/flink/flink-docs-release-1.19/docs/dev/table/sql/queries/window-tvf/#session
  20. Window Join • A window join adds the dimension of

    time into the join criteria themselves. • Use case: compute click-through events
  21. Temporal Join • Enrich a stream with the value of

    the joined record at the event time. • Example: Continuously computing the price for each order based on the exchange rate happened when the order is placed
  22. Summary • Event time is essential for timely response and

    analytics • Watermark and windowing are the key concepts • Flink SQL simplifies event time processing
  23. Decodable Talks at Current 2024 Timing is Everything: Understanding Event-Time

    Processing in Flink SQL 🗣 Sharon Xie 📆 Tuesday 4pm 🗺 Ballroom F Data Contracts In Practice With Debezium and Apache Flink 🗣 Gunnar Morling 📆 Tuesday 3pm 🗺 Meeting Room 18C So You Want to Write a User-Defined Function (UDF) for Flink? 🗣 Hans-Peter Grahsl 📆 Wednesday 1:30pm 🗺 Ballroom F The Joy of JARs (and Other Flink SQL Troubleshooting Tales) 🗣 Robin Moffatt 📆 Wednesday 3pm 🗺 Ballroom F