Structured streaming : watermark vs. exactly-once semantics

Viewed 3372

The programming guide says that structured streaming guarantees end-to-end exactly once semantics using appropriate sources/sinks.

However I'm not understanding how this works when the job crashes and we have a watermark applied.

Below is an example of how I currently imagine it working, please correct me on any points that I'm misunderstanding. Thanks in advance!

Example:

Spark Job: Count # events in each 1 hour window, with a 1 hour Watermark.

Messages:

  • A - timestamp 10am
  • B - timestamp 10:10am
  • C - timestamp 10:20am
  • X - timestamp 12pm
  • Y - timestamp 12:50pm
  • Z - timestamp 8pm

We start the job, read A, B, C from the Source and the job crashes at 10:30am before we've written them out to our Sink.

At 6pm the job comes back up and knows to re-process A, B, C using the saved checkpoint/WAL. The final count is 3 for the 10-11am window.

Next, it reads the new messages from Kafka, X, Y, Z in parallel since they belong to different partitions. Z is processed first, so the max event timestamp gets set to 8pm. When the job reads X and Y, they are now behind the watermark (8pm - 1 hour = 7pm), so they are discarded as old data. The final count is 1 for 8-9pm, and the job does not report anything for the 12-1pm window. We've lost data for X and Y.

---End example---

Is this scenario accurate? If so, the 1 hour watermark may be sufficient to handle late/out-of-order data when flowing normally from Kafka-Sspark, but not when the spark job goes down/Kafka connection is lost for a long period of time. Would the only option to avoid data loss be to use a watermark longer than you expect the job to ever go down for?

2 Answers
Related