Create and Configure Pub/Sub Topics and Subscriptions in GCP
📖 Scenario: You are setting up a messaging system in Google Cloud Platform (GCP) using Pub/Sub. This system will allow different parts of your application to communicate by sending and receiving messages asynchronously.First, you will create a topic where messages can be published. Then, you will create a subscription to that topic so that messages can be received and processed.
🎯 Goal: Build a Pub/Sub topic named news-updates and a subscription named news-subscriber that listens to this topic.
📋 What You'll Learn
Create a Pub/Sub topic called news-updates.
Create a subscription called news-subscriber for the news-updates topic.
Set the subscription's acknowledgment deadline to 20 seconds.
Use the Google Cloud SDK gcloud commands in the correct order.
💡 Why This Matters
🌍 Real World
Messaging systems in cloud applications often use Pub/Sub to decouple components and handle asynchronous communication.
💼 Career
Cloud engineers and developers use Pub/Sub topics and subscriptions to build scalable, event-driven architectures.
Progress0 / 4 steps
1
Create the Pub/Sub topic
Use the gcloud pubsub topics create command to create a topic named news-updates.
GCP
Hint
Remember the command format: gcloud pubsub topics create [TOPIC_NAME].
2
Create the subscription variable
Create a variable called SUBSCRIPTION_NAME and set it to news-subscriber.
GCP
Hint
Use the syntax VARIABLE_NAME=value to create a variable in shell.
3
Create the subscription with acknowledgment deadline
Use the gcloud pubsub subscriptions create command to create a subscription named news-subscriber for the topic news-updates. Set the acknowledgment deadline to 20 seconds using the --ack-deadline flag.
GCP
Hint
Use --ack-deadline=20 to set the acknowledgment deadline to 20 seconds.
4
Verify the subscription configuration
Use the gcloud pubsub subscriptions describe command with the subscription name news-subscriber to verify the subscription exists and has the correct acknowledgment deadline.
GCP
Hint
This command shows the subscription details including the acknowledgment deadline.
Practice
(1/5)
1. What is the main role of a topic in Google Cloud Pub/Sub?
easy
A. To authenticate users
B. To receive messages from subscribers
C. To store messages permanently
D. To send messages to subscribers
Solution
Step 1: Understand the role of a topic
A topic is where messages are sent or published by a publisher.
Step 2: Differentiate topic from subscription
Subscriptions receive messages from topics, not the other way around.
C. Listening for messages... followed by Received:
D. Error because callback is not called
Solution
Step 1: Understand code flow
The code subscribes to a topic and sets a callback to print received messages and acknowledge them.
Step 2: Identify output behavior
It prints 'Listening for messages...' immediately, then prints 'Received: ...' when a message arrives.
Final Answer:
Listening for messages... followed by Received: <message content> -> Option C
Quick Check:
Subscribe prints listening then message received [OK]
Hint: Callback prints message after initial listening print [OK]
Common Mistakes:
Assuming callback runs before print
Thinking no output until message arrives
Ignoring message acknowledgment
4. You created a subscription but messages are not being received. Which of the following is the most likely cause?
medium
A. The subscription is not attached to the correct topic
B. The topic does not exist
C. The subscription name is too long
D. The messages are too large
Solution
Step 1: Check subscription-topic link
If subscription is not linked to the right topic, it won't receive messages.
Step 2: Evaluate other options
Topic existence is important but usually creation fails if topic missing; name length or message size usually cause errors, not silent no messages.
Final Answer:
The subscription is not attached to the correct topic -> Option A
Quick Check:
Subscription-topic mismatch blocks messages [OK]
Hint: Check subscription's topic link first if no messages [OK]
Common Mistakes:
Ignoring subscription-topic association
Assuming message size silently blocks delivery
Thinking subscription name length causes no messages
5. You want to ensure that messages published to a topic are delivered to multiple independent services. Which setup should you use?
hard
A. Create multiple topics, one per service, and one subscription per topic
B. Create one topic and multiple subscriptions, one per service
C. Create one subscription and multiple topics linked to it
D. Create one topic and one subscription shared by all services
Solution
Step 1: Understand message fan-out
One topic can send messages to many subscriptions, allowing multiple services to receive independently.
Step 2: Evaluate options
Create one topic and multiple subscriptions, one per service uses one topic with multiple subscriptions, which is the recommended pattern for multiple independent consumers.
Final Answer:
Create one topic and multiple subscriptions, one per service -> Option B
Quick Check:
One topic, many subscriptions for multiple services [OK]
Hint: Use multiple subscriptions on one topic for multiple receivers [OK]
Common Mistakes:
Creating multiple topics unnecessarily
Using one subscription shared by all services
Linking subscriptions to multiple topics
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.