In this lab, you'll learn how to use Google Cloud Pub/Sub to send and receive asynchronous messages between services. You'll create a topic for publishing messages, set up a subscription to receive those messages, publish test messages through the Console, and pull messages from your subscription. Pub/Sub is the foundation of event-driven architectures on GCP - it's used to decouple microservices, stream data to analytics systems, and trigger serverless functions.
Requirements to Pass This Lab
Topic with name starting with lab-topic- exists
Subscription named lab-subscription exists and is attached to the topic
Before You Begin
Step 0: Authentication
How this works: You need a Google account that uses the same email address as your GCP Study Hub account. When you sign into that Google account, the system will be able to grant you temporary access to a GCP project when you start the lab.
Once your lab starts, use the "Open GCP Console" button from the lab portal to open the Google Cloud Console with your temporary lab project already selected.
Step 2: Navigate to Pub/Sub
In the Google Cloud Console, you can search for "Pub/Sub" in the search bar at the top
Or click the hamburger menu (three horizontal lines) and select Pub/Sub from the left navigation menu
Click on Topics
If prompted, click Got it or Dismiss on any welcome messages
Step 3: Create a Topic
A topic is a named channel where publishers send messages. Think of it like a radio broadcast station - publishers send messages to the station, and anyone tuned in (subscribed) receives them.
Click CREATE TOPIC at the top of the page
Enter a Topic ID:lab-topic-notifications
IMPORTANT: You'll see a checkbox for "Add a default subscription" that is checked by default. Make sure to UNCHECK it. We'll create the subscription manually in the next step to better understand how subscriptions work.
Schema: None (schemas validate message structure, but we don't need that for this lab)
Click CREATE
You should see your new topic appear in the topics list. Topics can have multiple subscriptions, allowing different consumers to process the same messages independently.
Step 4: Create a Subscription
A subscription represents a stream of messages from a topic to a subscriber. Multiple subscriptions can exist on the same topic, and each subscription receives its own copy of every message.
After creating your topic, make sure you're viewing the topic details page for lab-topic-notifications (if you navigated away, click on the topic to view its details)
Scroll down and click CREATE SUBSCRIPTION
Configure the subscription (leave most settings at their defaults for this introductory lab):
Subscription ID:lab-subscription
Delivery type: Pull (default). This means your application explicitly requests messages, as opposed to Push where Pub/Sub sends HTTP requests to your endpoint.
Message retention duration: 7 days (default). Unacknowledged messages are kept for this long.
Acknowledgment deadline: 10 seconds (default is fine). This is how long Pub/Sub waits for acknowledgment before redelivering the message.
Transforms: Leave empty (default). Transforms let you manipulate and filter message data before delivery, but we don't need that for this lab.
Subscription filter: Leave empty (default). Filters let you receive only messages matching specific attribute criteria (e.g., attributes:k OR attributes.k1 = "v"). Useful for selective message processing.
Exactly once delivery: Leave unchecked (default). When enabled, guarantees messages won't be resent before acknowledgment deadline expires, eliminating duplicates. This increases latency and changes the acknowledgment deadline to 60 seconds minimum.
Message ordering: Leave unchecked (default). When enabled, messages with the same ordering key are delivered in the order they were published. This can increase latency and decrease availability.
Dead lettering: Leave unchecked (default). Dead lettering republishes undeliverable messages (after max delivery attempts) to a separate dead letter topic for later analysis.
Retry policy: Retry immediately (default). You can also choose "Retry after exponential backoff delay" which waits progressively longer between retries, useful for rate-limited systems.
Expiration period: 31 days (default). Inactive subscriptions are automatically deleted after this period to prevent accumulating unused resources.
Click CREATE
Your subscription is now ready to receive messages. Pub/Sub guarantees at-least-once delivery, meaning subscribers might occasionally see duplicate messages in rare failure scenarios.
Step 5: Publish Test Messages
Now you'll publish some test messages to your topic. In production systems, applications would publish messages programmatically, but the Console provides a convenient way to test.
Navigate back to Topics (click "Topics" in the left sidebar)
Click on your topic lab-topic-notifications
Click the MESSAGES tab
Click PUBLISH MESSAGE
In the message body field, enter: Hello from GCP Pub/Sub!
Optionally, you can add message attributes by clicking Add attribute:
Key:source
Value:manual-test
Click PUBLISH
Publish 2-3 more messages with different content (e.g., "Order #12345 processed", "User login event", etc.)
Each message is now queued in your subscription, waiting to be pulled and acknowledged.
Step 6: Pull and Acknowledge Messages
Now you'll pull messages from your subscription and learn how acknowledgment works. In production, your application code would do this automatically, but the Console lets us observe the behavior.
Part A: Pull without acknowledgment
Navigate to Subscriptions in the left sidebar
Click on lab-subscription
Click the MESSAGES tab
Make sure "Enable ack messages" is NOT checked (this is the default)
Click PULL
You should see the messages you published appear in the list
After about 10 seconds, you'll see "Deadline exceeded" appear next to the messages
What's happening? Without acknowledgment enabled, you're just viewing the messages. Since you didn't acknowledge them within the 10-second deadline, Pub/Sub will automatically make them available for redelivery. This ensures messages aren't lost if a subscriber crashes while processing them.
Part B: Pull with acknowledgment enabled
Now check the box next to "Enable ack messages" (this enables the ability to acknowledge)
Click PULL again
You should see the same messages reappear (they were redelivered because you didn't acknowledge them earlier)
Select one or more messages by checking the boxes next to them
Click ACK (acknowledge) to confirm you've successfully processed them
Notice the acknowledged messages disappear from the subscription - they've been permanently removed
What just happened? When you acknowledged the messages, you told Pub/Sub "I've successfully processed these messages." Pub/Sub then permanently removed them from the subscription queue. This is the core of Pub/Sub's at-least-once delivery guarantee: messages are redelivered until acknowledged, ensuring reliable message processing even in the face of subscriber failures.
Step 7: Understanding Message Redelivery
Let's verify you understand how acknowledgment works by observing message behavior:
Publish one more test message to your topic (go back to Topics → click your topic → MESSAGES tab → PUBLISH MESSAGE)
Return to your subscription (Subscriptions → lab-subscription → MESSAGES tab)
Pull the message with "Enable ack messages" checked
See the message appear, but don't acknowledge it yet
Wait about 15 seconds (longer than the 10-second acknowledgment deadline)
Click PULL again - the same message reappears (automatic redelivery!)
Now acknowledge the message - it disappears permanently
Key takeaway: Pub/Sub's reliability comes from this behavior. If your application crashes mid-processing, unacknowledged messages are automatically redelivered to another instance of your subscriber. This prevents message loss but means your code must be idempotent (safe to process the same message multiple times).
Validate Your Work
Get Your Completion Token
Click the button below to open the lab checker in a new window. Sign in with Google to verify your work and get your completion token.
Paste your token below and click Validate. If valid, a button will appear to mark the lesson complete.
Note: The "Mark Lesson Complete" button will not do anything if you are not in an actual lesson - but congratulations, you successfully completed the lab!
What You Learned
How to navigate to Pub/Sub in the Google Cloud Console
What topics and subscriptions are and how they work together
How to create a Pub/Sub topic for message publishing
How to create subscriptions to receive messages from a topic
How to publish messages to a topic through the Console
How to pull messages from a subscription and acknowledge them
Why message acknowledgment matters for reliable message delivery
The difference between Pull and Push delivery types
Real-world use cases: decoupling microservices, event-driven architectures, and asynchronous workflows
Troubleshooting
Validation Fails
If your validation fails, check that:
Your topic name starts with lab-topic- (e.g., lab-topic-notifications)
Your subscription is named exactly lab-subscription (case-sensitive)
The subscription is attached to your topic (verify in the subscription details page under "Topic name")
Both resources are in the correct project (check the project ID in the console header)
You're using the same email address for validation that you used to start the lab
No Messages Appear When Pulling
If you don't see messages when pulling:
Messages may have already been acknowledged - publish new messages and try again
Verify you're pulling from the correct subscription (lab-subscription)
Check that you published messages to the correct topic
Verify the subscription is attached to the topic that received the messages
Permission Denied Errors
If you get permission errors:
Ensure you started the lab first and are signed into the correct Google account
Check that you're working in the correct GCP project (shown in the top navigation bar)
The lab session may have expired - start a new lab session