GCP & Backend Engineer Guide: Event-Driven Serverless ETL with Cloud Functions Gen2 & BigQuery
Running a 24/7 Apache Airflow cluster or dedicated VM fleet just to ingest hourly partner CSV/Parquet drops from Cloud Storage into BigQuery wastes hundreds of dollars in idle compute. Yet naive Cloud Functions ETL scripts often fail in production due to duplicate Eventarc triggers, out-of-memory pandas crashes on 400MB files, and unpartitioned BigQuery load jobs. As a GCP engineer and backend engineer, I architect zero-ops, idempotent serverless ETL pipelines on Google Cloud Functions Gen2 (Cloud Run-backed) and BigQuery.
1. Streaming Chunked Ingestion & Eventarc Idempotency
Eventarc and Cloud Storage notifications guarantee at-least-once delivery, meaning the same google.cloud.storage.object.v1.finalized event can fire twice. By deriving a deterministic BigQuery job_id from the GCS object's generation number (metageneration / generation), duplicate invocations become harmless no-ops:
# main.py (Cloud Functions Gen2)
import hashlib
import functions_framework
from cloudevents.http import CloudEvent
from google.api_core.exceptions import Conflict
from google.cloud import bigquery
bq_client = bigquery.Client()
@functions_framework.cloud_event
def ingest_gcs_parquet_to_bq(cloud_event: CloudEvent) -> None:
data = cloud_event.data
bucket_name = data["bucket"]
object_name = data["name"]
generation = str(data["generation"])
if not object_name.startswith("incoming/settlements/") or not object_name.endswith(".parquet"):
return
uri = f"gs://{bucket_name}/{object_name}"
digest = hashlib.sha256(f"{uri}:{generation}".encode("utf-8")).hexdigest()[:32]
deterministic_job_id = f"etl_settlement_{digest}"
job_config = bigquery.LoadJobConfig(
source_format=bigquery.SourceFormat.PARQUET,
write_disposition=bigquery.WriteDisposition.WRITE_APPEND,
time_partitioning=bigquery.TimePartitioning(
type_=bigquery.TimePartitioningType.DAY,
field="settlement_date",
),
)
try:
load_job = bq_client.load_table_from_uri(
uri,
"prod-warehouse.finance.partner_settlements_staging",
job_id=deterministic_job_id,
job_config=job_config,
)
load_job.result()
except Conflict:
# Job with deterministic_job_id already submitted by an earlier delivery
return
2. Atomic MERGE Upsert from Staging to Production Fact Table
After loading raw Parquet files into an expiring staging table, a parameterized BigQuery MERGE statement deduplicates records by primary key into the production partitioned table within a single atomic transaction.