GCP-220 · Lesson 5 of 12 · 19 min

Choosing an ingestion approach: batch or streaming

▶ Video coming soon A video version of this lesson is on the way.

On Tuesday, October 20, Ravi puts the source inventory from lesson 1 back on the screen. "We know what the data looks like and how to clean it," he says. "Now: how does each source get here?" The answer is different for each row of the table, and choosing well depends on a few questions that the exam asks again and again in different forms.

Batch, micro-batch and streaming

Batch ingestion moves data in chunks on a schedule: last night's till files, this month's supplier price lists, a one-time export of ten years of history. It is simple, cheap and easy to re-run, and it is the right choice whenever the people using the data can wait for the next batch.

Streaming ingestion moves each record as it happens, typically within seconds: a click on the website, a payment approval, a sensor reading. It is needed when decisions are made in near real time, such as showing the shop's operations team a live view of checkout failures, or flagging a suspicious order before it ships.

Between the two sits micro-batch: small batches every few minutes. Many "real-time" requirements turn out to be satisfied by data that is five or fifteen minutes old, and micro-batch is often simpler to run than true streaming. Change data capture from a database, covered below, behaves somewhat like a stream of small changes.

Batch, micro-batch and streaming compared.
ApproachTypical delayBirchline exampleStrengthsCosts
BatchHours to a dayNightly till files, monthly supplier price listsSimple, cheap, easy to re-run and checkData is old by the time it is used
Micro-batchMinutesOnline orders every 15 minutes for the operations dashboardFresh enough for most uses, still easy to reason aboutMore runs to schedule and monitor
StreamingSecondsWeb events for a live checkout-failure viewLowest delay; reacts to events as they happenMore moving parts; ordering, duplicates and late data must be handled

The question to ask is not "how fast can we make it?" but "how old can the data be when someone uses it, and what does it cost if it is older?" Mateo goes back to each data owner with that question. Finance needs store sales by eight in the morning for the daily flash report, so nightly batch is enough. The operations team wants to see checkout errors within a minute so they can call the payment provider, which needs streaming. Merchandising looks at web search trends weekly and can use whatever arrives.

Choosing a transfer tool

Google Cloud has several tools for moving data in, and the exam often describes a situation and asks which one fits. The table sorts them by what they move and from where.

Google Cloud tools for getting data in.
ToolMovesFromTypical use
gcloud storage (cp, rsync)FilesA computer or server you controlSmall or occasional uploads; scripts on a server
Storage Transfer ServiceFiles and objectsOther clouds (such as Amazon S3 and Azure Blob Storage), HTTP sources, other Cloud Storage buckets, and on-premises file systems through agentsLarge or recurring transfers, scheduled, with retries and verification
Transfer ApplianceFiles, offlineA data centre with too much data or too little bandwidthShipping a physical device for very large one-time moves
BigQuery Data Transfer ServiceData into BigQuery tablesGoogle SaaS (Google Ads, YouTube, Google Play, Search Ads 360), other clouds' storage, some data warehouses for migration, and scheduled queriesRecurring loads of reporting data straight into BigQuery
Database Migration ServiceWhole databasesMySQL, PostgreSQL, SQL Server and Oracle sourcesMigrating a database to Cloud SQL or AlloyDB
DatastreamOngoing changes from databases (change data capture)MySQL, PostgreSQL, Oracle and SQL ServerReplicating changes into BigQuery or Cloud Storage with low delay
Pub/SubEvent messagesApplications and devices that publish eventsStreaming ingestion (lesson 7)

Several of these tools are easy to confuse. Storage Transfer Service moves files into Cloud Storage; BigQuery Data Transfer Service loads data into BigQuery tables, usually on a schedule, from sources it has connectors for. Database Migration Service moves a database to another database; Datastream copies changes from a database continuously, typically into BigQuery for analysis. Keeping those four apart answers a surprising number of exam questions.

A decision tree. The first question is what is moving. Files lead to a second question: is it a very large one-time move with limited bandwidth? Yes leads to Transfer Appliance; no leads to a third question, recurring or from another cloud? Yes leads to Storage Transfer Service, no to gcloud storage. A database leads to the question: move the database or copy its changes for analysis? Move leads to Database Migration Service; copy changes leads to Datastream. SaaS or reporting data for BigQuery leads to BigQuery Data Transfer Service. Events from applications lead to Pub/Sub.
A simple decision tree for choosing an ingestion tool.

Online or offline: the bandwidth question

For the one-time move of the old warehouse's history and the 40 TB product photo library, the first question is whether to send the data over the network or ship it on a Transfer Appliance. The answer depends on the amount of data and the bandwidth that can be spared. Birchline's head office has a 1 gigabit per second internet connection, but the network team will allow only about half of it for transfers during business hours.

