Bird
Raised Fist0
GCPcloud~20 mins

Publishing messages in GCP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pub/Sub Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Message Ordering in Pub/Sub

You have a Google Cloud Pub/Sub topic with message ordering enabled. What happens if you publish messages with the same ordering key but the subscriber acknowledges messages out of order?

AMessages with the same ordering key are delivered in the order they were published, regardless of acknowledgment order.
BMessages are delivered in any order, and acknowledgment order does not affect delivery.
CMessages with the same ordering key are redelivered until acknowledged in the correct order, blocking later messages.
DMessages with the same ordering key are dropped if acknowledged out of order.
Attempts:
2 left
💡 Hint

Think about how Pub/Sub ensures message order with ordering keys and what happens if acknowledgments are delayed or out of order.

❓ Architecture
intermediate
2:00remaining
Designing a Scalable Pub/Sub Publisher

You want to design a system that publishes millions of messages per second to a Google Cloud Pub/Sub topic. Which approach best ensures scalability and reliability?

AUse multiple publisher client instances with asynchronous publishing and batching enabled.
BUse a single publisher client instance with synchronous publishing calls to ensure message order.
CPublish messages directly from client devices without batching or retries.
DUse a single publisher client instance with manual retries and no batching.
Attempts:
2 left
💡 Hint

Consider how batching and asynchronous publishing affect throughput and reliability.

❓ security
advanced
2:00remaining
Securing Pub/Sub Message Publishing

You want to restrict who can publish messages to a Pub/Sub topic in your project. Which IAM role should you assign to the publisher service account to allow publishing but prevent subscription management?

Aroles/pubsub.editor
Broles/pubsub.admin
Croles/pubsub.subscriber
Droles/pubsub.publisher
Attempts:
2 left
💡 Hint

Think about the principle of least privilege and which role allows only publishing.

✅ Best Practice
advanced
2:00remaining
Handling Message Duplication in Pub/Sub

Google Cloud Pub/Sub may deliver messages more than once. What is the best practice to handle this duplication in your subscriber application?

AImplement idempotent message processing using unique message IDs.
BIgnore duplicates because Pub/Sub guarantees exactly-once delivery.
CAcknowledge messages immediately without processing to avoid duplicates.
DUse message filtering to prevent duplicates from being delivered.
Attempts:
2 left
💡 Hint

Consider how to safely process messages that might arrive multiple times.

🧠 Conceptual
expert
2:00remaining
Pub/Sub Message Ordering with Multiple Ordering Keys

You publish messages with multiple different ordering keys to a single Pub/Sub topic. How does Pub/Sub guarantee message ordering in this scenario?

APub/Sub does not guarantee any ordering when multiple ordering keys are used.
BMessages are ordered only within each ordering key; messages with different keys can be delivered out of order relative to each other.
CMessages with different ordering keys are delivered in the order they were published across all keys.
DMessages with different ordering keys are merged and delivered in alphabetical order of the keys.
Attempts:
2 left
💡 Hint

Think about how ordering keys isolate message order guarantees.

Practice

(1/5)
1. What is the main purpose of publishing messages in Google Cloud Pub/Sub?
easy
A. To send data to a topic so other services can receive it
B. To store data permanently in a database
C. To create virtual machines automatically
D. To monitor network traffic in real-time

Solution

  1. Step 1: Understand the role of publishing in Pub/Sub

    Publishing messages means sending data to a topic so subscribers can get it.
  2. Step 2: Compare with other options

    Storing data, creating VMs, or monitoring traffic are unrelated to publishing messages.
  3. Final Answer:

    To send data to a topic so other services can receive it -> Option A
  4. Quick Check:

    Publishing = sending data to topic [OK]
Hint: Publishing means sending messages to topics for others [OK]
Common Mistakes:
  • Confusing publishing with storing data
  • Thinking publishing creates resources like VMs
  • Mixing publishing with monitoring tasks
2. Which of the following is the correct way to publish a message using the Google Cloud Pub/Sub Python client?
easy
A. publisher.send_message(topic_path, data="Hello World")
B. publisher.publish(topic_path, data=b"Hello World")
C. publisher.publish(topic_path, message="Hello World")
D. publisher.send(topic_path, "Hello World")

Solution

  1. Step 1: Recall the correct method and parameters

    The correct method is publish(), and the message data must be bytes, so data=b"Hello World".
  2. Step 2: Check other options for errors

    send() and send_message() are not valid methods; also, data must be bytes, not string.
  3. Final Answer:

    publisher.publish(topic_path, data=b"Hello World") -> Option B
  4. Quick Check:

    Use publish() with bytes data [OK]
Hint: Use publish() method with bytes data to send messages [OK]
Common Mistakes:
  • Passing string instead of bytes as data
  • Using incorrect method names like send()
  • Using wrong parameter names like message=
3. Given the following Python code snippet, what will be printed?
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path('my-project', 'my-topic')
future = publisher.publish(topic_path, b'Message 1')
print(future.result())
medium
A. The content of the message 'Message 1'
B. The topic path string
C. An error because publish() returns None
D. The message ID of the published message

Solution

  1. Step 1: Understand what publish() returns

    publish() returns a future that resolves to the message ID after publishing.
  2. Step 2: Understand future.result() behavior

    Calling future.result() waits for publishing to complete and returns the message ID string.
  3. Final Answer:

    The message ID of the published message -> Option D
  4. Quick Check:

    future.result() = message ID [OK]
Hint: future.result() returns message ID after publish completes [OK]
Common Mistakes:
  • Expecting future.result() to return message content
  • Thinking publish() returns None
  • Confusing topic path with message ID
4. You wrote this code to publish a message but get a TypeError: 'str' object cannot be interpreted as bytes. What is the mistake?
publisher.publish(topic_path, 'Hello World')
medium
A. The message data must be bytes, not a string
B. The topic_path variable is not defined
C. publish() method does not exist
D. You must call future.result() after publish()

Solution

  1. Step 1: Identify the error cause

    The error says a string was given where bytes are expected.
  2. Step 2: Fix the data type for message

    Message data must be bytes, so prefix string with b like b'Hello World'.
  3. Final Answer:

    The message data must be bytes, not a string -> Option A
  4. Quick Check:

    Message data = bytes, not string [OK]
Hint: Always prefix message with b to make bytes [OK]
Common Mistakes:
  • Passing string instead of bytes as message data
  • Ignoring error message details
  • Assuming publish() method is missing
5. You want to publish multiple messages to a topic and ensure all are delivered before your program exits. Which approach is best?
hard
A. Publish messages using a loop but do not wait for any future results
B. Publish all messages and ignore the futures, then exit immediately
C. Publish each message and call future.result() immediately to wait for delivery
D. Publish messages and call future.cancel() to stop waiting

Solution

  1. Step 1: Understand message delivery confirmation

    Calling future.result() waits for the message to be confirmed as delivered.
  2. Step 2: Ensure program waits for all messages

    Waiting on each future ensures no message is lost before program exits.
  3. Step 3: Evaluate other options

    Ignoring futures or cancelling them risks losing messages; not waiting is unsafe.
  4. Final Answer:

    Publish each message and call future.result() immediately to wait for delivery -> Option C
  5. Quick Check:

    Wait on future.result() to confirm delivery [OK]
Hint: Wait on future.result() for each message to confirm delivery [OK]
Common Mistakes:
  • Ignoring futures and exiting too soon
  • Cancelling futures instead of waiting
  • Assuming publish() is synchronous
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