Dataflow for stream/batch processing in GCP - Time & Space Complexity
When using Dataflow to process data streams or batches, it is important to understand how the time to complete the job changes as the amount of data grows.
We want to know how the number of processing steps and API calls grows when we increase the data size.
Analyze the time complexity of the following Dataflow pipeline code.
pipeline.apply("ReadData", TextIO.read().from(inputPath))
.apply("ParseData", ParDo.of(new ParseFn()))
.apply("GroupByKey", GroupByKey.create())
.apply("WriteResults", TextIO.write().to(outputPath));
This pipeline reads data, parses it, groups by key, and writes the results.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Processing each data element through parsing and grouping.
- How many times: Once per data element for parsing; grouping depends on keys but involves shuffling data.
As the number of data elements increases, the parsing step runs once per element, and the grouping step involves shuffling data across workers.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 parse calls, small shuffle |
| 100 | About 100 parse calls, larger shuffle |
| 1000 | About 1000 parse calls, much larger shuffle |
Pattern observation: The number of operations grows roughly in direct proportion to the number of data elements.
Time Complexity: O(n)
This means the processing time grows linearly with the amount of data to process.
[X] Wrong: "Grouping data is a constant-time operation regardless of data size."
[OK] Correct: Grouping requires shuffling data across workers, which grows with data size and can be costly.
Understanding how Dataflow scales with data size helps you design efficient pipelines and shows you grasp cloud data processing concepts.
"What if we added a filter step before grouping? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand streaming processing
Streaming processes data continuously as new data arrives in real time.Step 2: Understand batch processing
Batch processes data in fixed sets or chunks, not continuously.Final Answer:
Streaming processes data continuously as it arrives; batch processes fixed sets of data. -> Option BQuick Check:
Streaming = continuous, Batch = fixed sets [OK]
- Confusing batch as continuous processing
- Thinking streaming only reads data
- Assuming batch is always faster
Solution
Step 1: Identify the correct property for streaming
In Dataflow Python SDK, streaming mode is enabled by setting streaming = True on StandardOptions.Step 2: Check other options
Batch is default and does not require setting batch = True; mode property does not exist; stream = False disables streaming.Final Answer:
options.view_as(StandardOptions).streaming = True -> Option DQuick Check:
streaming = True enables streaming mode [OK]
- Using batch=True which is invalid
- Setting mode='batch' which is not a valid property
- Confusing stream with streaming property
import apache_beam as beam
with beam.Pipeline() as p:
result = (
p | beam.Create([1, 2, 3])
| beam.Map(lambda x: x * 2)
| beam.Map(lambda x: x + 1)
)
result | beam.Map(print)Solution
Step 1: Trace the transformations
Input list is [1, 2, 3]. First Map multiplies each by 2: [2, 4, 6]. Second Map adds 1 to each: [3, 5, 7].Step 2: Confirm batch mode output
Batch mode processes all data at once, so output will be printed as 3, 5, 7.Final Answer:
3, 5, 7 -> Option AQuick Check:
Multiply by 2 then add 1 = 3,5,7 [OK]
- Forgetting to multiply before adding
- Assuming streaming mode affects output here
- Expecting original input as output
Solution
Step 1: Identify streaming mode requirement
Streaming pipelines require setting streaming=True in options to process live data.Step 2: Evaluate other options
Changing input to batch file disables streaming; removing transforms breaks logic; no options means default batch mode.Final Answer:
Set options.view_as(StandardOptions).streaming = True before running the pipeline. -> Option AQuick Check:
Enable streaming option to fix live data processing [OK]
- Not enabling streaming mode in options
- Switching to batch input source mistakenly
- Removing necessary transforms
Solution
Step 1: Choose streaming mode for live sensor data
Live sensor data requires streaming mode to process data as it arrives.Step 2: Select Pub/Sub as source and apply Filter transform
Pub/Sub is the streaming data source; Filter removes values below 50; BigQuery sink stores results.Final Answer:
Use streaming mode with a Pub/Sub source, apply a Filter transform, then write to BigQuery sink. -> Option CQuick Check:
Streaming + Pub/Sub + Filter + BigQuery = correct pipeline [OK]
- Using batch mode for live data
- Using Cloud Storage as streaming source
- Applying Map instead of Filter for filtering
Ready to prove it?
Pass the test and this pattern moves unknown
