Skip to the page
Tecyfy

PySpark interview question · hard

What is Spark Streaming, and how does it differ from Spark Batch?

The short answerSpark Streaming processes data continuously as it arrives, while Spark batch processing runs over a fixed, already-complete dataset and then stops.

The full answer

Spark Streaming processes data continuously as it arrives, while Spark batch processing runs over a fixed, already-complete dataset and then stops. "Spark Streaming" originally meant the DStream API, which is now a legacy project with no further updates; modern Spark streaming uses Structured Streaming, which runs streams with the same DataFrame and SQL API as batch.

Batch

  • Input is bounded: a set of files or a table snapshot.
  • A job reads everything, computes and finishes.
  • Latency: minutes to hours, depending on schedule.
  • Typical uses: nightly loads, reports, model training, backfills.

Streaming

  • Input is unbounded: Kafka topics, event hubs, files landing in a folder, change feeds from Delta tables.
  • A query keeps running, processing new data in small increments (micro-batches, by default as soon as the previous one finishes, or on a fixed interval).
  • Latency: typically from under a second to a few minutes.
  • Needs state for aggregations and joins over time, event-time handling for late data, and checkpointing for recovery.
  • Typical uses: real-time dashboards, alerting, fraud detection, continuous ingestion into a lakehouse.

Structured Streaming in one example

from pyspark.sql import functions as F

events = (spark.readStream.format("kafka")
            .option("kafka.bootstrap.servers", "broker:9092")
            .option("subscribe", "clicks").load()
            .select(F.from_json(F.col("value").cast("string"), click_schema).alias("c"))
            .select("c.*"))

per_minute = (events
    .withWatermark("event_time", "10 minutes")
    .groupBy(F.window("event_time", "1 minute"), "page")
    .count())

(per_minute.writeStream
    .format("delta")
    .outputMode("append")
    .option("checkpointLocation", "/chk/clicks_per_minute")
    .toTable("gold.clicks_per_minute"))

The transformation code is the same as a batch query; only readStream and writeStream differ.

Key differences in practice

BatchStreaming
InputBoundedUnbounded
RunStarts and endsRuns continuously (or incrementally on a schedule)
RecoveryRe-run the jobResume from the checkpoint with exactly-once results
StateNot needed between runsKept for aggregations, joins and deduplication
TimeData is completeLate and out-of-order data; watermarks decide how long to wait
CostPay while the job runsPay while the stream runs

A useful middle ground trigger(availableNow=True) runs a streaming query that processes everything new since the last run and then stops. You get streaming's incremental processing and checkpointed progress with batch-style scheduling, which is how many ingestion pipelines run today.

Last updated 16 October 2026