Full answer
Event time versus processing time
Event time is when something happened (the click's timestamp). Processing time is when the engine sees it. A phone that was offline for 5 minutes sends old events late. Counting by processing time would put those clicks in the wrong minute and make results change every time you rerun. Event time gives correct, repeatable results.
Windows
- Tumbling: fixed size, no overlap. Clicks per minute: [10:00, 10:01), [10:01, 10:02).
- Sliding (hopping): fixed size, overlapping. Last 5 minutes, updated every minute; each event belongs to 5 windows.
- Session: per key, closes after a gap of inactivity (for example 30 minutes without activity ends a learner's study session). Window lengths vary.
- Global windows with triggers: custom rules such as "emit every 100 events".
Watermarks and late data
A watermark is a statement "no more events with timestamp before T are expected". A common strategy is max event time seen minus a bound (for example 2 minutes). When the watermark passes a window's end, the window fires.
Late events (older than the watermark) are handled by:
- Allowed lateness: keep window state a bit longer and emit an updated result.
- Side outputs: send them to a separate stream for batch correction.
- Dropping: acceptable for some metrics.
The bound is a trade-off: a larger delay catches more late data but makes results slower.
Exactly-once
Exactly-once means each event affects the results once, even after failures (events may still be processed more than once internally).
Flink's approach:
- Periodically inject checkpoint barriers into the streams. When an operator has received the barrier on all inputs, it snapshots its state. The snapshot of all operators plus the source offsets is a consistent checkpoint.
- After a failure, restore every operator's state from the last checkpoint and rewind sources (for example Kafka offsets) to the stored positions.
- Sinks must not double-write the replayed output: use two-phase commit sinks (Kafka transactions committed when the checkpoint completes) or idempotent upserts keyed by (window, key).
Other concepts
- Keyed state is partitioned by key and scales out with the job.
- Backpressure slows sources when operators fall behind.
- Kappa architecture: one streaming pipeline that can reprocess history by replaying the log, instead of separate batch and stream code.
By level
Fresher: Explain that events are grouped into time windows such as one minute, and that some events arrive late. A fresher should explain the difference between when an event happened and when it arrived.
Mid: Describe window types, watermarks, allowed lateness and side outputs, and the checkpoint-based exactly-once mechanism. Give example uses.
Senior: Discuss watermark strategies, session merging, state size and TTLs, sink semantics, backpressure and Kappa-style reprocessing. Expect precise end-to-end guarantees.
Key points
- Use event time for correct, repeatable results
- Tumbling, sliding and session windows serve different questions
- Watermarks decide when windows fire; lateness is a trade-off
- Late data: allowed lateness, side outputs or drop
- Exactly-once: consistent checkpoints, rewindable sources, transactional or idempotent sinks
Common mistakes
- Counting by processing time
- Thinking exactly-once means each event is processed only once internally
- Using a watermark delay of zero and losing late events
- Writing to non-idempotent sinks after replay
Follow-ups
- How would you choose the watermark delay?
- How do session windows merge when a late event bridges two sessions?
- What happens to state size with a 30-day sliding window?
Tips
- Use an offline phone example for event time
- Draw one window and the watermark crossing it
- Explain barriers simply
Evaluation rubric
- Correct time semantics
- Accurate window types
- Watermarks and late data handling
- Correct exactly-once mechanism