Asynchronous Code Execution with Google Cloud Tasks

Grant Timmerman
Oct 23, 2019 · 4 min read
Google Cloud Tasks

No application is an island — every non-trivial application has dependencies and exists alongside other services, sharing resources and data. Over the years, messaging has evolved from static, synchronous point-to-point models to asynchronous mechanisms.

In this blogpost, we’ll learn what Google Cloud Tasks, an asynchronous point-to-point queuing system for code execution, where you could benefit from using Cloud Tasks, and how to get started.

What is Cloud Tasks?

Cloud Tasks is a queuing system service for Google Cloud Platform for managing the execution, dispatch, and delivery of a large number of distributed tasks.

With Cloud Tasks, you can add millions of “Tasks” in a queue. You may also create many queues. The queue processes Tasks in the queue, sending an HTTP request to the target URL or executing an App Engine application handler, depending on your setup.

An example diagram of this relationship is below.

Left: 8 Cloud Tasks
Right: A server listening to HTTP requests (our HTTP target)

Using Cloud Tasks can increase the reliability, manageability, and responsiveness of your applications. For example, Cloud Tasks works particularly well with decoupled services built with Cloud Functions and Cloud Run.

Increase the reliability of your service

Cloud Tasks includes these features:

Dispatch tasks that reach any target within GCP and on-prem systems.

Offload heavyweight, background and long running processes to a task queue, allowing an application to be more responsive to users.

Control the rate at which tasks are dispatched to your service to ensure your microservices doesn’t get overwhelmed.

With each task is persisted in storage and retried automatically, your infrastructure is resilient to intermittent failures.

Microservices often don’t talk to each other via direct request/response, but rather asynchronously, allowing services to scale independently. Cloud Tasks helps you better structure and scale your application via dedicated independent, configurable Task queues.

This allows you to do things like pause a Tasks queue, or configure independent retry policies.

Other features include scheduling a Task for up to 30 days, Queue rate limiting, configurable retries, Task deduplication, and Task handler auth.

Where could you use Cloud Tasks?

Cloud Tasks can be used to call Google App Engine, Cloud Run, Cloud Functions, or any HTTP server.

Here are some example use-cases:

  • Push an asynchronous image processing job that retry.
  • Read from a database at a controlled and consistent rate.
  • Schedule an email to be sent 1 month after sign-up of a trial period.

So why would you use Cloud Tasks versus Pub/Sub?

The core difference between Cloud Pub/Sub and Cloud Tasks is the notion of implicit vs explicit invocation.

Cloud Pub/Sub supports implicit invocation: a publisher implicitly causes the subscribers to execute by publishing an event.

By contrast, Cloud Tasks is aimed at explicit invocation where the publisher retains full control of execution.

A detailed comparison and handy side-by-side comparison can be found here.

Tools of Cloud Tasks

How can you use Cloud Tasks? To control Cloud Tasks, you have a variety of tools, for manual and programmatic use:

At, you are presented an interface for viewing all queues and controls for resuming/pausing the queue, deleting the whole queue or individual Tasks, as well as purging/deleting all Tasks.

An example of ~10,000 Tasks running in the queue

One of the easiest ways to use Cloud Tasks is by using the command line.

With gcloud, you can easily create Tasks with a command like such:

gcloud tasks queues create "example-queue" 

Ignore the warning about queue.yaml and queue.xml.

This creates a new queue which can contain thousands of Tasks.

Programmatically, we can use Tasks with Google API clients, for example, the Node client:

A very simple API request to the Cloud Tasks API

Thanks for reading! That’s a brief introduction to Cloud Tasks.

Google Cloud - Community

A collection of technical articles published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Grant Timmerman

Written by

Google • Explorer

Google Cloud - Community

A collection of technical articles published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade