Maintaining and automating data workloads · Topic 1 of 5

Optimizing Cost and Resources for Data Workloads

Cut BigQuery, Dataflow and Dataproc costs per business need, protect capacity for critical jobs, and choose persistent or job-based Dataproc clusters.

  • 5 min
In a nutshell
  • Partition, cluster, select fewer columns and dry-run to cut BigQuery cost.
  • Maximum bytes billed and custom quotas cap query spending.
  • FlexRS for delay-tolerant Dataflow batch; Spot secondary workers for Dataproc.

What it is

Optimizing resources means paying only for what each business need requires, while making sure business-critical processes always get the capacity they need. For Hadoop and Spark workloads it also means choosing between persistent clusters that run all the time and job-based clusters created for each job.

Business needdeadline, freshnessRight service and sizeCost leversprune, autoscale, SpotProtected capacitycritical jobsReviewbilling and usage

How it works

Minimizing BigQuery costs

LeverHow it saves
Partitioning and clusteringQueries read fewer bytes
Select only needed columnsColumnar storage bills only columns read; avoid SELECT *
Table preview instead of SELECT * ... LIMITPreview reads no billed bytes
Dry runShows bytes a query would process before running it
Maximum bytes billedFails a query that would exceed a limit instead of charging for it
Custom quotasCaps query bytes per day per project or per user
Materialized views and aggregate tablesRepeated dashboards read small results
Long-term storageUnmodified tables cost about half after 90 days
On-demand versus capacity pricingPay per TiB scanned, or for slots when usage is high and steady
bq query --use_legacy_sql=false --dry_run \
  'SELECT customer_id, SUM(amount) FROM sales.order_facts GROUP BY customer_id'

bq query --use_legacy_sql=false --maximum_bytes_billed=1000000000000 \
  'SELECT customer_id, SUM(amount) FROM sales.order_facts GROUP BY customer_id'

Minimizing Dataflow and Dataproc costs

ServiceLevers
Dataflow batchFlexRS (--flexrs_goal=COST_OPTIMIZED) for jobs that can start within a delay of up to 6 hours; autoscaling; right machine types
Dataflow streamingStreaming Engine; a sensible maximum number of workers; avoid hot keys
DataprocJob-based clusters; autoscaling policies; Spot secondary workers; idle deletion
Cloud StorageStorage classes and lifecycle rules matched to access
Exam tip

"Nightly batch, results needed by morning, minimise cost" points to FlexRS for Dataflow or Spot secondary workers for Dataproc. Neither suits a job with a hard start time.

Ensuring resources for business-critical processes

RiskProtection
Ad hoc queries starve the nightly finance loadSeparate BigQuery reservations, with baseline slots for the critical workload
Spot VMs reclaimed during a deadline jobRun the critical path on standard primary workers; use Spot only for extra capacity
Quotas hit during month-end peaksRequest quota increases ahead of time; monitor quota usage
Shared clusters contended by many teamsSeparate job-based clusters per workload

Persistent or job-based Dataproc clusters

Job-based (ephemeral)Persistent
LifetimeCreated for a job or workflow, deleted afterAlways running
CostPay only while jobs runPay even when idle
DataMust live in Cloud Storage or BigQueryCan use HDFS, but Cloud Storage is still preferred
FitsScheduled batch jobs, per-team isolationInteractive notebooks, continuous shared use, low start latency
ToolsWorkflow templates with a managed cluster, Composer operators, Dataproc ServerlessAutoscaling policy, scheduled idle deletion as a safety net
gcloud dataproc clusters create nightly-etl \
  --region=europe-west1 \
  --num-workers=2 \
  --num-secondary-workers=8 --secondary-worker-type=spot \
  --max-idle=30m

--max-idle deletes the cluster after 30 minutes without jobs, so a forgotten cluster cannot run up a bill. Dataproc Serverless for Spark removes the cluster decision altogether for batch Spark jobs.

A worked example

A company runs one large persistent Dataproc cluster 24 hours a day, used for six hours of nightly jobs. Its BigQuery on-demand bill also spikes when analysts run SELECT * on a 40 TB table.

The engineer moves the nightly jobs to a workflow template that creates a cluster with Spot secondary workers and deletes it afterwards. For BigQuery, the table is partitioned by date and clustered, a custom quota caps each user's daily bytes, and dashboards use a materialized view.

Persistent cluster24 hoursJob based cluster6 hours, Spot workersSELECT star40 TBPartitioned and clusteredcustom quotaSmall scans

How the exam asks about this

  • "A Spark cluster sits idle 18 hours a day." Use job-based clusters created per workflow and deleted afterwards, keeping data in Cloud Storage.
  • "Prevent any single query from accidentally costing more than a set amount." Set maximum bytes billed on the query or a custom quota.
  • "A nightly Dataflow batch job is flexible on start time; reduce cost." Use FlexRS.

What to remember for the exam

  • Partition, cluster, select fewer columns and dry-run to cut BigQuery cost.
  • Maximum bytes billed and custom quotas cap query spending.
  • FlexRS for delay-tolerant Dataflow batch; Spot secondary workers for Dataproc.
  • Protect critical work with dedicated reservations and standard workers.
  • Job-based Dataproc clusters for scheduled batch; persistent for interactive or constant use.
Watch out

LIMIT on a query does not reduce bytes billed on a non-clustered table. Use table preview to look at data, and column selection and partition filters to cut cost.

Common mistakes

Running a persistent cluster for nightly jobs. You pay for idle hours; use job-based clusters.

Putting deadline-critical work entirely on Spot VMs. Reclaimed VMs delay the job; keep a standard core.

Buying capacity before fixing queries. Pruning and column selection often remove the need.

Check yourself

  • What does a BigQuery dry run tell you?

    The number of bytes the query would process, without running it or incurring charges.

  • When is a persistent Dataproc cluster the better choice?

    When it is used constantly or interactively, for example shared notebooks, where startup time matters and idle time is small.

  • What does Dataflow FlexRS trade for lower cost?

    Start-time flexibility: the batch job may be delayed by up to 6 hours and runs on a mix of standard and preemptible resources.

Topic 1 of 5 in Maintaining and automating data workloads

Test yourself on Maintaining and automating data workloads

The full Google Cloud Professional Data Engineer question bank has 733 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Google Cloud. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.