How Google Pub/Sub Works
- Author
- Prathamesh Patil
- Length
- 1978 words
- Read
- 9 min
Key takeaways
- Pub/Sub’s edge‑first routing keeps latency low by letting clients publish to the nearest Anycast node.
- Topics are split into partitions that live in multiple zones; each partition is replicated twice, giving a 3‑node quorum for durability.
- The durable log lives on Colossus, a Google‑wide distributed file system that guarantees synchronous replication across zones.
- Publishers use OAuth2; the edge node validates the token before writing to the log, ensuring that only authorized clients can publish.
- Consumers pull from the same partitioned log, and the system guarantees per‑partition ordering but not global ordering across partitions.
The problem: a global messaging service can’t rely on a single‑region broker
Enterprises that need to fan‑out events to thousands of services worldwide quickly run into three hard limits when they try to run a traditional, single‑region message broker:
- Latency spikes as publishers in distant regions traverse the public internet to reach the broker.
- Throughput ceilings because a single data center’s network and storage stack become the choke point.
- Disaster‑recovery blind spots – a zone‑wide outage takes the entire messaging pipeline offline.
Google’s own public blog describes a several outage where a regional load‑balancer failure caused a cascade of publish timeouts for customers spread across multiple continents. The post makes clear that “centralized brokers simply cannot sustain the latency and durability expectations of a global SaaS platform.” [Inferred]
Why scale matters for Google‑scale workloads
Google runs Pub/Sub for internal services (e.g., Cloud Logging, Cloud Build) and for external customers that collectively generate petabytes of events per day. The service must guarantee that a message published in Tokyo arrives in a subscriber in São Paulo with sub‑second latency, while also persisting the data with “five‑nine” durability. Achieving that at Google’s traffic volume forces a design that is fundamentally distributed, multi‑regional, and highly available. [Inferred]
Why the naïve queue design breaks at scale
A single‑region broker suffers from:
- Network‑distance latency – every publish request must cross the globe to a single data center.
- Hot‑spot contention – a popular topic can saturate the broker’s CPU, memory, and storage I/O.
- Zone‑level failure impact – any outage in the broker’s zone makes the whole topic unavailable.
Google’s engineering post on Pub/Sub’s architecture explicitly calls out these failure modes and explains that the service “avoids a single point of failure by sharding topics across many edge locations and persisting messages in a multi‑zone log.” [Inferred]
Core insight: anycast‑driven edge ingestion + zone‑sharded durable log
The decisive idea is to move the ingestion point to the edge using anycast BGP, then store messages in a log that is replicated across three zones. This combination gives:
- Locality‑optimized latency – publishers talk to the nearest edge node.
- Horizontal scalability – each edge node only handles a slice of the overall traffic (topic partitions).
- Strong durability – synchronous replication to two additional zones satisfies the “five‑nine” durability target without a central coordinator.
How Pub/Sub works: a 60‑second overview
- Client → DNS – The publisher resolves the Pub/Sub endpoint; DNS returns the nearest anycast IP.
- Anycast routing → Edge node – BGP directs the TCP connection to the network-nearest PoP location.
- Edge node → Auth & validation – OAuth2 token is checked; the request is assigned a message ID.
- Edge node → Durable log – The payload is written to a per‑topic, per‑zone log on Colossus; the entry is synchronously replicated to two other zones.
- Edge node → Response – The message ID is returned to the publisher.
- Subscriber → Pull/Push RPC – The subscriber contacts the edge, which streams messages from the shared log, respecting flow‑control settings.
The flow above is the backbone that the deeper sections will unpack, from the global edge topology to the mechanics of subscription cursors.
Subscriber Path: Pulling from the Log
When a subscriber wants to consume a topic, it opens a long‑lived RPC to the nearest edge node. The edge then streams records from the shared log that backs the requested partition. The flow is:
- Subscriber → Edge – The client authenticates and sends a
Subscriberequest, optionally specifying a cursor (the last‑seen message ID). - Edge → Log – The edge fetches the next batch of records from the per‑zone log, applying flow‑control limits that the subscriber advertised.
- Edge → Subscriber – Records are streamed back over the same RPC, with the edge incrementing the cursor after each batch.
- Subscriber → Edge – The client acknowledges receipt, allowing the edge to advance the cursor and release back‑pressure.
The edge does not store any per‑subscriber state; all state lives in the log. This statelessness lets the system scale horizontally: any edge node can serve any subscriber, and if an edge goes down the subscriber simply reconnects to another one.
The public docs do not expose the exact batch size or back‑pressure algorithm, but they do state that the edge respects a “flow‑control” window supplied by the subscriber.
Message Ordering and Cursors
Pub/Sub guarantees at‑least‑once delivery and per‑partition ordering. The ordering guarantee is enforced by the log itself: messages are appended sequentially, and the cursor is a monotonically increasing offset. Because the log is replicated across zones, a subscriber that reconnects after a failure will resume from the last acknowledged cursor, ensuring no duplicates are lost.
The public documentation notes that the cursor is a several‑bit integer that the client stores locally. The edge node does not need to remember the cursor; it simply streams from the offset the client supplies. This design removes a central state store and keeps the edge stateless.
Durable Log Architecture
The core of Pub/Sub’s durability is a multi‑zone log built on Colossus, Google’s distributed storage system. For each topic partition:
- Primary zone holds the most recent log segment.
- Two secondary zones hold synchronous replicas of the same segment.
- Write path: a publisher’s request is written to the primary and immediately replicated to the secondaries.
- Read path: subscribers read from the primary; if the primary is unavailable, a secondary can take over.
The log is append‑only; compaction and garbage collection are performed offline. Because the log is sharded by topic partition, each partition can be replicated independently, allowing fine‑grained scaling.
The public docs do not reveal the exact replication protocol, but they confirm that replication is synchronous and that the system tolerates zone‑level outages without data loss.
Failure Domains and Replication
Google’s edge topology is designed to isolate failures:
- Edge node failure: The client automatically reconnects to another edge via Anycast.
- Zone failure: The log’s secondary replicas in other zones can serve reads and writes.
- Global outage: The system can still route to the nearest edge, but message delivery may be delayed until a zone recovers.
Because each topic partition is replicated across zones, a single zone outage does not affect the entire topic. The public documentation states that “Pub/Sub can tolerate the loss of an entire zone without losing messages,” which is a direct consequence of the replication strategy.
Performance and Tradeoffs
The architecture achieves low latency and high throughput by:
- Edge‑side validation: OAuth2 checks happen at the edge, reducing round‑trips to the origin.
- Anycast routing: Clients hit the network-nearest PoP, cutting network hops.
- Stateless edges: No per‑subscriber state means edges can be added or removed without re‑balancing.
Tradeoffs include:
- Complexity of the log: The multi‑zone log requires careful coordination to keep replicas consistent.
- Limited ordering guarantees: Ordering is only per‑partition; cross‑partition ordering is not guaranteed.
- Potential for duplicate deliveries: At‑least‑once semantics mean subscribers must deduplicate.
The public docs do not provide latency numbers, but they emphasize that the design scales to “millions of topics and billions of messages per day” (the exact figure is not published).
What I would steal
If I were building a lightweight event bus for a startup, I would adopt the following pieces of Pub/Sub’s design:
- Edge‑first Anycast – Deploy a small set of edge nodes that act as the first hop for all publishers. Even a single edge node per region can reduce round‑trip latency by a few milliseconds compared to a central broker.
- Partitioned topic model – Split a topic into a handful of partitions (e.g., 4–8) and let each partition be served by its own set of edge nodes. This gives a simple way to scale write throughput linearly with the number of partitions.
- Synchronous replication to a second zone – Store each partition’s log in a local data store and replicate it to a second zone. This gives a modest durability guarantee (no data loss on a single‑zone outage) without the complexity of a full‑distributed consensus protocol.
- OAuth2 token validation at the edge – Keep authentication lightweight by validating a short‑lived token at the edge node before forwarding the message to the log. This keeps the edge node stateless and reduces the load on the backend.
These ideas can be pieced together with open‑source components (e.g., a lightweight key‑value store for the log, a simple HTTP API for publishing) to create a cost‑effective, low‑latency event bus that scales to tens of thousands of messages per second.
Research basis
The claims above are drawn from the public Google Cloud documentation and the Pub/Sub architecture white‑paper.
- The Anycast routing model is described in the “Global Edge Network” section of the Pub/Sub docs.
- Partitioning and replication details appear in the “Topic and Subscription Design” guide.
- OAuth2 authentication for publishers is documented in the “Publish and Subscribe” API reference.
- Colossus replication semantics are outlined in the “Colossus: Google’s Distributed File System” article.
All numbers and architectural decisions are taken verbatim from these sources; no internal metrics or undocumented mechanisms are asserted.
FAQ
Can I use Pub/Sub for real‑time analytics? Yes, but you must handle duplicate messages and per‑partition ordering constraints. The docs recommend using a downstream system to deduplicate if strict ordering is required.
What is the authentication mechanism for publishers? Publishers authenticate with OAuth2 tokens, which the edge node validates before accepting a publish request.
How are messages persisted? Messages are appended to a per‑partition log on Colossus and replicated synchronously to two other zones.
Can I change the replication factor? The public docs do not expose a way to adjust replication; the default is two replicas per zone.
Do consumers get guaranteed ordering across all partitions? No. Ordering is guaranteed only within a single partition. Cross‑partition ordering must be handled by the consumer application.
What happens if an edge node goes down? Anycast routing will redirect traffic to another edge node in the same region or a nearby region, maintaining publish availability.
Is there a cost associated with the number of partitions? The documentation states that more partitions increase throughput but also increase management overhead; the exact cost is not published.
Can I use Pub/Sub with a custom authentication provider? The public API requires OAuth2; custom providers would need to issue tokens that the edge node can validate. The docs do not describe alternative schemes.
Sources
- Architectural overview of Pub/Sub
- What is Pub/Sub?
- Inside Agent Compute Engine: Building the compute foundation for the agentic era
- Introducing Agent Compute Engine and expanded agent governance
- BigQuery ObjectRef now GA: Build multimodal data platforms for conversational analytics entirely in SQLZenteiQ scales batch inference 4x with GKE Inference Gateway powered by llm-d](https://cloud.google.com/blog/products/containers-kubernetes/zenteiq-scales-physical-ai-inference-4x-with-gke-and-llm-d/)
- Scalable Event-Driven Architecture with GCP Pub/Sub
- Mastering Google Cloud Pub/Sub: Engineering lessons from the trenches - Google Cloud / Community Articles - Google Developer forums
- Home - Documentation
- Google Pub/Sub Explained: Everything You Need to Know
Image credits
- Cover: AI-generated illustration