Stream processing continuously handles events as they arrive. The difficult part is not simply processing each message quickly: production systems must also deal with delayed and out-of-order events, duplicate delivery, state recovery, uneven traffic, and accurate results after failures.
1. How does stream processing differ from batch processing?
Batch processing works on a bounded dataset, usually on a schedule. Stream processing handles an unbounded sequence of events continuously, updating results as new events arrive.
| Dimension | Batch | Stream |
|---|---|---|
| Input | Bounded files, tables, or time ranges | Continuous events or records |
| Output timing | After scheduled processing completes | Continuously or at short intervals |
| State | Often scoped to a run or partition | May persist across events and checkpoints |
| Challenges | Large backfills, retries, and processing windows | Late events, state growth, backpressure, and recovery |
| Typical use cases | Daily reporting, historical transformations, model training | Fraud detection, live monitoring, event-driven metrics |
Streaming does not automatically mean every result is final or exactly-once. Delivery guarantees depend on the source, processing engine, checkpointing, and sink. Some platforms also use micro-batches internally.
2. What is the difference between event time and processing time?
Event time is when an event actually occurred according to its timestamp. Processing time is when the streaming application processes that event. Systems may also track ingestion time, which is when the platform receives the event.
| Time type | Meaning | Example |
|---|---|---|
| Event time | When the business event occurred | A payment occurred at 10:00 |
| Ingestion time | When the platform received the event | The broker received it at 10:04 |
| Processing time | When the application handled the event | The job processed it at 10:05 |
Use event time when business results should reflect when something happened—for example, hourly sales or device readings. Processing time can be useful when low latency is more important than aligning results to the original event timeline.
Example: A mobile device records an event at 10:00 but remains offline until 10:20. Event-time aggregation assigns it to the 10:00 window; processing-time aggregation assigns it to the window in which it is processed.
3. What are watermarks, and how do they affect late events?
A watermark is a system's estimate that event time has advanced to a point where events earlier than that point are unlikely to arrive, according to the configured policy. It helps a streaming engine decide when to close event-time windows and how long to retain state.
Example: Suppose a pipeline uses a 10-minute allowed-lateness policy. If the watermark advances beyond 10:00, the engine may finalize the 9:00–10:00 window under its configured semantics. An event for 9:45 arriving later may be dropped, sent to a side output, or cause an update if the engine and sink support it.
Why watermarks matter:
- Determine when event-time windows can emit results or be considered complete.
- Control how long state and window metadata must be retained.
- Trade off result latency, state size, and tolerance for late data.
- Help manage event-time progress when events arrive out of order.
event timestamp: 09:45
event arrives: 10:18
current watermark: 10:05
Possible behavior:
- update the old window, if supported and still allowed
- route the event to a late-data side output
- drop it, if the policy says the window is closedExact behavior varies by engine and window configuration. Some engines support allowed lateness and repeated firing; others have different triggers and retention semantics.
4. Explain tumbling, sliding, and session windows.
Windows group an unbounded stream into finite sets so the system can calculate aggregates such as count, sum, average, or maximum.
| Window type | How it works | Example use |
|---|---|---|
| Tumbling | Fixed-size, non-overlapping windows | Revenue per 5-minute interval |
| Sliding | Fixed-size windows that advance by a smaller slide interval, so windows overlap | Rolling 15-minute request count updated every minute |
| Session | Groups events separated by inactivity gaps; duration depends on activity | User session or browsing activity |
Time ------------------------------------------------>
Tumbling (5 min): [ 0 - 5 ][ 5 - 10 ][ 10 - 15 ]
Sliding (10 min size, 2 min slide):
[ 0 ------ 10 ]
[ 2 ------ 12 ]
[ 4 ------ 14 ]
Session (5 min inactivity gap):
Events: A B C D E
Windows: [session 1] [session 2]Trade-offs: Sliding windows may place one event in multiple windows, increasing computation. Session windows can require more state because sessions may extend or merge when new events arrive. All window types need a policy for late data and finalization.
5. How would you handle duplicate events in a streaming pipeline?
Duplicates can occur because producers retry, brokers redeliver messages, consumers restart, or a source emits the same business event more than once. The solution depends on whether duplicates share a stable event ID and how long deduplication must be guaranteed.
- Use a stable event ID: Prefer a producer-generated unique ID or a documented source key.
- Maintain deduplication state: Track processed IDs for a bounded retention period when the use case allows bounded deduplication.
- Use idempotent sinks: Upsert by business key or use a unique constraint/transactional write where supported.
- Choose retention deliberately: The deduplication horizon must cover realistic replay, outage, and late-arrival scenarios.
- Preserve ordering metadata: A duplicate older event must not overwrite a newer state.
- Monitor duplicates: Track duplicate rates by source and producer to find upstream reliability problems.
if event.id in recently_processed_ids:
ignore event
else:
apply_event_idempotently(event)
remember event.idThis pseudocode illustrates the idea, but production systems must make state updates and output writes recoverable together. If deduplication state expires, old replays may be seen again, so sink idempotency remains valuable.
6. What is backpressure, and how would you troubleshoot it?
Backpressure occurs when downstream processing cannot keep up with the rate of incoming data. Queues grow, consumer lag increases, and end-to-end latency rises. The root cause may be a slow sink, expensive transformation, skew, insufficient capacity, or a temporary traffic spike.
Troubleshooting sequence:
- Compare incoming event rate with processing and output rates.
- Inspect broker lag, queue depth, per-stage latency, busy/idle time, CPU, memory, garbage collection, and sink response time.
- Find the slowest stage rather than scaling the entire pipeline blindly.
- Optimize serialization, expensive transformations, network calls, and sink writes.
- Check key skew and partition imbalance; redistribute work where semantics allow.
- Increase parallelism or capacity if the stage can use it, while checking partition limits and external-system quotas.
- Use buffering and batching appropriately, and define overload behavior such as throttling or controlled degradation.
Example: If stream operators are healthy but the database sink writes slowly, adding more upstream workers may only increase the queue. Batch sink writes, tune connection pools, use asynchronous I/O where appropriate, or reduce unnecessary writes.
7. How would you recover a stateful streaming application after failure?
A stateful stream processor maintains information across events, such as per-key counts, window aggregates, session state, or deduplication IDs. Recovery requires restoring both processing progress and the corresponding state consistently.
Recovery design:
- Use durable checkpoints or snapshots: Persist operator state and source offsets consistently according to the engine's recovery model.
- Restore from a consistent point: Recover state and replay input from the corresponding source position.
- Make outputs recoverable: Use transactional or idempotent sinks so replay does not duplicate externally visible effects.
- Manage checkpoint storage: Monitor checkpoint duration, size, failure rate, retention, and storage availability.
- Test recovery: Simulate worker loss, process restart, and checkpoint corruption in a controlled environment.
- Plan state evolution: Validate compatibility when changing operators, keying, serializers, or state schemas during deployments.
Example: A fraud detection pipeline stores the recent transaction count per account. After a worker failure, it restores the account counters and source offsets from a consistent checkpoint, then replays later events. A safe sink prevents duplicate alerts during replay.
8. How would you maintain accurate aggregations when events arrive out of order?
Use event-time windows, watermarks, and an explicit late-event policy. The pipeline should distinguish provisional results from finalized results and define how corrections reach downstream consumers.
- Aggregate by event time: Assign events to windows using their business timestamps rather than arrival order.
- Set a lateness policy: Configure watermarks and allowed lateness based on observed delay distributions and business needs.
- Support revisions: If late events change a prior aggregate, update or emit a correction to the relevant window.
- Choose the sink contract: Decide whether downstream tables receive upserts, retractions, versioned results, or append-only correction records.
- Handle very late events: Route them to a side output or correction workflow, and backfill affected partitions if necessary.
- Monitor completeness: Track watermark lag, late-event rate, window revisions, and the age of the oldest unprocessed event.
Example: A 10:00–10:05 sales window initially reports $12,000. A delayed transaction later adds $300. The system can emit an updated aggregate of $12,300 or publish a correction, depending on its output contract.
event -> validate timestamp
-> assign event-time window
-> update window state
-> emit provisional/final result
-> revise result if late data is accepted
-> route data beyond allowed latenessNo watermark policy can guarantee that no later event will ever arrive. “Final” typically means final under a configured operational policy, not mathematically certain for all future time.
Quick Revision Table
| Topic | Core takeaway |
|---|---|
| Stream vs. batch | Continuous unbounded processing vs. bounded scheduled processing. |
| Event vs. processing time | When an event happened vs. when the system handled it. |
| Watermarks | Estimate event-time progress and help govern window completion and late data. |
| Windowing | Tumbling is fixed/non-overlapping, sliding overlaps, and session groups by inactivity gaps. |
| Duplicate events | Use stable IDs, deduplication state, and idempotent sinks. |
| Backpressure | Find the slowest stage using rates, lag, and resource metrics. |
| State recovery | Restore state and offsets consistently, then replay safely. |
| Out-of-order events | Use event-time windows, lateness policies, and correction-capable outputs. |
Scenario-Based Practice Questions
Try answering these scenarios out loud before checking the suggested approach.
Scenario 1: Your watermark is not advancing. What would you investigate?
Inspect event timestamps, idle input partitions, source lag, partition skew, and the engine's watermark propagation rules. One input with very delayed timestamps can hold back progress in some systems. Verify timestamp parsing and clock quality, then configure idle-partition handling or source-specific watermark behavior where supported.
Scenario 2: Consumer lag grows even after adding workers.
Find the actual bottleneck. Check sink latency, skew, partition count, serialization cost, external quotas, and whether the added workers can receive independent partitions. Improve the slow stage, batch or optimize sink writes, and compare input rate against sustained processing throughput.
Scenario 3: A late event changes an aggregate already consumed by a dashboard.
Define whether the aggregate is provisional or final. If the event is within allowed lateness, update the window and use an upsert or correction event so the dashboard reflects the revised result. If it is too late, route it to a correction workflow and clearly document how historical totals are repaired.
Scenario 4: A restart causes duplicate alerts from a stateful application.
Check whether source offsets and operator state restore from the same consistent checkpoint, and whether the alert sink supports idempotency or transactional output. Add a stable alert ID, persist output progress where possible, and test failures between state restoration and external side effects.
Chapter Summary
- Choose streaming when continuous processing and low latency justify the operational complexity.
- Distinguish event time, ingestion time, and processing time.
- Use watermarks and lateness policies to balance completeness, latency, and state retention.
- Choose window types based on business semantics and understand their state and compute costs.
- Handle duplicates with stable event IDs, stateful deduplication, and idempotent outputs.
- Troubleshoot backpressure by identifying the bottleneck, not just adding workers.
- Recover stateful applications from consistent checkpoints and make sink effects replay-safe.
- Support corrections when late events change previously emitted aggregates.
Practice Challenge
Design a streaming pipeline that calculates five-minute payment totals and detects suspicious activity. Explain event-time assignment, watermark strategy, window type, duplicate handling, backpressure monitoring, checkpoint recovery, and how you would correct aggregates when late events arrive.
Suggested Blogger post title: Top 8 Stream Processing Interview Questions for Data Engineers | Watermarks, Windows and State
Comments
Post a Comment