Python Example
# How long would it take to send each dataset over the network?
def days_to_transfer(terabytes, link_gbps, usable_share):
    bits = terabytes * 1e12 * 8                 # 1 TB = 10**12 bytes
    seconds = bits / (link_gbps * 1e9 * usable_share)
    return seconds / 86_400

datasets = {"Warehouse history (Parquet)": 0.6, "Product photo library": 40}
for name, tb in datasets.items():
    day = days_to_transfer(tb, link_gbps=1, usable_share=0.5)
    night = days_to_transfer(tb, link_gbps=1, usable_share=0.9)
    print(f"{name:28s} {tb:5.1f} TB  about {day:5.1f} days at half the link, {night:5.1f} days at 90 in 100")
Output
Warehouse history (Parquet)    0.6 TB  about   0.1 days at half the link,   0.1 days at 90 in 100
Product photo library         40.0 TB  about   7.4 days at half the link,   4.1 days at 90 in 100

What this code does

It converts each dataset's size from terabytes to bits and divides by the part of the internet connection that can be used, giving the transfer time in seconds and then in days. It does this twice: once using half the connection, as allowed during business hours, and once using most of it, as might be possible overnight. The results show that the warehouse history moves in hours, while the photo library needs about a week at half the link.

The warehouse history, exported to compressed Parquet at about 0.6 TB, takes under three hours. The photo library takes a little over seven days at half the link, or about four days with most of the link overnight and at weekends. That is slow but manageable, so the team plans to use Storage Transfer Service with an on-premises agent, which can be limited to a set bandwidth and resumes automatically after interruptions. Transfer Appliance makes sense when an online transfer would take far longer, typically weeks or months, or when the network cannot be spared at all. Google's documentation suggests comparing the online transfer time with the time to order, fill and ship an appliance.

Online or offline for each large dataset.
DataSizeOnline time at half of 1 GbpsDecision
Warehouse history (Parquet)About 0.6 TBAbout 2.7 hoursUpload with gcloud storage or Storage Transfer Service
Product photo libraryAbout 40 TBAbout 7.4 daysStorage Transfer Service with an on-premises agent, bandwidth-limited
A hypothetical 500 TB archive500 TBAbout 93 daysTransfer Appliance
A horizontal bar chart of transfer time in days at half of a 1 gigabit per second link, on a logarithmic scale: warehouse history 0.6 TB about 0.11 days, photo library 40 TB about 7.4 days, a hypothetical 500 TB archive about 93 days. A vertical dashed line marks two weeks, labelled consider Transfer Appliance beyond this.
Online transfer time grows with size; beyond a few weeks, shipping a Transfer Appliance is usually faster.

Birchline's ingestion plan

By the end of the session, Mateo has a plan for each source. It is the first page of what will become the team's ingestion design document.

The ingestion approach chosen for each Birchline source.
SourceApproachToolLanding placeLesson
Store till filesNightly batchStores upload to Cloud Storage; BigQuery load job (ELT)gs://bl-data-landing/pos/ then raw.pos_sales6
Online orders and product catalogueContinuous changesDatastream from MySQLraw dataset in BigQuery6
Web eventsStreamingPub/Sub, then a BigQuery subscription or Dataflowraw.web_events7 and 8
Returns portalNightly batch with a light transform (ETLT)Cloud Run job converts and masks; BigQuery loadraw.returns6
Supplier price listsMonthly batch from many formatsCloud Data Fusion pipelinestaging.supplier_prices9
Google Ads campaign dataDaily batchBigQuery Data Transfer Serviceraw.google_ads6
Warehouse historyOne-time bulkExport to Parquet; upload; loadraw.history_sales6
Product photo libraryOne-time bulkStorage Transfer Service with an agentgs://bl-product-photos/11

Triggering a batch: schedules and arrival events

A batch load needs something to start it. The simplest trigger is a schedule: run the load at 03:00 every night, after the stores' 02:00 delivery deadline. Cloud Scheduler, BigQuery scheduled queries and the schedulers built into Storage Transfer Service and BigQuery Data Transfer Service all work this way. Schedules are easy to understand, but they assume the data has arrived. If store 019's file turns up at 03:20, a 03:00 load misses it until the next night.

The alternative is to trigger on arrival. Cloud Storage can announce each new object, and Eventarc can route that event to a Cloud Run service or a Cloud Run function that starts a load for just that file. Each store's sales then load minutes after they arrive, and a late file is simply loaded late. The price is more moving parts: one load per file instead of one per night, and a separate check that all 40 files eventually arrived. Birchline starts with a nightly schedule plus a missing-file check, and plans to revisit arrival triggers once the stores move to the new tills.

