Bird
Raised Fist0
GCPcloud~5 mins

Pub/Sub vs Cloud Tasks in GCP - CLI Comparison

Choose your learning style10 modes available
Introduction
Sometimes you need to send messages between parts of your app. Pub/Sub and Cloud Tasks help with this but in different ways. Pub/Sub is for sending many messages quickly to many receivers. Cloud Tasks is for managing jobs that must run once and in order.
When you want to broadcast notifications to many services at once, like sending alerts to multiple apps.
When you need to process background jobs reliably, like sending emails one by one without losing any.
When your app parts need to stay loosely connected and handle messages independently.
When you want to control the timing and order of tasks, such as retrying failed jobs later.
When you want to scale message processing automatically without worrying about managing servers.
Commands
This command creates a Pub/Sub topic named 'example-topic' where messages can be published.
Terminal
gcloud pubsub topics create example-topic
Expected OutputExpected
Created topic [projects/your-project/topics/example-topic].
This creates a subscription named 'example-subscription' to receive messages from 'example-topic'.
Terminal
gcloud pubsub subscriptions create example-subscription --topic=example-topic
Expected OutputExpected
Created subscription [projects/your-project/subscriptions/example-subscription].
→
--topic - Specifies the topic to subscribe to.
This command creates a Cloud Tasks queue named 'example-queue' to hold tasks for processing.
Terminal
gcloud tasks queues create example-queue
Expected OutputExpected
Created queue [projects/your-project/locations/us-central1/queues/example-queue].
This creates an HTTP task in 'example-queue' that will send a POST request with a message to the specified URL.
Terminal
gcloud tasks create-http-task --queue=example-queue --url=https://example.com/task-handler --method=POST --body-content='{"message":"Hello"}'
Expected OutputExpected
Created task [projects/your-project/locations/us-central1/queues/example-queue/tasks/1234567890].
→
--queue - Specifies the queue to add the task to.
→
--url - The endpoint that will receive the task request.
→
--method - HTTP method used for the task request.
This command shows details about the 'example-queue' to check its status and configuration.
Terminal
gcloud tasks queues describe example-queue
Expected OutputExpected
name: projects/your-project/locations/us-central1/queues/example-queue state: RUNNING rateLimits: maxDispatchesPerSecond: 500 maxBurstSize: 100 retryConfig: maxAttempts: 5
Key Concept

If you remember nothing else, remember: Pub/Sub is for broadcasting many messages to many receivers quickly, while Cloud Tasks is for managing individual jobs that must run reliably and in order.

Common Mistakes
Using Pub/Sub when you need guaranteed single execution and order of tasks.
Pub/Sub can deliver messages multiple times and does not guarantee order, which can cause duplicate or out-of-order processing.
Use Cloud Tasks when you need tasks to run once, in order, and with retries.
Creating Cloud Tasks without specifying the correct HTTP endpoint or method.
Tasks will fail if the endpoint is wrong or does not accept the HTTP method, causing retries or lost tasks.
Ensure the URL and HTTP method match the receiving service's expectations.
Not creating a subscription for a Pub/Sub topic before publishing messages.
Messages published to a topic without subscriptions are lost because no one receives them.
Always create at least one subscription to receive messages from a topic.
Summary
Create a Pub/Sub topic and subscription to send and receive broadcast messages.
Create a Cloud Tasks queue and add HTTP tasks for reliable, ordered job processing.
Use Pub/Sub for many-to-many messaging and Cloud Tasks for one-to-one task execution.

Practice

(1/5)
1. Which Google Cloud service is best suited for sending messages to multiple subscribers quickly and asynchronously?
easy
A. Cloud Functions
B. Cloud Tasks
C. Pub/Sub
D. Cloud Run

Solution

  1. Step 1: Understand Pub/Sub's purpose

    Pub/Sub is designed to send messages to many subscribers quickly and asynchronously, enabling event-driven architectures.
  2. Step 2: Compare with Cloud Tasks

    Cloud Tasks manages individual work items with control over retries and timing, not broad message delivery.
  3. Final Answer:

    Pub/Sub -> Option C
  4. Quick Check:

    Event broadcasting = Pub/Sub [OK]
Hint: Pub/Sub = many subscribers fast; Cloud Tasks = controlled retries [OK]
Common Mistakes:
  • Confusing Cloud Tasks as a message broadcaster
  • Thinking Pub/Sub controls task retries
  • Mixing Cloud Functions with messaging services
