5.1 Cloud Pub/Sub Core Architecture: Topics, Subscriptions, and Message Lifecycle
Key Takeaways
- Cloud Pub/Sub decouples distributed message publishers and subscribers through a globally scaled architecture separating stateless HTTP/gRPC ingress routers from an underlying Colossus-backed, multi-zone replicated message store.
- Topics support one-to-many fan-out message distribution to independent subscriptions; each subscription manages its own independent consumption cursor, acknowledging or retaining messages without cross-subscription interference.
- Pull subscriptions provide lease-based message consumption with ackDeadlineSeconds (10 to 600 seconds) and automatic lease extension via StreamingPull bidirectional gRPC streams, while push subscriptions deliver messages via HTTPS webhooks authenticated with OIDC service account bearer tokens.
- The message lifecycle enforces end-to-end durability: a published message is synchronously replicated across at least two zones within the designated region before a publish acknowledgement is issued, guaranteeing zero data loss.
- Pub/Sub Seek allows instant rewind or fast-forward of a subscription's consumption state to a designated UTC timestamp or pre-created snapshot, enabling non-destructive replay of historical messages retained up to the 7-day retention limit.
5.1 Cloud Pub/Sub Core Architecture: Topics, Subscriptions, and Message Lifecycle
Exam Focus: The Google Cloud Professional Data Engineer exam rigorously tests your understanding of Pub/Sub as an asynchronous buffering layer in event-driven and streaming analytics pipelines. You must know how to choose between Pull and Push delivery models, tune acknowledgement deadlines (
ackDeadlineSeconds) to prevent duplicate processing, configure secure OpenID Connect (OIDC) authentication for serverless push endpoints, manage message retention boundaries, and execute Seek operations using timestamps and snapshots for disaster recovery and stream replay.
At the core of modern event-driven data platforms on Google Cloud lies Cloud Pub/Sub, an enterprise-grade, fully managed real-time messaging service. Pub/Sub acts as a resilient buffer between upstream producers (IoT edge devices, transaction logs, application clickstreams) and downstream consumers (Cloud Dataflow, BigQuery, Cloud Storage, Cloud Functions, and Kubernetes workloads). Understanding its internal architecture, decoupled storage mechanisms, and lifecycle states is essential for building fault-tolerant, high-throughput data architectures.
1. Distributed Pub/Sub Architecture: Routers, Forwarders, and Decoupled Storage
Unlike traditional messaging brokers that couple connection handling, message queueing, and local disk storage on individual virtual machine brokers, Cloud Pub/Sub is architected as a horizontally scalable, decoupled distributed system comprising two distinct planes:
-
Stateless Router Tier (Ingress and Connection Handling):
- Upstream publishers connect to Pub/Sub via globally routed Anycast IP addresses using high-performance gRPC or REST over HTTP/2 (
pubsub.googleapis.com). - Stateless router servers terminate client TLS connections, authenticate identity credentials via Cloud IAM, validate schema definitions (Avro or Protocol Buffers), and partition incoming message traffic.
- Because routers are entirely stateless, any router can handle requests from any publisher, eliminating hot-spotting at the connection tier and enabling instantaneous horizontal auto-scaling to millions of requests per second.
- Upstream publishers connect to Pub/Sub via globally routed Anycast IP addresses using high-performance gRPC or REST over HTTP/2 (
-
Storage and Forwarder Tier (Persistence and Delivery):
- Once validated by a router, messages are transferred to forwarder servers and persisted to Google's globally distributed storage layer, backed by Colossus (Google's next-generation distributed file system) and distributed consensus metadata logs.
- Multi-Zone Synchronous Durability Quorum: Before Pub/Sub issues a publish acknowledgement (
ACK) back to the publisher client, the message payload is synchronously replicated across storage nodes in at least two independent datacenter zones within the configured Google Cloud region. If an entire zone experiences hardware destruction milliseconds after publication, the message is preserved without loss. - Message Storage Policies (
allowedPersistenceRegions): Although Pub/Sub offers a single global endpoint, organizations subject to data sovereignty regulations (such as GDPR or HIPAA) can define strict storage policies on topics. This guarantees that message payloads are stored exclusively within explicitly permitted geographic boundaries (e.g.,europe-west1andeurope-west4), regardless of where publishers and subscribers are located globally.
+----------------------------------------------------------------------------------+
| CLOUD PUBSUB INTERNAL ARCHITECTURE |
+----------------------------------------------------------------------------------+
| |
| [ Publishers ] ──(Anycast gRPC)──> [ Stateless Ingress Routers ] |
| │ |
| (Route & Partition) |
| ▼ |
| [ Forwarder Server Tier ] |
| │ |
| ┌─────────────────────┴─────────────────────┐ |
| ▼ ▼ |
| [ Zone A Storage Node ] [ Zone B Storage Node ] |
| (Colossus) (Colossus) |
| │ │ |
| └───────────── Synchronous Quorum ──────────┘ |
| │ |
| (Publish ACK returned) |
| ▼ |
| [ Independent Subscriptions ] |
| ├── Subscription 1 (Pull Engine) |
| └── Subscription 2 (Push Webhook) |
+----------------------------------------------------------------------------------+
2. Topics, Subscriptions, and the Fan-Out Paradigm
Cloud Pub/Sub strictly decouples the publishing entity from the consuming entity through Topics and Subscriptions:
- Topic: A named resource to which publishers send messages. Publishers have zero knowledge of which applications consume the messages or how many consumers exist.
- Subscription: A named resource representing an independent stream of messages from a specific topic to be delivered to a consuming application. A subscription maintains its own cursor, tracking which messages have been delivered and acknowledged.
One-to-Many Fan-Out Architecture
A single Pub/Sub topic can be bound to multiple independent subscriptions. When a message is published to the topic, Pub/Sub ensures that a copy or reference of that message is delivered to every attached subscription:
┌──> [ Subscription A: Dataflow ] ──> BigQuery Analytics
[ E-Commerce Topic ] ─────┼──> [ Subscription B: Cloud Run ] ──> Fraud Detection
└──> [ Subscription C: GCS Direct ] ──> Cold Compliance Lake
- Decoupled Consumer Isolation: If
Subscription Aexperiences a consumer failure and falls 4 hours behind in message processing,Subscription Bcontinues processing messages in real time with sub-100 millisecond latency. Consumer lag on one subscription has zero impact on the throughput, delivery latency, or cursor state of any other subscription. - Competing Consumers Pattern: When multiple worker instances (e.g., 20 pods running on Google Kubernetes Engine) connect to a single subscription (
Subscription A), Pub/Sub distributes incoming messages among those 20 workers. Each individual message is delivered to only one worker instance within that subscription. This allows consumer pools to scale out horizontally to match incoming throughput without requiring manual partition rebalancing.
Pub/Sub vs. Traditional Partitioned Brokers (Apache Kafka)
| Feature / Metric | Google Cloud Pub/Sub | Apache Kafka / Managed Kafka |
|---|---|---|
| Scaling Unit | Dynamic, automatic per-message allocation; no manual sharding | Static partitions per topic; scaling requires adding partitions |
| Consumer Rebalancing | Zero rebalancing pause; forwarders distribute messages dynamically | Group coordinator rebalancing pauses consumption during scaling |
| Ordering Scope | Ordered per orderingKey globally; independent of partitions | Ordered strictly within a physical partition |
| Operational Overhead | Serverless; zero capacity planning, broker provisioning, or disk tuning | Requires sizing brokers, disk IOPS, JVM heap, and ZooKeeper/KRaft |
| Global Reach | Global Anycast endpoint; publish anywhere, consume anywhere | Typically deployed in single VPC/cluster; cross-region requires MirrorMaker |
3. Pull Subscriptions: Lease Management, Ack Deadlines, and StreamingPull
In a Pull subscription, the subscriber application initiates requests to retrieve messages from Cloud Pub/Sub. Managing message leases and acknowledgement deadlines is one of the most heavily tested topics on the exam.
Acknowledgement Deadlines and Lease Mechanics
When Pub/Sub delivers a message to a pulling subscriber, it starts an internal timer called the acknowledgement deadline (ackDeadlineSeconds):
- Configurable Range: 10 seconds to 600 seconds (default: 10 seconds).
- The In-Flight Lease: While the timer is running, the message is considered leased to that subscriber and is hidden from other consumers on the same subscription.
- Acknowledgement (
ack): If the subscriber successfully processes the message and sends anacknowledgeRPC before the deadline expires, Pub/Sub permanently marks the message as acknowledged and evicts it from active delivery. - Negative Acknowledgement (
nack): If the subscriber encounters a transient processing error (e.g., database connection pool full), it can send anack(or callmodifyAckDeadlinewithackDeadlineSeconds = 0). Pub/Sub immediately terminates the lease and makes the message available for redelivery to another worker. - Deadline Expiration (Automatic Redelivery): If the worker crashes or processing takes longer than
ackDeadlineSecondswithout an acknowledgement or lease extension, Pub/Sub assumes the worker died. The lease expires, and Pub/Sub automatically redelivers the message to another available worker.
Exam Trap — Duplicate Processing from Low Ack Deadlines: If a subscriber takes 45 seconds to process a batch of records, but the subscription's
ackDeadlineSecondsis set to 10 seconds, Pub/Sub will redeliver the exact same message every 10 seconds to different workers in the pool. This causes duplicate processing storms, resource exhaustion, and high database write contention, even though the application has not crashed. The solution is either to increaseackDeadlineSecondsor use client library automatic lease renewal.
Synchronous Pull vs. StreamingPull
-
Synchronous Pull (
projects.subscriptions.pullRPC):- A standard request-response HTTP/gRPC call where the client requests a batch of messages (
maxMessages = 100). - If no messages are available, the call blocks until a timeout occurs or returns an empty list.
- Drawbacks: High network polling overhead, increased latency, and requires manual lease management. If processing exceeds
ackDeadlineSeconds, the developer must manually callmodifyAckDeadlinein separate background threads. - Best Use Case: Low-volume batch jobs, scheduled Cloud Functions running on Cloud Scheduler, or administrative debugging scripts.
- A standard request-response HTTP/gRPC call where the client requests a batch of messages (
-
StreamingPull (
projects.subscriptions.streamingPullbidirectional gRPC stream):- Establishes a persistent, full-duplex, bidirectional gRPC connection between the subscriber client and Pub/Sub forwarders.
- Messages are streamed to the client with sub-10ms delivery latency as soon as they are committed to storage.
- Built-in Flow Control: Subscribers declare limits on unacknowledged messages (
maxOutstandingMessagesandmaxOutstandingBytes). Once these limits are reached, the Pub/Sub forwarder pauses streaming to that client until existing messages are acknowledged, preventing out-of-memory (OOM) crashes. - Automatic Lease Extension (Heartbeating): The official Google Cloud client libraries automatically maintain an asynchronous background thread that periodically sends
modifyAckDeadlinerequests to Pub/Sub for all messages currently being processed. The library extends the lease up to a configured maximum duration (e.g.,maxDurationPerLeaseExtension = 1 hour), ensuring that long-running tasks are not prematurely redelivered to other workers. - Best Use Case: All production high-throughput streaming pipelines, including Apache Beam on Cloud Dataflow and high-performance microservices.
4. Push Subscriptions: Webhooks, Serverless Endpoints, and Authentication
In a Push subscription, Cloud Pub/Sub acts as an HTTPS client, initiating HTTPS POST requests to deliver messages to a pre-configured webhook endpoint.
Push Delivery Mechanics
- Target Endpoints: Publicly accessible HTTPS URLs, including Cloud Run services, App Engine endpoints, Google Kubernetes Engine Ingress controllers, or third-party webhooks.
- Delivery Envelope: Pub/Sub wraps the message in a standardized JSON payload:
{ "message": { "data": "ZXhhbXBsZSBwYXlsb2Fk", "attributes": { "source": "pos_terminal", "store_id": "982" }, "messageId": "10482910482910", "publishTime": "2026-09-15T12:00:00.000Z" }, "subscription": "projects/my-prod/subscriptions/orders-push-sub" } - Acknowledgement via HTTP Status Codes:
- Success (
200,201,202,204): Pub/Sub considers the message successfully delivered and marks it acknowledged. - Failure / Retry (Any other code, e.g.,
500,503, or HTTP timeout): Pub/Sub considers delivery failed and retries delivery using exponential backoff (configurable minimum and maximum retry delays, ranging from 10 seconds to 600 seconds).
- Success (
OIDC Authentication for Secure Serverless Webhooks
Production push endpoints (such as Cloud Run or Cloud Functions) must never be left unauthenticated (allUsers). To deliver messages securely to private endpoints, Cloud Pub/Sub uses OpenID Connect (OIDC) authentication:
[ Cloud Pub/Sub ]
│ 1. Impersonates User-Managed Service Account
▼
[ Cloud IAM Credentials API ]
│ 2. Mints short-lived OIDC JSON Web Token (JWT) with Audience = Cloud Run URL
▼
[ Cloud Pub/Sub ]
│ 3. Sends HTTPS POST with Authorization: Bearer <OIDC_JWT>
▼
[ Cloud Run Service (Private) ]
4. Validates JWT signature, expiration, and audience claim
5. Executes business logic and returns HTTP 200 OK
Required IAM Configuration
- Create a User-Managed Service Account: Dedicated identity for push delivery (e.g.,
pubsub-push-invoker@my-project.iam.gserviceaccount.com). - Grant Invoker Role to Service Account: Assign
roles/run.invokeron the target Cloud Run service to the push service account. - Grant Token Creator to Pub/Sub Service Agent: The Google-managed Pub/Sub service agent (
service-[PROJECT_NUMBER]@gcp-sa-pubsub.iam.gserviceaccount.com) must be granted theroles/iam.serviceAccountTokenCreatorrole on the push service account. This allows Pub/Sub to mint OIDC tokens on behalf of the service account. - Configure Subscription with OIDC: In the push subscription settings, enable authentication, select the service account, and set the Audience (
aud) to the target service's base HTTPS URL.
5. Comparative Evaluation: Pull vs. StreamingPull vs. Push Subscriptions
| Architectural Attribute | Synchronous Pull | StreamingPull (gRPC) | Push Subscription (HTTPS) |
|---|---|---|---|
| Communication Protocol | HTTP/1.1 or gRPC request/response | Persistent bidirectional gRPC stream | Outbound HTTPS POST webhook |
| Delivery Latency | High (bounded by polling intervals) | Sub-10ms (instantaneous delivery) | Low to moderate (HTTPS handshake overhead) |
| Throughput Capacity | Moderate; bounded by request rate | Massive (> 1,000,000 msg/sec) | Moderate; bounded by target webhook concurrency |
| Network Ingress Requirements | Outbound HTTPS/gRPC from consumer | Outbound persistent gRPC to Google | Target endpoint must be reachable via public HTTPS |
| Consumer Scalability | Auto-scaled based on queue depth metrics | Auto-scaled via GKE/Compute based on metrics | Auto-scaled natively by Cloud Run / Cloud Functions |
| Lease Management | Manual modifyAckDeadline calls | Automatic lease renewal via client library | Bounded strictly by HTTP connection timeout (max 600s) |
| Authentication Model | Standard GCP IAM bearer tokens | Standard GCP IAM bearer tokens | OpenID Connect (OIDC) JWT injected in headers |
| Primary Use Cases | Batch ingestion, scheduled tasks | Dataflow pipelines, high-volume microservices | Serverless event triggers, Cloud Run webhooks |
6. Complete Message Lifecycle and Durability Guarantees
A Pub/Sub message transitions through seven distinct lifecycle phases from creation to eventual eviction:
+-----------------------------------------------------------------------------------+
| PUBSUB MESSAGE LIFECYCLE PHASES |
+-----------------------------------------------------------------------------------+
| 1. PUBLISH Publisher transmits payload and attributes to topic |
| │ |
| 2. ROUTE & INGRESS Ingress router validates schema, sets messageId & timestamp |
| │ |
| 3. SYNCHRONOUS REPL Replicated across >=2 zones in region; Publish ACK returned |
| │ |
| 4. FAN-OUT QUEUE Forwarders attach message reference to each subscription |
| │ |
| 5. DELIVERY & LEASE Delivered to consumer; ackDeadlineSeconds timer starts |
| │ |
| ┌──────────────┴──────────────┐ |
| ▼ ▼ |
| 6a. ACKNOWLEDGED 6b. EXPIRED / NACKED |
| Subscriber sends ack; Lease expires or nack sent; |
| evicted from delivery. redelivered to alternate worker. |
| │ |
| 7. RETENTION / EVICTION |
| If retainAckedMessages=true, preserved until messageRetentionDuration. |
+-----------------------------------------------------------------------------------+
Message Retention Boundaries
- Unacknowledged Message Retention: Cloud Pub/Sub retains all unacknowledged messages for a configurable period between 10 minutes and 7 days (default: 7 days). If a subscriber is completely offline for 6 days, no messages are lost; once the subscriber restarts, it resumes consuming the backlogged messages.
- Acknowledged Message Retention (
retainAckedMessages): By default, when a message is acknowledged, Pub/Sub immediately marks it for deletion. However, subscriptions can enableretainAckedMessages = trueand configure a retention duration up to 7 days. This preserves acknowledged messages in storage, unlocking the ability to replay historical streams. - Topic-Level Retention: Retention can also be configured at the topic level (
messageRetentionDuration), ensuring that all attached subscriptions inherit the ability to inspect historical data regardless of when each individual subscription was provisioned.
7. Message Replay: Seek by Timestamp and Subscription Snapshots
A frequent requirement in enterprise streaming systems is recovering from downstream failures. For example, if a deployment bug causes a downstream service to corrupt database records for two hours before being rolled back, how do you reprocess the original messages without re-publishing them from upstream producers?
Pub/Sub solves this through the Seek operation, which resets the message consumption cursor of a subscription:
1. Seek by Timestamp
- An operator or automated script rewinds the subscription cursor to any specific UTC timestamp within the retention window:
gcloud pubsub subscriptions seek analytics-sub \ --time="2026-09-15T10:00:00Z" - Mechanics: Pub/Sub inspects all messages published after the specified timestamp. Any messages that were previously acknowledged are reset back to unacknowledged status. Subscribed consumers immediately begin re-receiving and processing those historical messages in chronological order.
2. Seek by Subscription Snapshot
- A Snapshot captures the point-in-time acknowledgement state of a subscription at a specific instant:
# Create a point-in-time snapshot prior to deploying application update gcloud pubsub snapshots create pre-deploy-snap \ --subscription=analytics-sub - Snapshots retain unacknowledged messages for up to 7 days from the moment of snapshot creation, even if the subscription's standard retention expires.
- If a production deployment fails, the team can instantly revert the subscription to its exact pre-deployment state:
gcloud pubsub subscriptions seek analytics-sub \ --snapshot=pre-deploy-snap - Snapshots can also be used to initialize new subscriptions with the exact cursor state of an existing subscription, enabling parallel pipeline testing against production traffic.
8. Realistic Exam Scenarios & Architecture Pitfalls
| Operational Scenario | Architecture Anti-Pattern | Correct Google Cloud Architecture |
|---|---|---|
| High-Throughput Streaming Ingestion<br>An IoT streaming pipeline processes 50,000 sensor readings/sec. Workers use Synchronous Pull with default 10s ack deadlines, resulting in massive redelivery storms and high VM CPU utilization. | Increasing VM instance count and keeping Synchronous Pull. | Migrate consumers to StreamingPull using official Google Cloud client libraries. StreamingPull establishes persistent gRPC streams with automatic lease renewal and flow control, eliminating premature deadline expiration and duplicate processing. |
| Secure Push Webhooks to Cloud Run<br>A push subscription targeting a private Cloud Run service fails continuously with HTTP 403 Forbidden errors. | Making the Cloud Run service public by granting roles/run.invoker to allUsers. | Enable OIDC authentication on the push subscription. Grant roles/iam.serviceAccountTokenCreator to the Pub/Sub service agent, and grant roles/run.invoker on the Cloud Run service to the subscription's dedicated user-managed service account. |
| Downstream Pipeline Recovery<br>A bug introduced during a midnight release corrupted analytical aggregations. The team needs to reprocess all messages from the last 6 hours. | Asking upstream IoT devices to resend the last 6 hours of telemetry data. | Enable retainAckedMessages on the subscription with a 7-day retention period. Execute gcloud pubsub subscriptions seek [SUB_NAME] --time="[UTC_TIMESTAMP]" to rewind the cursor and reprocess historical messages non-destructively. |
| Data Sovereignty Compliance<br>A European healthcare provider requires that patient telemetry never be persisted in US datacenters, but publishers operate globally. | Relying on client-side routing logic to choose different regional endpoints. | Configure a Message Storage Policy (allowedPersistenceRegions) on the Pub/Sub topic restricted to europe-west1 and europe-west3. Ingress routers accept messages globally but strictly guarantee storage persistence in European zones only. |
A data engineering team operates a Python worker application that consumes messages from a Cloud Pub/Sub subscription using Synchronous Pull (the 'pull' RPC). Each message triggers a machine learning inference task that takes approximately 45 seconds to complete. The subscription's 'ackDeadlineSeconds' is configured to the default value of 10 seconds. In production, engineers observe that the same message is processed 4 to 5 times by different worker instances, leading to duplicate database records and severe resource contention. How should the team resolve this duplicate processing issue without changing the business logic?
A financial enterprise uses a Cloud Pub/Sub push subscription to deliver transaction alerts to a Cloud Run service. The Cloud Run service has its ingress set to 'Internal' and authentication configured to 'Require authentication'. Deliveries are failing continuously, with Cloud Monitoring reporting HTTP 403 Forbidden errors, and unacknowledged messages are accumulating in the subscription. Which configuration steps are required to establish secure, authenticated delivery from Pub/Sub to Cloud Run?
An enterprise analytics pipeline consumes billing events from a Cloud Pub/Sub subscription named 'billing-sub'. At 10:00 UTC, a faulty microservice deployment began writing corrupted currency data into downstream analytical databases. The bug was identified and fixed in code at 11:30 UTC. The subscription has 'retain_acked_messages' enabled with a 7-day retention period, but the engineering team did not create a subscription snapshot prior to the incident. How should the team replay the exact message sequence published between 10:00 UTC and 11:30 UTC without losing unacknowledged messages currently in the pipeline?
A multinational healthcare organization ingests patient telemetry globally into Cloud Pub/Sub. Due to strict European Union GDPR data residency compliance, patient data generated by European hospitals must never be stored on physical media outside the European Union. However, hospital edge gateways connect to Pub/Sub via its global Anycast DNS endpoint ('pubsub.googleapis.com'). What configuration must the data engineering team apply to guarantee compliance with data residency requirements without disrupting global publishing?