Structured Streaming
Watermarking
Exactly-once Semantics
Data Processing
Real-time Analytics

Structured streaming watermark vs. exactly-once semantics

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Structured streaming is a scalable and fault-tolerant stream processing engine built on the Spark SQL engine. It's designed to handle complex operations like aggregations, joins, and window functions on streaming data. Structured streaming provides a high-level abstraction called a DataFrame that represents a micro-batch of data to process streams as a series of small, stateful, incremental batch jobs. Two critical features that enhance the robustness of stream processing in Spark are watermarks and exactly-once semantics. Understanding these concepts is key to developing resilient streaming applications.

Watermarking in Structured Streaming

A watermark is a tool that allows the Spark engine to track and handle late-arriving data in streaming applications. It's a threshold to specify how late the data is expected to be, helping manage state and minimize the resource consumption used for stateful operations.

How Watermarks Work

Watermarks track a point in time before which the application will not expect more data. This is crucial for time window-based aggregations and stateful operations, wherein late data might alter the results of computations performed on earlier data.

For example, consider a streaming application that processes logs of events that are windowed over 10 minutes to compute counts of event occurrences. Without a watermark, the application would need to maintain state indefinitely, lest late data pertinent to previous windows arrives. A watermark set to 5 minutes would mean the system expects data to be delayed by no more than 5 minutes. Data delayed beyond this period would be dropped.

Example of Setting a Watermark:

scala
1import spark.implicits._
2
3val events = spark
4  .readStream
5  .schema(...)
6  .json(...)
7  .withWatermark("eventTime", "5 minutes")  // eventTime is the timestamp column
8  .groupBy(
9    window($"eventTime", "10 minutes", "5 minutes"), $"eventType"
10  )
11  .count()

Exactly-Once Semantics

Exactly-once semantics ensure that each record in a streaming application is processed exactly once, i.e., even in the event of failures or retries, no duplicates are processed or generated. This is crucial for applications where accuracy and data integrity are important, such as in financial transactions.

Implementation in Spark Structured Streaming

Spark achieves exactly-once semantics through a combination of checkpointing and write-ahead logs. When a streaming job starts, Spark logs the record offsets being processed to a distributed file system. After successful processing, the offsets are committed. If a failure occurs, Spark will replay the logs from the last committed offset.

Example of Enabling Checkpointing:

scala
1val query = events
2  .writeStream
3  .outputMode("complete")
4  .option("checkpointLocation", "/path/to/checkpoint/dir")
5  .start()

Comparison Table

Here's a comparison table that encapsulates the key differences and purposes of watermarks and exactly-once semantics in Spark structured streaming:

FeatureWatermarkingExactly-Once Semantics
PurposeHandles late-arriving dataEnsures no duplicated processing of data
Use CaseTime-window aggregations, Stateful EventsFinancial transactions, Critical state changes
ImplementationThrough a time thresholdCheckpointing and write-ahead logs
Impact on ResultsCan drop data based on time policyProvides accurate and consistent results

Further Considerations

  • Trade-offs: Employing watermarks can lead to a trade-off between latency and accuracy due to the handling of late data. Exactly-once, meanwhile, can impact system performance due to the overhead of checkpointing.
  • Configuration: Both features require proper configuration to balance system performance and fault tolerance. Setting up watermarks too aggressively might lead to loss of data, while too lenient could use excessive resources.

Conclusion

Both watermarks and exactly-once semantics are fundamental for enhancing the reliability and accuracy of stream processing in Spark Structured Streaming. Proper understanding and implementation of these features can greatly improve the performance and resilience of your streaming applications, ensuring that your data processing workflows are both efficient and robust.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions