Structured Streaming with PySpark (Micro-Batching, Triggers, Checkpointing)
Build streaming pipelines with micro-batches, triggers, and fault-tolerant checkpoints.
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
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.
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.
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.
How it works, step by step
Step 1 of 41. 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.
- 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
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.
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.
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?
Knowledge check
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.