Apache Beam

On this page 7

Apache Beam is an open-source, unified programming model for defining both batch and streaming data pipelines with a single API, which are then executed by a separate distributed engine called a runner. In data processing, Beam separates the logic of a pipeline from the system that runs it, so the same code can run on Apache Flink, Apache Spark, Google Cloud Dataflow or other supported backends.

The model grew out of the Dataflow research at Google and became a top-level Apache project in 2017. Its central idea is that a bounded dataset is simply a special case of an unbounded stream, so windowing, event time and triggers apply uniformly to all data.

How Apache Beam pipelines are built

A Beam program constructs a Pipeline object, a directed acyclic graph of processing steps. Data moves through the graph as PCollections, immutable and potentially unbounded collections of elements, and each step is a PTransform that consumes one or more PCollections and produces new ones.

Pipelines are written with one of the language SDKs, mainly Java, Python and Go, plus Beam SQL and a YAML interface. The SDK translates the graph into a portable representation that the chosen runner optimizes, for example by fusing consecutive steps to avoid materializing intermediate data.

The table below lists the building blocks that appear in almost every Beam pipeline.

AbstractionPurposeExample
PipelineContainer for the whole processing graphOne job definition
PCollectionDistributed dataset, bounded or unboundedStream of click events
PTransformOperation applied to PCollectionsFilter, aggregate, join
ParDo and DoFnElement-wise parallel processingParse JSON records
GroupByKey and CombineShuffle and aggregate by keySum revenue per customer
I/O connectorsRead from sources and write to sinksKafka, files, BigQuery
Core abstractions of the Beam model

Windowing, watermarks and triggers in Beam

Beam answers four questions for every computation: what is computed, where in event time, when results are emitted, and how refinements relate. This framework allows correct results on streams whose events arrive late or out of order.

Windowing divides data by event time, the moment an event occurred, rather than by processing time. A watermark is the runner’s estimate of how complete the input is up to a given event time. Triggers decide when to emit a window’s result, and accumulation modes define whether later results replace or add to earlier ones. Allowed lateness specifies how long late data is still accepted.

Window typeBehaviorTypical use
Global windowAll data in one windowBatch jobs, simple totals
Fixed windowsNon-overlapping intervals of equal lengthPer-minute or hourly metrics
Sliding windowsOverlapping intervals with a stepMoving averages
Session windowsGaps of inactivity close a windowUser activity sessions
Window types available in Beam

Choosing the trigger involves a trade-off between latency and completeness: early firings give fast, speculative results, while waiting for the watermark gives fewer but more accurate ones.

Beam runners and portability

A runner translates a Beam pipeline into jobs for a specific execution engine. The portability framework uses language-neutral protocols, the Runner API and Fn API, so a Python or Go pipeline can execute on a Java-based engine, and transforms from different languages can be mixed in cross-language pipelines.

RunnerExecution engineStrengthsConsiderations
Direct RunnerLocal processTesting and validationNot for production volumes
Flink RunnerApache FlinkMature streaming, low latencyCluster operations required
Spark RunnerApache SparkReuses existing Spark clustersStreaming support less complete
Dataflow RunnerGoogle Cloud DataflowManaged autoscaling and rebalancingTied to one cloud provider
Prism RunnerLocal portable engineFast local testing across SDKsDevelopment use
Commonly used Beam runners

Portability is real but not absolute: the capability matrix of each runner differs, and features such as stateful processing, timers or splittable DoFns may behave differently or be unsupported on some engines.

Beam is often confused with the engines it runs on. Apache Spark and Apache Flink are execution engines with their own APIs, while Beam is an abstraction layer above them. MapReduce, by contrast, is an older batch-only model that Beam generalizes.

TechnologyCategoryBatchStreaming
Apache BeamPortable programming modelYesYes, event-time native
Apache SparkExecution engine and APIYesMicro-batch and continuous modes
Apache FlinkExecution engine and APIYesNative streaming
MapReduceBatch programming modelYesNo
DaskPython parallel computing libraryYesLimited
Beam and neighboring technologies

When to use Apache Beam and its limitations

Beam fits teams that need one codebase for batch backfills and real-time streams, want to avoid lock-in to a single engine, or run on a managed service that executes Beam natively. It is also well suited to event-time correctness, for example billing or fraud metrics that must account for late events.

  • Abstraction overhead: engine-specific optimizations and APIs, such as Spark DataFrame tuning, are not directly exposed.
  • Uneven runner support means a pipeline tested on one runner should be validated again before moving to another.
  • Steep learning curve for watermarks, triggers and accumulation, which are frequent sources of incorrect streaming results.
  • Python performance can lag behind Java because of serialization between the SDK harness and the runner.
  • Interactive analysis is not its strength; notebooks with pandas or SQL engines are better for exploration.

A plain-language summary

Apache Beam is like writing a recipe once and letting different kitchens cook it. The recipe describes the steps. Each kitchen has its own staff and equipment. Beam lets you write the processing steps once and choose later which system runs them.

It also treats a pile of old records and a live stream of new events the same way. The main benefit is fewer rewrites. One set of instructions can handle yesterday’s data and today’s live data.

An example from practice

An online retailer needs revenue per product category every five minutes for its operations dashboard, and the same figures recomputed nightly for finance. Order events flow through Apache Kafka, and some arrive up to 20 minutes late because mobile apps buffer them offline. The data team writes one Beam pipeline in Java with five-minute fixed windows on event time, an early trigger every minute and allowed lateness of 30 minutes.

The pipeline runs continuously on a Flink cluster for the dashboard. For the nightly report, the team runs the same code in batch mode against archived order files. Because both paths share identical logic, finance and operations see matching totals, and the team maintains one pipeline instead of two, cutting reconciliation work that previously took several hours each week.