0
0
GCPcloud~5 mins

Pub/Sub vs Cloud Tasks in GCP - CLI Comparison

Choose your learning style9 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.