← GCP Data Engineering

GCP Data Engineering · Chapter 06

Streaming with Pub/Sub and Dataflow

Batch pipelines run on a schedule. Streaming pipelines run all the time, handling each event as it arrives. On GCP the pair is Pub/Sub, which carries the messages, and Dataflow, which processes them. This chapter is where time becomes a first-class problem.

0 of – checks answered

01

Batch or streaming?

All data is created in real time. Batching is a simplification we choose because people ask for figures over a period and batches are easier to control. Streaming earns its complexity when the value depends on immediacy.

BatchStreaming
When it runs On a schedule (hourly, daily, weekly)Continuously, one job that never ends
Typical question How much X per day or month?What is happening right now?
Control You control the schedule and rerunsYou handle time, lateness and state
Share of pipelines About 90% (the author’s estimate)The remainder, but growing

Which fits?

  1. 01 The CEO wants last month’s revenue, calculated once from purchase history.

  2. 02 A card transaction must be checked for fraud the moment it happens.

  3. 03 A sales dashboard should tick up second by second during a flash sale.

  4. 04 Daily fact tables are rebuilt each night for the operations team.

The GCP pattern: sources publish to Pub/Sub, Dataflow processes the messages in a distributed way (using Cloud Storage for temporary data if needed), and the results stream into BigQuery. There is no scheduler; the job is long-running.

02

Pub/Sub: a messaging bridge

Pub/Sub receives messages from many systems and hands them to many systems. Play with it: publish messages, pull them with different subscribers, acknowledge them, and let a deadline expire.

Subscription A has two subscribers sharing its load. Subscription B has one. A message published to the topic is copied to both subscriptions.

Nothing published yet.

Grey chips are waiting in the subscription. Blue chips have been delivered but not acknowledged. An ack tells Pub/Sub to stop sending that message; without one it is redelivered until acked or expired.

Name the piece

  1. 01 The application code that sends each bike-trip JSON message.

  2. 02 The named store that holds the messages, like a table holds rows.

  3. 03 An entity that has an interest in a topic. Two of them on one topic each receive identical messages.

  4. 04 One of several workers that share the load of a single subscription.

Subscription settings that matter

SettingWhat it controlsTrade-off
Delivery type Pull (subscriber asks) or push (HTTP endpoint)Pull is recommended for pipelines; push has a far smaller throughput quota
Message retention How long unacked messages are keptLonger means less risk of loss but more storage cost
Expiration period When an inactive subscription is deletedPrevents forgotten subscriptions piling up
Ack deadline How long before an unacked message is resentHeavy processing needs a longer deadline so subscribers are not overwhelmed
  1. 01 Dataflow, using the Pub/Sub client, will read messages for a data pipeline.

  2. 02 The consumer cannot use the Pub/Sub client and instead exposes an HTTP endpoint.

03

Apache Beam and Dataflow

Dataflow is a serverless processing engine for batch and streaming, positioned like Spark but without a cluster. You write the pipeline with Apache Beam, which hands it to a runner.

Pipe, name, transform

A Beam pipeline is a chain of steps. Each step is written | 'name' >> transform. The name is optional but always worth adding, because Dataflow shows it in the monitoring graph. Beam describes the pipeline; a runner executes it.

Read
ReadFromText, ReadFromPubSub
Write
WriteToText, WriteToBigQuery
Debug only
Map(print): sample first, never print a big dataset
Spark on DataprocBeam on Dataflow
Infrastructure You create and size a cluster (or make it ephemeral)Serverless: submit the job
Data structure RDD / DataFramePCollection
Batch and streaming Both, with different APIsBoth, with very similar code
Billing Cluster hoursWorker hours; set a maximum number of workers

Which runner?

  1. 01 Quickly test a change on the Cloud Shell machine before deploying.

  2. 02 Run the pipeline in production with autoscaling and a monitoring graph.

