Bird
Raised Fist0
GCPcloud~10 mins

Topics and subscriptions in GCP - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Topics and subscriptions
Create Topic
↓
Create Subscription
↓
Publish Message
↓
Message Stored in Topic
↓
Subscription Receives Message
↓
Subscriber Processes Message
↓
Acknowledge Message
↓
Message Removed from Subscription
This flow shows how a message moves from creation in a topic to delivery and acknowledgment by a subscription.
Execution Sample
GCP
1. Create topic 'news-updates'
2. Create subscription 'user1-sub'
3. Publish message 'Hello World'
4. Subscription receives message
5. Subscriber acknowledges message
This example shows creating a topic and subscription, publishing a message, and the subscriber receiving and acknowledging it.
Process Table
StepActionState ChangeMessage StateSubscription State
1Create topic 'news-updates'Topic 'news-updates' createdNo messagesNo subscriptions
2Create subscription 'user1-sub'Subscription 'user1-sub' createdNo messagesSubscription 'user1-sub' active, no messages
3Publish message 'Hello World'Message added to topicMessage 'Hello World' in topicSubscription 'user1-sub' active, message 'Hello World' pending delivery
4Subscription receives messageMessage delivered to subscriptionMessage 'Hello World' in topicSubscription 'user1-sub' has message 'Hello World' pending ack
5Subscriber acknowledges messageMessage acknowledged and removed from subscriptionMessage 'Hello World' in topicSubscription 'user1-sub' no pending messages
💡 Message acknowledged, no pending messages in subscription, flow ends
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
TopicNoneCreated, no messagesCreated with 1 messageCreated with 1 messageCreated with 1 message
SubscriptionNoneCreated, no messagesCreated with 1 message pending deliveryHas 1 message pending ackNo messages pending
MessageNoneNone'Hello World' in topic'Hello World' delivered to subscriptionAcknowledged and removed
Key Moments - 3 Insights
Why does the message stay in the topic even after the subscription receives it?
Because Pub/Sub topics retain messages for a configurable retention duration (default 7 days), independent of acknowledgments by subscriptions. The subscription gets its own copy to process.
What happens if the subscriber does not acknowledge the message?
The message stays in the subscription as pending and can be redelivered until acknowledged, ensuring no message loss.
Can multiple subscriptions receive the same message from one topic?
Yes, each subscription gets its own copy of the message independently.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the subscription first receive the message?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Subscription State' column for when the message appears in the subscription
According to the variable tracker, what is the state of the message after step 5?
AMessage acknowledged and removed
BMessage still pending in subscription
CMessage deleted from topic
DMessage duplicated in subscription
💡 Hint
Look at the 'Message' row under 'After Step 5' in variable_tracker
If the subscriber never acknowledges the message, what would happen according to the flow?
AMessage is removed from topic immediately
BSubscription loses the message
CMessage remains pending in subscription and can be redelivered
DTopic deletes the subscription
💡 Hint
Refer to the key moment about message acknowledgment and subscription state
Concept Snapshot
Topics hold messages published by senders.
Subscriptions receive copies of messages from topics.
Subscribers pull messages from subscriptions.
Messages stay until acknowledged by subscribers.
Multiple subscriptions get independent copies.
Acknowledgment removes message from subscription.
Full Transcript
This visual execution shows how Google Cloud Pub/Sub topics and subscriptions work together. First, a topic is created to hold messages. Then, a subscription is created to receive messages from that topic. When a message is published, it is stored in the topic. The subscription gets its own copy of the message to deliver to subscribers. The subscriber processes the message and sends an acknowledgment. Only after acknowledgment is the message removed from the subscription. The topic retains the message for a retention period (default 7 days), regardless of acknowledgments. This ensures reliable delivery and no message loss. Multiple subscriptions can independently receive the same message from one topic.

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

  1. Step 1: Understand the role of a topic

    A topic is where messages are sent or published by a publisher.
  2. Step 2: Differentiate topic from subscription

    Subscriptions receive messages from topics, not the other way around.
  3. Final Answer:

    To send messages to subscribers -> Option D
  4. Quick Check:

    Topic = message sender [OK]
Hint: Topics send; subscriptions receive messages [OK]
Common Mistakes:
  • Confusing topic as receiver instead of sender
  • Thinking topics store messages permanently
  • Mixing roles of topic and subscription
2. Which of the following is the correct way to create a subscription to a topic using gcloud CLI?
easy
A. gcloud pubsub subscriptions create my-sub --topic=my-topic
B. gcloud pubsub topics create my-sub --subscription=my-topic
C. gcloud pubsub subscriptions create my-topic --topic=my-sub
D. gcloud pubsub topics create my-topic --subscription=my-sub

Solution

  1. Step 1: Identify correct command syntax

    The command to create a subscription uses subscriptions create with the subscription name and --topic flag for the topic.
  2. Step 2: Verify gcloud pubsub subscriptions create my-sub --topic=my-topic matches syntax

    gcloud pubsub subscriptions create my-sub --topic=my-topic correctly uses gcloud pubsub subscriptions create my-sub --topic=my-topic.
  3. Final Answer:

    gcloud pubsub subscriptions create my-sub --topic=my-topic -> Option A
  4. Quick Check:

    Subscriptions create with --topic flag [OK]
Hint: Subscriptions created with 'subscriptions create' and --topic [OK]
Common Mistakes:
  • Using topics create instead of subscriptions create
  • Swapping topic and subscription names
  • Using --subscription flag instead of --topic
3. Given the following code snippet, what will be printed if the subscription receives a message?
from google.cloud import pubsub_v1

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path('my-project', 'my-sub')

def callback(message):
    print(f'Received: {message.data.decode()}')
    message.ack()

streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print('Listening for messages...')
medium
A. Only Listening for messages...
B. Only Received:
C. Listening for messages... followed by Received:
D. Error because callback is not called

Solution

  1. Step 1: Understand code flow

    The code subscribes to a topic and sets a callback to print received messages and acknowledge them.
  2. Step 2: Identify output behavior

    It prints 'Listening for messages...' immediately, then prints 'Received: ...' when a message arrives.
  3. Final Answer:

    Listening for messages... followed by Received: <message content> -> Option C
  4. 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

  1. Step 1: Check subscription-topic link

    If subscription is not linked to the right topic, it won't receive messages.
  2. 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.
  3. Final Answer:

    The subscription is not attached to the correct topic -> Option A
  4. 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

  1. Step 1: Understand message fan-out

    One topic can send messages to many subscriptions, allowing multiple services to receive independently.
  2. 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.
  3. Final Answer:

    Create one topic and multiple subscriptions, one per service -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard