Skip to the page
Tecyfy

PySpark interview question · hard

What is Structured Streaming, and how does it differ from DStreams?

The short answerStructured Streaming is Spark's current streaming engine.

The full answer

Structured Streaming is Spark's current streaming engine. It treats a stream as a table that keeps growing, lets you write the same DataFrame or SQL query you would write for batch data, and runs it incrementally, updating results as new data arrives with exactly-once guarantees. DStreams, the older API, represented a stream as a sequence of RDDs and is now a legacy project.

The model Each new record is a new row appended to an unbounded input table. Your query defines a result table; on each trigger, Spark processes only the new rows, updates any state, and writes changes to the sink according to the output mode (append, update or complete).

from pyspark.sql import functions as F

orders = (spark.readStream.format("delta").table("bronze.orders"))

revenue = (orders
    .withWatermark("order_ts", "30 minutes")
    .groupBy(F.window("order_ts", "5 minutes"), "country")
    .agg(F.sum("amount").alias("revenue")))

(revenue.writeStream
    .outputMode("append")
    .option("checkpointLocation", "/chk/revenue_5min")
    .trigger(processingTime="1 minute")
    .toTable("gold.revenue_5min"))

Differences from DStreams

DStreams (legacy)Structured Streaming
AbstractionSequence of RDDsUnbounded DataFrame or table
APIRDD-style functions, separate from batch codeSame DataFrame and SQL API as batch
OptimisationNoneCatalyst optimizer and Tungsten, like batch
TimeProcessing time (arrival); event time is hardEvent-time windows and watermarks for late data
Exactly-onceDeveloper's responsibilityBuilt in, with replayable sources, checkpoints and idempotent or transactional sinks
StateupdateStateByKey, mapWithStateManaged state for aggregations, joins and deduplication; transformWithState for custom logic; RocksDB state store for large state
TriggersFixed batch intervalMicro-batches as fast as possible, fixed intervals, availableNow (process what's there, then stop), experimental continuous mode
Sources and sinksReceivers and custom codeKafka, files, Delta and other tables, rate source; foreachBatch for any batch sink
StatusNo more updatesActively developed

Why it matters in practice

  • One codebase for batch and streaming logic.
  • Correct results with late data, through watermarks.
  • Fault tolerance and exactly-once writes without hand-written bookkeeping.
  • Incremental pipelines can run continuously or on a schedule with availableNow, the basis of most lakehouse ingestion today (including Databricks Auto Loader and Lakeflow declarative pipelines).

Limitations to mention

  • Some operations aren't supported on streams, such as most sorting outside aggregations, and some outer joins need watermarks.
  • Micro-batch latency is typically from a few hundred milliseconds to seconds; very low latency needs special modes or a different engine.
  • State must be sized and monitored; unbounded state grows forever without watermarks.

Last updated 16 October 2026