2. Which of the following is the correct way to create a Cloud Task with a scheduled delay in Google Cloud?
easy
A. Use Pub/Sub with a delay attribute
B. Use Cloud Tasks client and set scheduleTime to future timestamp
C. Send a message to Pub/Sub and rely on subscriber delay
D. Create a Cloud Function with a timer trigger instead

Solution

  1. Step 1: Identify scheduling method in Cloud Tasks

    Cloud Tasks allows setting a scheduleTime property to delay task execution until a future timestamp.
  2. Step 2: Eliminate incorrect options

    Pub/Sub does not support scheduling messages; Cloud Functions timer triggers are separate services.
  3. Final Answer:

    Use Cloud Tasks client and set scheduleTime to future timestamp -> Option B
  4. Quick Check:

    Scheduled tasks = Cloud Tasks scheduleTime [OK]
Hint: Schedule tasks with Cloud Tasks scheduleTime, not Pub/Sub [OK]
Common Mistakes:
  • Trying to delay Pub/Sub messages directly
  • Confusing Cloud Functions timers with Cloud Tasks scheduling
  • Assuming subscribers can delay message processing
3. Given the following scenario: A Cloud Task is created with maxAttempts set to 3 and a retry delay of 10 seconds. If the task fails twice, what happens next?
medium
A. The task is retried after 10 seconds delay for the third attempt
B. The task is retried immediately without delay
C. The task is discarded after the second failure
D. Pub/Sub resends the message automatically

Solution

  1. Step 1: Understand Cloud Tasks retry policy

    Cloud Tasks retries failed tasks up to maxAttempts, applying the configured retry delay between attempts.
  2. Step 2: Apply to given scenario

    After two failures, the third attempt will be retried after the 10 seconds delay before final failure or success.
  3. Final Answer:

    The task is retried after 10 seconds delay for the third attempt -> Option A
  4. Quick Check:

    Retries respect delay and maxAttempts in Cloud Tasks [OK]
Hint: Cloud Tasks retries wait delay before next try, up to maxAttempts [OK]
Common Mistakes:
  • Assuming immediate retry without delay
  • Thinking task is discarded too early
  • Confusing Pub/Sub automatic retries with Cloud Tasks
4. You have a Cloud Task that never executes and keeps retrying indefinitely. What is the most likely cause?
medium
A. The Cloud Task queue is paused
B. Pub/Sub subscription is missing
C. The task's maxAttempts is set to 0 or unlimited
D. The task's scheduleTime is in the past

Solution

  1. Step 1: Analyze retry behavior

    If a task retries indefinitely, it means it keeps failing but is still being retried.
  2. Step 2: Check queue state

    If the Cloud Task queue is paused, tasks will not execute but may keep retrying or remain pending.
  3. Step 3: Eliminate other options

    Pub/Sub subscription missing is unrelated to Cloud Tasks; maxAttempts 0 means no retries; scheduleTime in past triggers immediate execution.
  4. Final Answer:

    The Cloud Task queue is paused -> Option A
  5. Quick Check:

    Paused queue blocks task execution causing retries [OK]
Hint: Paused queue stops tasks but retries may appear stuck [OK]
Common Mistakes:
  • Confusing Pub/Sub with Cloud Tasks
  • Assuming maxAttempts 0 means infinite retries
  • Thinking scheduleTime in past delays execution
5. You need to design a system that processes user signup events by notifying multiple services quickly, but also schedules follow-up tasks with guaranteed retries and delays. Which combination of services should you use?
hard
A. Use only Cloud Tasks for both notifications and follow-ups
B. Use only Pub/Sub with delayed message delivery
C. Use Cloud Functions triggered by HTTP for all processing
D. Use Pub/Sub for notifications and Cloud Tasks for follow-up tasks

Solution

  1. Step 1: Identify service for quick multi-subscriber notifications

    Pub/Sub is ideal for sending events quickly to multiple subscribers asynchronously.
  2. Step 2: Identify service for scheduled, reliable task execution

    Cloud Tasks provides control over retries, delays, and guaranteed execution for follow-up work.
  3. Step 3: Combine services for best fit

    Use Pub/Sub to notify many services fast, and Cloud Tasks to manage follow-up tasks with retries and delays.
  4. Final Answer:

    Use Pub/Sub for notifications and Cloud Tasks for follow-up tasks -> Option D
  5. Quick Check:

    Pub/Sub for events + Cloud Tasks for reliable tasks [OK]
Hint: Combine Pub/Sub for events and Cloud Tasks for retries/delays [OK]
Common Mistakes:
  • Trying to use Pub/Sub for delayed retries
  • Using Cloud Tasks for broad event broadcasting
  • Ignoring retry guarantees needed for follow-ups
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