# Spark Structured Streaming

> Build streaming pipelines with the same DataFrame API you use for batch: Kafka sources, event-time windows, watermarks, checkpoints and output modes. Free...

Canonical: https://www.internhack.in/roadmaps/tracks/data-engineer/spark-structured-streaming
Last updated: 2026-11-28

Build streaming pipelines with the same DataFrame API you use for batch: Kafka sources, event-time windows, watermarks, checkpoints and output modes.

Advanced · about 8 hours · difficulty 4/5

Learn first: [Apache Kafka Fundamentals](https://www.internhack.in/roadmaps/tracks/data-engineer/kafka), [PySpark DataFrames](https://www.internhack.in/roadmaps/tracks/data-engineer/pyspark-dataframes)

Spark Structured Streaming treats a stream as an unbounded table that keeps growing. You write almost the same DataFrame code as batch, and Spark runs it incrementally, usually in micro-batches. If you already know PySpark, this is the fastest way into streaming.

### Building a stream

- Read: `spark.readStream.format("kafka")` with bootstrap servers and topics; parse the value bytes from JSON or Avro.
- Transform with normal DataFrame operations.
- Write: `writeStream` to Delta, Parquet, Kafka or a `foreachBatch` function for custom sinks such as MERGE into a table.

### Time and state

- `groupBy(window("event_time", "5 minutes"))` for tumbling or sliding windows.
- `withWatermark("event_time", "10 minutes")` bounds state and handles late data.
- Stream-stream joins and deduplication (`dropDuplicatesWithinWatermark`) also rely on watermarks.

### Reliability

- A **checkpoint location** stores offsets and state; never share it between queries or delete it casually.
- **Output modes**: append, update, complete. Not all are valid for every query.
- **Triggers**: default micro-batch, fixed interval, or `availableNow` to process all available data then stop (handy for cheap scheduled "streaming").
- Exactly-once with replayable sources (Kafka) and idempotent or transactional sinks (Delta).

### Monitoring

Watch input rate vs processing rate, batch duration and state size in the Spark UI's Structured Streaming tab.

### Pitfalls

- No watermark on an aggregation, so state grows until the job runs out of memory.
- Changing the query in incompatible ways and reusing the old checkpoint.
- Writing tiny files every few seconds to a lake without compaction.

You are ready to move on when you can stream Kafka order events into a Delta table with 5-minute event-time windows and a watermark, restart the job and show it resumes without duplicates.

## Key concepts

- **readStream and writeStream**
- **Kafka source**
- **Event-time windows**
- **Watermarks**
- **Checkpoints**
- **Output modes**
- **Triggers and availableNow**
- **foreachBatch and MERGE**
- **Stream-stream joins**

## Other options for this step

- [Apache Flink Basics](https://www.internhack.in/roadmaps/tracks/data-engineer/flink): Learn Apache Flink, the true event-at-a-time stream processor: DataStream and Table APIs, Flink SQL, state, checkpoints and event-time processing.

## Mini project

Build a pipeline that reads simulated delivery events from Kafka, computes per-city orders per 5-minute window with a watermark, and writes to a Delta table. Kill and restart the job and verify counts are not duplicated.

## Check yourself

What happens to the state of a windowed aggregation if you do not set a watermark?

## Free resources

- [Structured Streaming programming guide](https://spark.apache.org/docs/latest/structured-streaming-programming-guide.html) (Apache Spark)
- [Structured Streaming and Kafka integration](https://spark.apache.org/docs/latest/structured-streaming-kafka-integration.html) (Apache Spark)
- [Delta Lake documentation](https://docs.delta.io/) (Delta Lake)

Previous: [Kafka Connect and Schema Registry](https://www.internhack.in/roadmaps/tracks/data-engineer/kafka-ecosystem) · Next: [Apache Flink Basics](https://www.internhack.in/roadmaps/tracks/data-engineer/flink)

## More in Streaming Data

- [Stream Processing Concepts](https://www.internhack.in/roadmaps/tracks/data-engineer/streaming-concepts)
- [Apache Kafka Fundamentals](https://www.internhack.in/roadmaps/tracks/data-engineer/kafka)
- [Kafka Connect and Schema Registry](https://www.internhack.in/roadmaps/tracks/data-engineer/kafka-ecosystem)
- [Spark Structured Streaming](https://www.internhack.in/roadmaps/tracks/data-engineer/spark-structured-streaming)
- [Apache Flink Basics](https://www.internhack.in/roadmaps/tracks/data-engineer/flink)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Data Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/data-engineer)
