11.2 Stream and Batch Processing: Dataflow & Dataproc
Key Takeaways
- Cloud Dataflow provides a fully managed, serverless execution engine for the Apache Beam SDK, unifying batch (bounded) and stream (unbounded) processing pipelines with end-to-end exactly-once semantics.
- Dataflow eliminates stragglers and runtime bottlenecks via Dynamic Work Rebalancing and Streaming Engine, which offloads pipeline state and shuffle operations from worker VMs.
- Stream processing in Dataflow manages time dimensions using Fixed, Sliding, and Session windows, Event-Time Watermarks, and allowed lateness with accumulating or discarding panes.
- Dataflow Prime introduces resource-based horizontal and vertical autoscaling, right-fitting worker machine resources dynamically to individual pipeline execution stages.
- Cloud Dataproc provides fast (<90s) managed clusters for Apache Spark and Hadoop, optimizing costs via ephemeral job-scoped clusters with Spot/Preemptible secondary workers, Dataproc Serverless, and Dataproc on GKE.
Stream and Batch Processing: Dataflow & Dataproc
Architectural Selection Principle: Selecting between Cloud Dataflow and Cloud Dataproc depends primarily on the software ecosystem, operational model, and pipeline semantics. Choose Cloud Dataflow for new, cloud-native pipelines requiring serverless operation, unified batch/stream processing via Apache Beam, advanced windowing, and zero infrastructure management. Choose Cloud Dataproc when migrating existing open-source big data ecosystems (Apache Spark, Hadoop, Hive, Presto, Flink), executing ephemeral job-scoped Spark workloads, or utilizing customized JVM configurations on cost-effective Spot VMs.
Cloud Dataflow: Architecture & Apache Beam Mechanics
Cloud Dataflow is a fully managed, serverless distributed execution framework for executing Apache Beam pipelines (written in Java, Python, Go, or SQL). Dataflow abstracts away all underlying compute infrastructure, handling provisioning, horizontal scaling, graph optimization, and fault tolerance automatically.
+-----------------------------------------------------------------------------------------+
| CLOUD DATAFLOW EXECUTION MODEL |
+-----------------------------------------------------------------------------------------+
| PIPELINE DEFINITION | Apache Beam SDK (Java / Python / Go / Beam SQL) |
| | - Unified abstractions: PCollection (Data) & PTransform (Logic)|
+-----------------------+-----------------------------------------------------------------+
| GRAPH OPTIMIZATION | Execution Graph Optimization & Pipeline Fusion |
| | - Fuses adjacent DoFns into single stages to eliminate I/O |
+-----------------------+-----------------------------------------------------------------+
| DISTRIBUTED RUNTIME | Dynamic Work Rebalancing + Streaming Engine |
| | - State & Shuffle offloaded to managed Google backend |
+-----------------------+-----------------------------------------------------------------+
| AUTOSCALING | Horizontal Worker Scaling + Dataflow Prime Vertical Scaling |
+-----------------------------------------------------------------------------------------+
Core Apache Beam Abstractions
- Pipeline: Encapsulates the entire data processing workflow from data ingestion to transformation and output sinking.
- PCollection (Parallel Collection): Represents a distributed, multi-element dataset. A
PCollectioncan be bounded (finite, fixed-size dataset for batch processing) or unbounded (continuous, infinite data stream for real-time processing). - PTransform (Parallel Transform): Represents an operation that takes one or more
PCollectionsas input, performs transformation logic (e.g.,ParDo,GroupByKey,Combine,Filter), and outputs one or morePCollections. - Pipeline Fusion: Before executing a pipeline, Dataflow optimizes the execution graph by fusing adjacent
ParDooperations into single execution steps. This eliminates unnecessary intermediate data serialization, disk writes, and network roundtrips between worker VMs. - Dynamic Work Rebalancing: In batch pipelines, uneven data distribution can cause certain worker nodes to become "stragglers," holding up pipeline completion. Dataflow continuously monitors worker progress in real time and dynamically splits remaining unprocessed work from lagging workers, redistributing it across idle workers to minimize overall execution time.
DYNAMIC WORK REBALANCING (STRAGGLER MITIGATION)
Worker 1 (Fast): [ Finished Chunk A ] ---> [ IDLE ] <==================+
Worker 2 (Lagging): [ Processing Chunk B .............................. ] |
|
Dataflow Runtime Splits Work Automatically |
v |
Worker 2 (Relieved):[ Processing Chunk B1 ] |
Worker 1 (Assisting): [ Takes Over Chunk B2 ] <-------+
Dataflow Streaming Engine & Exactly-Once Semantics
- Streaming Engine Architecture: Traditional stream runners store intermediate window state and shuffle data directly on worker VM disks. Dataflow Streaming Engine decouples state storage and shuffle processing from worker VMs, moving state into a dedicated, highly optimized Google Cloud backend service. This drastically reduces worker VM CPU and memory overhead, accelerates autoscaling responsiveness, and provides smoother throughput.
- End-to-End Exactly-Once Processing: Dataflow ensures that every record in an unbounded stream is processed exactly once by coordinating checkpoints, tracking message IDs, and integrating with transactional sinks (such as BigQuery Storage Write API with exactly-once stream semantics, Cloud Spanner, and Cloud Storage with metadata tracking).
Windowing, Watermarks & Handling Late Data
Real-time stream processing must resolve the fundamental disconnect between Event Time (when the event actually occurred on the client device) and Processing Time (when the event reaches the processing engine over the network).
EVENT TIME VS. PROCESSING TIME & WATERMARKS
Event Time Clock: 12:00 ---- 12:05 ---- 12:10 ---- 12:15 ---- 12:20
| | | |
| (Network Delays) | |
v v v v
Processing Time: 12:02 ---- 12:09 ---- 12:14 ---- 12:26
^
[ Watermark Advances to 12:05 ]
(System assumes all <= 12:05 events have arrived)
1. Windowing Strategies
Windows partition unbounded streams into finite temporal chunks for aggregation.
| Window Type | Description | Behavioral Mechanics | Typical Use Cases |
|---|---|---|---|
| Fixed (Tumbling) Windows | Non-overlapping, consistent time intervals (e.g., 5 minutes). | Every event belongs to exactly one window (e.g., [12:00-12:05), [12:05-12:10)). | Calculating 5-minute metric rollups, hourly revenue totals. |
| Sliding (Hopping) Windows | Overlapping time intervals defined by duration and period (e.g., 10-min duration, 1-min slide). | An individual event belongs to multiple overlapping windows simultaneously. | Calculating 10-minute moving averages refreshed every 60 seconds. |
| Session Windows | Dynamic, data-driven windows based on periods of user activity separated by a gap duration (e.g., 30 min of inactivity). | Windows expand dynamically per user/key; closes when no events arrive within the gap duration. | Tracking user web sessions, gaming play sessions, fraud burst patterns. |
| Global Windows | Single default window spanning all time across the entire dataset. | Requires explicit non-default triggers (e.g., element count) to emit results on unbounded streams. | Continuous running totals, anomaly detection with custom count triggers. |
2. Watermarks, Allowed Lateness & Panes
- Watermarks: A watermark is a dynamically computed temporal threshold that represents the pipeline's confidence that all data up to event time $T$ has arrived. Once the watermark passes the end of a window, the window is evaluated and emitted.
- Allowed Lateness (
withAllowedLateness): Mobile devices and IoT sensors frequently experience offline network disconnections, delivering data hours after the watermark has passed. Allowed lateness specifies how long Dataflow keeps window state open after the watermark has closed the window. - Accumulating vs. Discarding Panes: When late data arrives for an already-emitted window:
- Accumulating Mode: Dataflow re-emits the complete window total including both original and newly arrived late records (e.g., emits 100, then emits 105).
- Discarding Mode: Dataflow emits only the incremental delta brought by the late data (e.g., emits 100, then emits +5).
WINDOW EMISSION & LATE DATA TIMELINE
Window [12:00 - 12:05)
|--- Normal Events Arrive ---> Watermark passes 12:05 ---> [ Pane 1 Emitted: Total = 100 ]
|
|--- (Window closed, but within Allowed Lateness = 10m) ------------+
|--- Late Event (EventTime 12:03) arrives at ProcessingTime 12:12
| |
+---> [ Accumulating Mode ] ----------------------------------------> [ Pane 2 Emitted: Total = 105 ]
+---> [ Discarding Mode ] ----------------------------------------> [ Pane 2 Emitted: Delta = +5 ]
Dataflow Prime: Next-Generation Serverless Optimization
Dataflow Prime enhances standard Cloud Dataflow with intelligent resource management and automated infrastructure optimization.
- Resource-Based Autoscaling: Traditional Dataflow scales by adding identical worker VMs (homogeneous scaling). Dataflow Prime provisions compute resources (CPU vs. Memory) dynamically per pipeline step. If a transform is memory-heavy (e.g., large cache lookup) but another is CPU-heavy (e.g., encryption), Prime assigns right-sized resources to individual stages without over-provisioning the entire cluster.
- Vertical Autoscaling for Memory: Automatically detects impending Out-Of-Memory (OOM) conditions during large aggregations and dynamically increases worker memory allocations on the fly, preventing costly pipeline crashes and retries.
- Right-Fitting Recommendations: Analyzes historical pipeline metrics in Cloud Logging/Monitoring to provide actionable machine configuration recommendations.
Cloud Dataproc: Architecture & Cluster Topologies
Cloud Dataproc is Google Cloud's fully managed service for open-source big data processing engines, including Apache Spark, Apache Hadoop, Hive, Presto/Trino, Pig, and Flink.
+-----------------------------------------------------------------------------------------+
| CLOUD DATAPROC CLUSTER TOPOLOGY |
+-----------------------------------------------------------------------------------------+
| MASTER / PRIMARY WORKERS (Standard Persistent VMs) |
| - Runs HDFS NameNode, YARN ResourceManager, Spark Driver, Hive Metastore |
| - State & cluster metadata maintained; NEVER run on Spot VMs |
+-----------------------------------------------------------------------------------------+
| PRIMARY WORKERS (Standard VMs) |
| - Runs YARN NodeManager & HDFS DataNode; maintains block storage |
+-----------------------------------------------------------------------------------------+
| SECONDARY WORKERS (Spot / Preemptible VMs) |
| - Compute-only (YARN NodeManagers); DO NOT store HDFS data |
| - Scaled up/down aggressively; up to 80% cost reduction; zero HDFS corruption on loss |
+-----------------------------------------------------------------------------------------+
| PERSISTENCE TIER: Cloud Storage (gs://data-lake-bucket) via Cloud Storage Connector |
+-----------------------------------------------------------------------------------------+
Modern Dataproc Deployment Models
- Ephemeral (Job-Scoped) Clusters:
- Rather than maintaining an expensive 24/7 static Hadoop cluster, architects deploy an ephemeral cluster via Cloud SDK/Airflow, execute a single Spark/Hive job, and immediately delete the cluster upon job completion.
- Rapid Provisioning: Dataproc clusters initialize in under 90 seconds, making ephemeral job execution operationally efficient.
- Decoupled Storage: Persistent cluster storage is completely replaced by Google Cloud Storage (GCS) using the open-source Cloud Storage Connector (
gs://connector instead ofhdfs://). Cluster termination causes zero data loss.
- Primary vs. Secondary (Spot/Preemptible) Workers:
- Primary Workers: Run both YARN NodeManagers and HDFS DataNodes. Primary workers must be standard Compute Engine instances to preserve cluster stability and HDFS replication integrity.
- Secondary Workers (Compute-Only): Run only YARN NodeManagers and execute stateless Spark executors. They do not participate in HDFS storage. Secondary workers can be configured as Spot VMs (or Preemptible VMs). If Google Cloud reclaims Spot VMs, Dataproc reschedules lost executor tasks onto remaining nodes without risking HDFS data corruption or cluster failure.
- Dataproc Serverless:
- Enables developers to submit Spark batch workloads (PySpark, Spark SQL, Spark R, Java/Scala) directly without creating, configuring, sizing, or maintaining any cluster infrastructure.
- Google Cloud provisions and auto-tunes Spark runtime containers on demand, billing strictly for the fractional CPU cores and memory utilized per second of job execution.
- Dataproc on GKE:
- Deploys Spark workloads directly onto existing Google Kubernetes Engine (GKE) clusters, unifying containerized microservices and big data batch processing on a single shared compute plane.
Cloud Dataflow vs. Cloud Dataproc Decision Matrix
| Evaluation Dimension | Cloud Dataflow | Cloud Dataproc |
|---|---|---|
| Core Framework | Apache Beam (Java, Python, Go, SQL). | Apache Spark, Hadoop, Hive, Presto/Trino, Flink. |
| Infrastructure Management | 100% Serverless; zero VM, OS, or cluster administration. | Managed clusters (Ephemeral or Long-lived) or Dataproc Serverless for Spark. |
| Scaling Mechanics | Horizontal autoscaling + Dynamic Work Rebalancing + Dataflow Prime vertical scaling. | Cluster Autoscaler (YARN metrics) + Spot VM secondary worker pools. |
| Streaming / Batch Paradigm | Unified programming model; identical primitives for bounded & unbounded data. | Separate APIs (Spark Streaming / Structured Streaming vs Spark Core RDD/DataFrames). |
| Windowing & Late Data | Native, sophisticated event-time windowing, watermarks, and allowed lateness. | Basic micro-batch / structured streaming windowing; requires manual watermarking logic. |
| Migration Strategy | Ideal for greenfield cloud-native stream/batch architectures. | Ideal for lift-and-shift of existing Hadoop/Spark codebases with minimal rewrites. |
| Startup Latency | Pipeline worker provisioning: ~3-5 minutes. | Cluster provisioning: < 90 seconds; Serverless job startup: ~30-60s. |
| Pricing Model | Sized by vCPU, memory, and Streaming Engine data processed. | Underlying Compute Engine VM pricing + Dataproc management fee ($0.01/vCPU/hr) or Serverless DCU. |
Concrete Architectural Scenario: Connected Vehicle IoT Fleet Analytics
Scenario Profile
- Client: Automotive OEM collecting real-time telemetry (speed, engine temperature, GPS) from 2 million connected vehicles.
- Requirements:
- Ingest 500,000 telemetry messages/sec from Cloud Pub/Sub.
- Compute 1-minute moving average speeds and detect engine overheating alerts within 3 seconds of event occurrence.
- Handle delayed telemetry uploads from vehicles traveling through cellular dead zones (up to 30 minutes late).
- Train a daily fleet maintenance prediction model using an existing complex Apache Spark MLlib library.
[ 2M Connected Vehicles ] ---> [ Cloud Pub/Sub Topic ]
|
v
[ Cloud Dataflow Pipeline (Streaming Engine) ]
- Sliding Windows (1-min duration, 10s slide)
- Watermarks + Allowed Lateness (30 minutes)
- Dynamic Work Rebalancing & Exactly-Once Ingestion
|
+------------------+------------------+
| |
v v
[ BigQuery Real-Time Dashboard ] [ Cloud Storage Data Lake (Parquet) ]
(Sub-3s Overheating Anomaly Alerts) |
v
[ Ephemeral Dataproc Cluster ]
- Provisioned via Cloud Composer (Airflow)
- Primary: 2 Standard VMs | Secondary: 50 Spot VMs
- Runs Daily Spark MLlib Training Job
- Deletes Cluster Immediately on Completion (<90s init)
Architecture Blueprint
- Real-Time Stream Processing: Cloud Dataflow reads unbounded streams from Cloud Pub/Sub. Telemetry is grouped into Sliding Windows (1-minute window, 10-second slide) to calculate moving averages. To accommodate intermittent cellular connectivity, Dataflow sets
allowedLateness(Duration.standardMinutes(30))with accumulating panes. - Serverless Streaming Engine: Dataflow Streaming Engine offloads pipeline state, ensuring real-time anomaly detection alerts reach BigQuery and operations teams in under 3 seconds.
- Batch Machine Learning Pipeline: Raw telemetry is persisted in Parquet format on Cloud Storage. A Cloud Composer (Managed Airflow) DAG provisions an ephemeral Cloud Dataproc cluster daily. The cluster uses 2 Standard Master/Worker VMs and 50 Spot VM Secondary Workers to execute the existing Spark MLlib model at minimal cost, terminating the cluster immediately upon completion.
[!IMPORTANT] Exam Watch:
- If an exam scenario describes migrating an existing Apache Spark, Hadoop, Hive, or Presto codebase to Google Cloud with minimal engineering effort and the lowest operational disruption, Cloud Dataproc is the correct answer.
- To minimize Dataproc batch processing costs, configure ephemeral clusters storing persistent data in Cloud Storage (
gs://) and utilizing Secondary Workers on Spot / Preemptible VMs.- If the scenario demands a serverless, unified batch and stream pipeline with complex event-time windowing, watermarks, late data handling, or Dynamic Work Rebalancing, choose Cloud Dataflow (Apache Beam).
A streaming analytics pipeline running on Cloud Dataflow aggregates mobile app usage metrics into 5-minute fixed windows. Due to intermittent mobile device network connectivity, some log events arrive up to 15 minutes after the window period closes. The business requires that late-arriving events update the corresponding 5-minute window aggregates rather than being dropped. How should the Apache Beam pipeline be configured?
An enterprise is migrating 150 existing Apache Spark and Hive batch processing workflows from an on-premises Hadoop cluster to Google Cloud. The workflows run overnight for 3 hours. The architecture team mandates minimizing compute costs, avoiding cluster maintenance during the day, and preventing data loss if worker nodes are reclaimed. What is the most cost-effective and operationally sound architecture?
A batch Dataflow pipeline processing terabytes of data experiences significant performance degradation. Monitoring dashboards indicate that while 95% of worker VMs finish their assigned tasks within 10 minutes, a few worker VMs remain stuck processing massive, uneven data splits for over 2 hours. What built-in feature of Cloud Dataflow automatically addresses this straggler problem?
A data science team needs to execute ad-hoc PySpark data transformation scripts against large datasets stored in Cloud Storage. The team does not have Hadoop administration expertise and does not want to manage cluster sizing, node provisioning, YARN tuning, or cluster lifecycle scripts. Which Google Cloud service should the team adopt?