Whichever trigger is used, the job runs as a service account, not as a person. Ravi creates one service account for the nightly load with only the permissions it needs: read objects in the landing bucket, create load jobs and write to the raw dataset. GCP-222 returns to access control, but the habit starts here: no pipeline should depend on someone's personal account staying active.

Change data capture

The online shop's MySQL database deserves a closer look. Copying the whole orders table every fifteen minutes would mostly copy rows that have not changed, and would put load on the production database. Change data capture (CDC) instead reads the database's own change log, the binary log in MySQL, and sends only inserts, updates and deletes. Datastream does exactly this and can write the changes into BigQuery, where it keeps a table that mirrors the source. The delay is typically seconds to minutes, and the shop database barely notices.

CDC does need some preparation on the source. The database must keep its change log long enough for Datastream to read it, Datastream needs a user with the right permissions, and the network path between Datastream and the database must be set up, for example through private connectivity. The shop team schedules that work with Mateo for early November, and the first Datastream stream is planned for the week of November 16.

Thinking about reliability

Whatever the approach, an ingestion job fails sometimes: a store's upload is late, a network link drops, a source sends a malformed file. Good designs plan for it. Batch loads should be idempotent, so re-running a night does not double it, as lesson 4 explained. Transfers should verify what they copied, which Storage Transfer Service does by checking checksums. Streaming pipelines must expect duplicates and late events, which lessons 7 and 8 cover. And every feed needs a way to notice missing data, which is the subject of lesson 10.

Common mistakes

  • MistakeChoosing streaming because it sounds modern.

    InsteadAsk how old the data can be when used; choose batch unless fresher data is worth the extra complexity.

  • MistakeUsing Storage Transfer Service to load data into BigQuery tables.

    InsteadStorage Transfer Service moves files into Cloud Storage; BigQuery Data Transfer Service and load jobs fill tables.

  • MistakeUsing Database Migration Service to feed analytics from a live database.

    InsteadUse Datastream for continuous change data capture into BigQuery; DMS is for migrating databases.

  • MistakeOrdering a Transfer Appliance for a few terabytes.

    InsteadCalculate the online transfer time first; an appliance pays off only for very large moves or very limited bandwidth.

Worked problem

Picking tools for a new source

Birchline's marketing team wants the daily performance of its Google Ads campaigns in BigQuery, and the e-commerce team wants every change to the product catalogue in BigQuery within a few minutes. Which tool fits each?

Show solution
  1. Google Ads is a Google SaaS source with a connector in BigQuery Data Transfer Service, which can load campaign data into BigQuery tables on a daily schedule.
  2. The product catalogue lives in the shop's MySQL database and changes during the day.
  3. Copying the whole table repeatedly would be wasteful; change data capture sends only the changes.
  4. Datastream reads MySQL's change log and writes the changes into BigQuery within minutes.

Answer: BigQuery Data Transfer Service for Google Ads; Datastream for the product catalogue.

Summary and key terms

  • Batch ingestion moves data in scheduled chunks, streaming moves each record within seconds, and micro-batch runs small batches every few minutes.
  • Choose by asking how old data can be when it is used. gcloud storage suits small uploads; Storage Transfer Service moves files into Cloud Storage from other clouds, HTTP and on-premises agents; Transfer Appliance ships very large datasets offline.
  • BigQuery Data Transfer Service loads SaaS and other sources into BigQuery tables on a schedule.
  • Database Migration Service migrates databases; Datastream captures database changes continuously into BigQuery.
  • Pub/Sub receives events.
  • Compare online transfer time with appliance time for large moves, and design every feed to be re-runnable and checked.
Batch ingestion
Moving data in chunks on a schedule.
Streaming ingestion
Moving each record as it happens, typically within seconds.
Micro-batch
Small batches run every few minutes.
Storage Transfer Service
A managed service that copies files into Cloud Storage from other clouds, HTTP sources, buckets and on-premises systems.
Transfer Appliance
A physical device Google ships for very large offline data transfers.
BigQuery Data Transfer Service
A service that loads data from supported sources into BigQuery on a schedule.
Change data capture
Copying only the inserts, updates and deletes from a database's change log.
Datastream
Google Cloud's managed change data capture service.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. What is the first question when choosing between batch and streaming?

    Show answer

    How old the data can be when someone uses it, and what it costs if it is older.

  2. Which tool copies ongoing changes from MySQL into BigQuery?

    Show answer

    Datastream.

  3. Which tool loads Google Ads data into BigQuery on a schedule?

    Show answer

    BigQuery Data Transfer Service.

  4. About how long does 40 TB take over half of a 1 Gbps link?

    Show answer

    About 7.4 days.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz