Event time and watermarks

Flink can work with the time when an event was created, which is the event time, or with the time when Flink processes it. Watermarks tell Flink how far event time has progressed, so that Flink knows when it has all events for a time period.

Event time and processing time

  • Event time is the time when the event was created. The time is already in the record before it enters Flink, for example in a timestamp column.
  • Processing time is the time at which an operator processes the event. It uses the clock of the machine.

Use event time for alerting and time-sensitive results, because the results then do not depend on delays in the pipeline. If timeliness between input sources matters, write the original timestamp of the event into the event itself.

Watermarks

With event time, the timestamp of a record shows only when the event was created. It does not show how far the processing has progressed. A watermark measures the progress of event time. It tells an operator that no events with an earlier timestamp are expected.

For example, a window aggregates the data between 10:00 and 11:00. When the watermark reaches 11:00, Flink knows that the events up to 11:00 have arrived, and it produces the result for the window.

In SQL, you define the event time column and the watermark in the table definition, with a WATERMARK clause:

CREATE TABLE orders (
  order_id STRING,
  amount DOUBLE,
  order_time TIMESTAMP(3),
  WATERMARK FOR order_time AS order_time - INTERVAL '5' SECOND
) WITH (
  'connector' = 'kafka'
  -- more connector properties
);

In this example, Flink waits up to 5 seconds for events that arrive late. For the options for Kafka tables, see Defining event time for Kafka tables.

For more information, see Timely stream processing in the Apache Flink documentation.