Integrating Google Cloud Pub/Sub with Spring Boot
Fast recall
Quick revision
Core answer
spring-cloud-gcp-starter-pubsub dependency, then use PubSubTemplate to publish messages and @ServiceActivator or PubSubInboundChannelAdapter to consume them. Spring Cloud GCP handles credential management and message serialization, letting you work with Pub/Sub using familiar Spring Integration or Spring Messaging abstractions.Ability to integrate async messaging into a Spring application and understanding of Pub/Sub's push/pull model and acknowledgement semantics.
Forgetting to acknowledge messages (causing infinite redelivery), or conflating Pub/Sub topics with subscriptions — topics receive messages, subscriptions deliver them.
Discuss dead-letter topics for poison messages, ordering keys for FIFO delivery within a key, and exactly-once delivery mode introduced in Pub/Sub.
Interview-ready guide
Interview answer
Cloud Pub/Sub is GCP's managed messaging service. Spring Cloud GCP makes the publishing side trivial and provides annotation-based subscription handling, but understanding the delivery semantics — at-least-once, push vs pull — is what makes the integration reliable.
Publishing with PubSubTemplate
- Inject
PubSubTemplateand callpubSubTemplate.publish(topicName, message)— Spring Cloud GCP serializes the object to JSON by default and publishes asynchronously; the return value is aListenableFuturefor the publish confirmation - For high-throughput publishing, configure the publisher's batch settings:
spring.cloud.gcp.pubsub.publisher.batching.enabled=truebatches multiple messages into one API call, which reduces per-message overhead significantly
Consuming with @PubSubListener
@PubSubListener(subscriptions = "my-subscription")on a method marks it as a subscription handler — Spring Cloud GCP creates the subscription client, polls for messages, deserializes them, and calls the method; the message is automatically acknowledged if the method returns normally- If the method throws an exception, the message is not acknowledged and becomes available for redelivery after the ack deadline — at-least-once delivery; implement idempotent handlers to handle duplicate delivery safely
Push subscriptions
- A push subscription has Pub/Sub call a URL when messages arrive — which means Cloud Run functions or App Engine services can receive Pub/Sub messages via HTTP POST without running a continuous polling loop
- The HTTP handler receives the message as a Base64-encoded payload in a JSON envelope;
@RestController+ Spring's@PubSubSubscriberutility handles the deserialization
Message attributes and filtering
- Pub/Sub message attributes are key-value string metadata —
pubSubTemplate.publish(topic, message, Map.of("type", "OrderPlaced"))adds an attribute - Subscription filters (
subscription.message_filter) allow a subscription to receive only messages matchingattributes.type = "OrderPlaced", reducing traffic to consumers that only need specific event types
Integrating Google Cloud Pub/Sub with Spring Boot
Direct Answer
Add the spring-cloud-gcp-starter-pubsub dependency, then use PubSubTemplate to publish messages and @ServiceActivator or PubSubInboundChannelAdapter to consume them. Spring Cloud GCP handles credential management and message serialization, letting you work with Pub/Sub using familiar Spring Integration or Spring Messaging abstractions.
Common Mistakes & Pitfalls
Forgetting to acknowledge messages (causing infinite redelivery), or conflating Pub/Sub topics with subscriptions — topics receive messages, subscriptions deliver them.
Check your understanding
Answer these without looking back at the article.
- What is the difference between a Pub/Sub push subscription and a pull subscription?
- How do you implement idempotent message consumers with Pub/Sub?
- How would you test a Pub/Sub consumer in a Spring Boot integration test?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.