A streaming Dataflow job stays on until you stop it. Cancel stops immediately; Drain finishes processing the data already accepted first. The minimum is one worker, so a job left running for a day costs one VM for 24 hours. Cap the maximum workers to avoid surprise autoscaling bills.

04

Windows: giving a stream boundaries

Streams never end, so you cannot sum 'everything'. A window slices time so each slice yields a result. Below is a small stream of bike trips. Change the window size and watch the aggregated rows that would be appended to BigQuery.

Window start Station Trips Sum of duration (s)

In Beam this is WindowInto(FixedWindows(60)), then key by station and CombinePerKey(sum). Each appended row also needs its window timestamp, otherwise rows for the same station are indistinguishable. A view that sums per station gives you the running total.

05

Hands-on: stream bike trips

Streaming jobs cost money for as long as they run. Do the exercises in one session and cancel everything at the end.

  1. Open each service once in the console.

  2. Name it informatively, such as bike-sharing-trips. Untick the default-subscription option so you can create one by hand and see the settings.

    
    						gcloud pubsub topics create bike-sharing-trips
    					
  3. Generate a random JSON trip, publish it in a loop of 10, and wait on the publish futures with a callback for errors.

  4. Choose pull delivery, a short message retention, an expiration period and an acknowledgement deadline that suits your processing speed.

    
    						gcloud pubsub subscriptions create bike-sharing-trips-subs-1 \
      --topic=bike-sharing-trips --ack-deadline=60
    					
  5. Publish twice, pull in the console, and count 20 messages. Tick "enable ack messages" and ack one so it disappears.

  6. Use a venv to avoid install errors, and install the GCP extras. The book pins an old release; use a current supported version.

    
    						python3 -m venv beam-env && source beam-env/bin/activate
    pip install "apache-beam[gcp]"
    					
  7. Read a text file from Cloud Storage, sample ten lines and print them. Sample first so you never print a huge dataset.

  8. Use ParDo to split the log lines, key by URL, count per key and write the result.

  9. Change only the runner flag, then open the job graph in the Dataflow console.

  10. Read from your subscription with streaming enabled, decode and parse the JSON, and write to a BigQuery table. Test with three tabs: the pipeline, the BigQuery console and the publisher.

  11. Use Cancel to stop immediately, or Drain to finish in-flight data first. Dataflow bills by worker hours, and a streaming job will not stop itself.

  12. Window, key by start station, combine per key with sum, then add the window timestamp to each record so appended rows are distinguishable.

  13. Leave nothing running.

06

Key terms

Batch / streaming
Process groups on a schedule, or each event as it arrives.
Pub/Sub
Google’s messaging service: publishers, topics, subscriptions, subscribers.
Topic
The named store of messages.
Subscription
A consumer’s interest in a topic; each gets a full copy.
Ack
Acknowledgement that stops redelivery of a message.
Dataflow
Serverless data processing for batch and streaming.
Apache Beam
The SDK for writing pipelines that runners execute.
PCollection
Beam’s distributed dataset.
ParDo
A flexible transform that can emit any number of rows per input.
Runner
The engine that executes a Beam pipeline, e.g. Direct or Dataflow.
Window
A slice of time used to aggregate an unbounded stream.
CDC
Change data capture: streaming a database’s inserts, updates and deletes.

07

Check yourself

Pick an answer to see the reasoning straight away.

Q1

Why are most data pipelines still batch?

Q2

A topic has two subscriptions. What does each receive?

Q3

What happens to a message that is delivered but never acknowledged?

Q4

Why aggregate a stream inside time windows?

Q5

A streaming Dataflow job you started is still running after your exercise. What is the consequence?

Q6

How is Dataflow different from Spark on Dataproc, from a user’s point of view?

Study notes in my own words, following the structure of Adi Widjaja's Data Engineering with Google Cloud Platform (Packt, 2022). GCP changes quickly, so check each service's current documentation before relying on names, prices or console steps.