Google Cloud Run and Cloud Tasks: Serverless Computing at Scale #1

Google Cloud Run and Cloud Tasks: Serverless Computing at Scale  #1
Photo by Ag PIC / Unsplash

Google Cloud Run and Cloud Tasks are two cloud tools that can help you build scalable and resilient applications.

Cloud Run is a fully managed serverless computing platform that allows you to run stateless containers in the cloud. It automatically scales your containers based on demand and you only pay for the computing time you use. It is ideal for running microservices, API servers, and other workloads that require high availability and scalability. To use Cloud Run, you need to package your code in a container image and deploy it to the platform. Cloud Run will handle the rest, including provisioning computing resources, managing traffic, and monitoring container health.

Google Cloud Tasks is a fully managed task queue that allows you to schedule, execute, and manage background tasks in the cloud. It can be used to handle workloads that are too large or time-consuming to be handled in a single request, such as sending emails, processing data, or making API calls. To use Cloud Tasks, you create a task and specify the work that needs to be done, either by providing a URL or uploading a message to a Cloud Pub/Sub topic. Cloud Tasks will then handle scheduling the task for execution and retrying if necessary. You can also set up Cloud Tasks to execute tasks regularly or at a specific time in the future.

By combining Cloud Run and Cloud Tasks, you can build highly scalable and resilient applications that can handle any workload. For example, you could use Cloud Run to run an API server that handles incoming requests and use Cloud Tasks to queue tasks such as sending emails or processing data. This allows you to offload the heavy lifting to the cloud and focus on building your core business logic.

However, it is important to note that there is a pitfall to consider when using Cloud Pub/Sub vs Cloud Tasks. People who are not familiar with the differences between the two might choose the wrong one for their use case. While Cloud Pub/Sub is useful for passing messages around, it does not allow for the granular control over message delivery and retries, while Cloud Tasks does. If you need to process queues in the background, I recommend using Cloud Tasks for most cases.

So both services are serverless - meaning you pay for what you use. I run NestJS on Cloud Run when I want to deploy. This is an example of Dockerfile I found on GitHub that would fit most of the cases and I think is a good starting point for a hosting NestJS application on GCP.