Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 77/ 160
04. Big Data Processing & Apache Spark

Structured Streaming with PySpark (Micro-Batching, Triggers, Checkpointing)

Build streaming pipelines with micro-batches, triggers, and fault-tolerant checkpoints.

AdvancedMicro-Batch · Triggers · Checkpoint3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Kafka/files with durable source offsets. Next, bounded micro-batch DataFrame operations. Finally, trigger cadence, output mode, and idempotent publication.

Think of it like…

Structured Streaming is a marked place in an endless book: every micro-batch reads a bounded page range, writes notes, and moves the bookmark only after the write commits.

Where you’ve seen it

readStream.format('kafka').option('subscribe','orders').load(); writeStream.outputMode('append').option('checkpointLocation','s3://chk/order-v3').trigger(processingTime='30 seconds').toTable(...) preserves the exact sink progress.

Words you’ll hear

Micro-Batch
Small source-offset ranges processed as successive triggers rather than one permanent continuously executing function per event.
Triggers
Trigger scheduling such as processing-time interval, available-now, or one batch at a time, not a guarantee of source latency.
Checkpoint
A persistent directory containing source offsets, sink commits, metadata, and state versions for recovery.
Operational boundary
Reusing or deleting a checkpoint can make offsets restart or be incompatible, append queries can silently duplicate without idempotent sinks, and trigger intervals that exceed batch processing time create growing backlog. Give each logical query a stable checkpoint path, use source-specific Kafka/file offsets, test upgrades and state compatibility, choose available-now or interval triggers from SLA, and write sinks with atomic transaction or dedupe keys.

Visual diagram

Loading interactive diagram

Theory & deep dive

Micro-Batch: the working mechanism

Spark Structured Streaming repeatedly treats unbounded data as incremental DataFrames, maintains offsets and state in a checkpoint, and publishes each trigger according to a trigger and output mode.

Kafka/files with durable source offsets. Bounded micro-batch DataFrame operations, then offsets, commits, and state store; this exposes the state governed by Checkpoint.

Structured Streaming with PySpark in one concrete design

readStream.format('kafka').option('subscribe','orders').load(); writeStream.outputMode('append').option('checkpointLocation','s3://chk/order-v3').trigger(processingTime='30 seconds').toTable(...) preserves the exact sink progress.

Key takeaway: Spark Structured Streaming repeatedly treats unbounded data as incremental DataFrames, maintains offsets and state in a checkpoint, and publishes each trigger according to a trigger and output mode

Structured Streaming with PySpark: failure mode and production response

Reusing or deleting a checkpoint can make offsets restart or be incompatible, append queries can silently duplicate without idempotent sinks, and trigger intervals that exceed batch processing time create growing backlog.

Give each logical query a stable checkpoint path, use source-specific Kafka/file offsets, test upgrades and state compatibility, choose available-now or interval triggers from SLA, and write sinks with atomic transaction or dedupe keys. Track Input rows per second as "compare to processing throughput at steady state", and make Batch duration visible to the dataset owner.

Key takeaway: Give each logical query a stable checkpoint path, use source-specific Kafka/file offsets, test upgrades and state compatibility, choose available-now or interval triggers from SLA, and write sinks with atomic transaction or dedupe keys.

How it works, step by step

Step 1 of 4
Micro-Batch

1. Read

Kafka/files with durable source offsets. Spark Structured Streaming repeatedly treats unbounded data as incremental DataFrames, maintains offsets and state in a checkpoint, and publishes each trigger according to a trigger and output mode.

Kafka/files with durable source offsets
Key numbers at this step
Input rows per second
compare to processing throughput at steady state
Triggers
Trigger scheduling such as processing-time interval, available-now, or one batch at a time, not a guarantee of source latency.

When to use it & trade-offs

What it is

Spark Structured Streaming repeatedly treats unbounded data as incremental DataFrames, maintains offsets and state in a checkpoint, and publishes each trigger according to a trigger and output mode. readStream.format('kafka').option('subscribe','orders').load(); writeStream.outputMode('append').option('checkpointLocation','s3://chk/order-v3').trigger(processingTime='30 seconds').toTable(...) preserves the exact sink progress.

Why it matters: Reusing or deleting a checkpoint can make offsets restart or be incompatible, append queries can silently duplicate without idempotent sinks, and trigger intervals that exceed batch processing time create growing backlog. Give each logical query a stable checkpoint path, use source-specific Kafka/file offsets, test upgrades and state compatibility, choose available-now or interval triggers from SLA, and write sinks with atomic transaction or dedupe keys.

When to reach for it

  • Build streaming pipelines with micro-batches, triggers, and fault-tolerant checkpoints.
  • Choose Micro-Batch because it provides this capability: Small source-offset ranges processed as successive triggers rather than one permanent continuously executing function per event.
  • Keep evidence for Input rows per second at "compare to processing throughput at steady state" so the last production stage-Sink - trigger cadence, output mode, and idempotent publication-remains testable.

When to avoid or be careful

  • Reusing or deleting a checkpoint can make offsets restart or be incompatible, append queries can silently duplicate without idempotent sinks, and trigger intervals that exceed batch processing time create growing backlog.
  • Do not conflate Triggers with Micro-Batch. The second concept contributes different behavior: Trigger scheduling such as processing-time interval, available-now, or one batch at a time, not a guarantee of source latency.
  • Release only after this guardrail is in place: Give each logical query a stable checkpoint path, use source-specific Kafka/file offsets, test upgrades and state compatibility, choose available-now or interval triggers from SLA, and write sinks with atomic transaction or dedupe keys.

Micro-Batch / Triggers / Checkpoint - roles, limits, and operating cost

Micro-Batch
Best for: Small source-offset ranges processed as successive triggers rather than one permanent continuously executing function per event.

Strengths: Small source-offset ranges processed as successive triggers rather than one permanent continuously executing function per event. Spark Structured Streaming repeatedly treats unbounded data as incremental DataFrames, maintains offsets and state in a checkpoint, and publishes each trigger according to a trigger and output mode.

Watch out: Reusing or deleting a checkpoint can make offsets restart or be incompatible, append queries can silently duplicate without idempotent sinks, and trigger intervals that exceed batch processing time create growing backlog.

Triggers
Best for: Trigger scheduling such as processing-time interval, available-now, or one batch at a time, not a guarantee of source latency.

Strengths: Trigger scheduling such as processing-time interval, available-now, or one batch at a time, not a guarantee of source latency. In this workflow it supports: State/checkpoint - offsets, commits, and state store.

Watch out: The operating cost is real: must remain below fixed trigger interval to avoid lag.

Checkpoint
Best for: A persistent directory containing source offsets, sink commits, metadata, and state versions for recovery.

Strengths: A persistent directory containing source offsets, sink commits, metadata, and state versions for recovery. Its output can be checked with Checkpoint version.

Watch out: Operating guardrail: Give each logical query a stable checkpoint path, use source-specific Kafka/file offsets, test upgrades and state compatibility, choose available-now or interval triggers from SLA, and write sinks with atomic transaction or dedupe keys.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Micro-Batch, and how would Input rows per second reveal it?”
  • “Why is Triggers distinct from this behavior: a persistent directory containing source offsets, sink commits, metadata, and state versions for recovery?”
  • Production clue: Reusing or deleting a checkpoint can make offsets restart or be incompatible, append queries can silently duplicate without idempotent sinks, and trigger intervals that exceed batch processing time create growing backlog.
  • Design probe: Give each logical query a stable checkpoint path, use source-specific Kafka/file offsets, test upgrades and state compatibility, choose available-now or interval triggers from SLA, and write sinks with atomic transaction or dedupe keys.

Benchmarks & rules of thumb

Input rows per second -
compare to processing throughput at steady state
Batch duration -
must remain below fixed trigger interval to avoid lag
Checkpoint version -
query logic/source changes need compatibility validation

Common mistakes & follow-ups

Common interview mistakes

  • Reusing or deleting a checkpoint can make offsets restart or be incompatible, append queries can silently duplicate without idempotent sinks, and trigger intervals that exceed batch processing time create growing backlog.
  • Reporting success from a job exit code without checking Input rows per second and Batch duration.
  • Treating this example as complete without replay, rollback, or access boundaries: readStream.format('kafka').option('subscribe','orders').load(); writeStream.outputMode('append').option('checkpointLocation','s3://chk/order-v3').trigger(processingTime='30 seconds').toTable(...) preserves the exact sink progress.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: Small source-offset ranges processed as successive triggers rather than one permanent continuously executing function per event.
  • What does the recovery plan do when this occurs? Reusing or deleting a checkpoint can make offsets restart or be incompatible, append queries can silently duplicate without idempotent sinks, and trigger intervals that exceed batch processing time create growing backlog.
  • When is Micro-Batch insufficient for Batch duration, and what does adding Triggers cost in Input rows per second?
Interview tip: Lead with the data path and failure boundary: Spark Structured Streaming repeatedly treats unbounded data as incremental DataFrames, maintains offsets and state in a checkpoint, and publishes each trigger according to a trigger and output mode. Then make the answer operational: Give each logical query a stable checkpoint path, use source-specific Kafka/file offsets, test upgrades and state compatibility, choose available-now or interval triggers from SLA, and write sinks with atomic transaction or dedupe keys.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Structured Streaming with PySpark (Micro-Batching, Triggers, Checkpointing) with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Reading Spark UI (DAG Visualization, Spill to Disk, GC Time, Shuffle Read/Write)
Next topic
Watermarking, Event-Time vs Processing-Time & Stateful Streaming