0
0
GCPcloud~30 mins

Pub/Sub vs Cloud Tasks in GCP - Hands-On Comparison

Choose your learning style9 modes available
Understanding Pub/Sub vs Cloud Tasks in GCP
📖 Scenario: You are building a simple order processing system on Google Cloud Platform (GCP). You want to learn how to use Pub/Sub and Cloud Tasks to handle messages and tasks asynchronously.Pub/Sub is like a message bus where many services can listen and react to messages. Cloud Tasks is like a to-do list where tasks are done one by one or retried if they fail.
🎯 Goal: Build a small Python script that creates a Pub/Sub topic and a Cloud Tasks queue, then publishes a message to Pub/Sub and enqueues a task in Cloud Tasks. This will help you see the difference in how these services work.
📋 What You'll Learn
Create a Pub/Sub topic named orders-topic
Create a Cloud Tasks queue named orders-queue
Publish a message with data 'New order received' to orders-topic
Enqueue a task with payload {"order_id": 123} to orders-queue
💡 Why This Matters
🌍 Real World
Many cloud applications use Pub/Sub to broadcast events to multiple services and Cloud Tasks to manage background work reliably with retries.
💼 Career
Understanding these services is essential for cloud engineers and developers building scalable, reliable systems on Google Cloud Platform.
Progress0 / 4 steps
1
Create a Pub/Sub topic named orders-topic
Write Python code to create a Pub/Sub client and create a topic called orders-topic using the pubsub_v1.PublisherClient() and create_topic() method.
GCP
Need a hint?

Use publisher.topic_path(project_id, topic_name) to get the full topic path.

2
Create a Cloud Tasks queue named orders-queue
Add Python code to create a Cloud Tasks client and create a queue called orders-queue using cloudtasks_v2.CloudTasksClient() and create_queue() method.
GCP
Need a hint?

Use tasks_client.location_path(project_id, location) and tasks_client.queue_path(project_id, location, queue_name) to build resource names.

3
Publish a message with data 'New order received' to orders-topic
Write Python code to publish the message 'New order received' to the Pub/Sub topic orders-topic using publisher.publish() with the topic_path.
GCP
Need a hint?

Remember to encode the string message to bytes before publishing.

4
Enqueue a task with payload {"order_id": 123} to orders-queue
Add Python code to create a Cloud Task with JSON payload {"order_id": 123} and enqueue it to the orders-queue using tasks_client.create_task(). Use tasks_client.queue_path() for the queue name and set the task http_request with body as JSON bytes.
GCP
Need a hint?

Set http_request.body to the JSON payload bytes and specify http_method and url.