Almost every distributed system ends up with an asynchronous messaging layer. One service publishes an event, several others consume it at their own pace, and the publisher doesn’t wait for them. On Google Cloud, that layer is Pub/Sub. On AWS, it’s usually two services wired together: SNS for fan-out and SQS for durable queues.
The building blocks are cut differently, and it shows in your code: how you publish, acknowledge, retry, and order messages. This article walks through Pub/Sub in Java, maps it onto SNS and SQS, and shows how MultiCloudJ, an open-source Java SDK from Salesforce, puts both behind one portable API.
The Google Cloud Pub/Sub Model
Pub/Sub has two core resources:
- Topic. A named channel that publishers send messages to. Topics are global resources: a publisher in
us-east1and a subscriber ineurope-west1use the same topic without any cross-region plumbing. - Subscription. A named, durable stream of messages from one topic. Each subscription gets its own copy of every message published after it was created. Several subscribers on the same subscription share the work, and each message goes to one of them.
So fan-out means many subscriptions, and load-balancing means many subscribers on one subscription, with no extra wiring for either.

Delivery: Pull, Push, or Export
- Pull. The subscriber asks for messages. The Java client library uses StreamingPull, a long-lived bidirectional gRPC stream, so messages flow as soon as they arrive.
- Push. Pub/Sub sends each message as an HTTPS POST to an endpoint you own, such as a Cloud Run service. It can attach a Google-signed OIDC token so the endpoint can verify the caller. A success status (102, 200, 201, 202, or 204) is an ack.
- Export. BigQuery and Cloud Storage subscriptions write messages straight into a table or bucket, with no consumer code at all.
Acknowledgment and the Ack Deadline
Delivery is at-least-once. A received message must be acked within the subscription’s ack deadline (10 seconds by default, up to 600), or Pub/Sub redelivers it. ModifyAckDeadline extends the deadline, and setting it to 0 is a nack: the message becomes available for redelivery right away. The Java client extends leases in the background while your callback runs, for up to an hour by default.
A subscription retains unacked messages for 7 days by default, configurable from 10 minutes to 31 days. Topics can also retain messages for up to 31 days, so a new or rewound subscription can replay history with Seek.
Publishing and Consuming in Java
The native publisher batches messages and returns a future per message. build() throws IOException:
The subscriber registers a callback that acks or nacks each message:
Both use Application Default Credentials, so on GKE, Cloud Run, or GCE, no keys appear in code.
The Features That Matter in Production
- Ordering keys. Messages that share an ordering key are delivered in publish order, as long as the subscription has message ordering enabled, and the messages are published in the same region. Messages with different keys can still be processed in parallel.
- Dead-letter topics. After a configurable number of delivery attempts (5 to 100), Pub/Sub forwards the message to a dead-letter topic instead of retrying forever.
- Filtering. A subscription can carry a filter on message attributes, such as
attributes.type = "order.created", so consumers only get what they care about. - Exactly-once delivery. Pull subscriptions can turn on exactly-once delivery. Pub/Sub then won’t redeliver a message once its ack has succeeded, and the Java client’s
AckReplyConsumerWithResponsetells you whether the ack went through. The guarantee holds when subscribers connect in the same region.
AWS for Comparison: SNS Plus SQS
SNS is the topic: it pushes each message to its subscribers immediately, and consumers can’t poll it. SQS is the durable queue: consumers poll it, and a message stays until someone deletes it. For Pub/Sub-style fan-out, you subscribe one SQS queue per consumer group to an SNS topic.

The same topology on AWS: an SNS topic fans out to one SQS queue per consumer group.
The concepts map closely; the table below lines them up. A few AWS details have no Pub/Sub equivalent:
- Each queue needs an access policy that lets the topic send to it.
- SQS has no ack call. You
DeleteMessagewith the receipt handle fromReceiveMessage. - There is no streaming pull.
ReceiveMessagelong-polls for up to 20 seconds. - FIFO adds a 5-minute deduplication window keyed by
MessageDeduplicationId. - Unless raw message delivery is on, each SQS body is an SNS JSON envelope with your payload in its
Messagefield.
Side by Side
| Concept | Google Cloud Pub/Sub | AWS SNS + SQS |
|---|---|---|
| Services involved | One | Two, wired together |
| Fan-out | Multiple subscriptions on a topic | SNS topic subscribed by multiple SQS queues |
| Durable consumer stream | Subscription | SQS queue |
| Consume | StreamingPull, push, or export to BigQuery/GCS | SQS polling (max 10 per call), or SNS push to Lambda/HTTPS |
| Ack | Acknowledge |
DeleteMessage |
| Ack window | Ack deadline: 10s default, 600s max | Visibility timeout: 30s default, 12h max |
| Nack | ModifyAckDeadline to 0 |
ChangeMessageVisibility to 0 |
| Ordering | Ordering keys on any topic | FIFO topics and queues with MessageGroupId |
| Dead letters | Dead-letter topic, 5–100 attempts | Redrive policy, maxReceiveCount |
| Max message size | 10 MB | 1 MiB (SQS), 256 KiB by default on SNS, configurable to 1 MiB |
| Retention | Subscription: 7 days by default, up to 31. Topic: up to 31 days | SQS: 4 days by default, up to 14. SNS FIFO archive: up to 365 days |
| Replay | Seek to a timestamp or snapshot, on any topic |
SNS FIFO archive and replay only. Standard topics and SQS can’t replay |
- Pub/Sub has fewer moving parts. A new consumer group is one subscription. On AWS it’s a queue, a subscription, and a queue policy.
- Ordering is cheaper to adopt on Pub/Sub. You add a key to a topic you already have. On AWS, you need FIFO resources from the start, because you can’t convert a standard queue to FIFO.
- SQS gives you a longer ack window. Long-running jobs fit the 12-hour visibility timeout. On Pub/Sub, you rely on the client extending the lease.
- Replay is broader on Pub/Sub.
Seekworks on any subscription, which makes reprocessing after a bad deploy a routine operation. On AWS, only FIFO topics with archiving enabled can replay.
One Interface for Both: MultiCloudJ
If your service runs on both clouds, these differences become if (cloud == GCP) branches through your messaging code. MultiCloudJ removes them. Its pubsub module has two portable clients, TopicClient and SubscriptionClient. They delegate to an abstract driver that owns batching, prefetching, ack batching, and error mapping, and a provider module per cloud makes the API calls. Providers are discovered with Java’s ServiceLoader and selected by an ID string, so switching clouds is a configuration change.

