Cloud Run Vs Cloud Functions

Posted on Tue Apr 11 2023

Cloud Run and Cloud Function are two comparable cloud computing services offered by Google Cloud Platform. When I first started using GCP, the App Engine platform was being replaced by Cloud Run. So I initially used Cloud Run to build a lot of services. Over time, I also started to utilize Cloud Functions, primarily for responding to Pub/Sub messages.

I have a personal preference for Cloud Run after using both technologies. I'll try to explain what kinds of use cases these technologies are most suited for.

Cloud Functions

It's a very nice piece of technology. Recently, I sent notifications to a certain chat group using Cloud Functions. Messages were being generated by various services and pushed to Cloud Pub/Sub topics. A Cloud Function would be started via a Cloud Pub/Sub subscription, which would then transmit those messages to a chat room.

Use Cloud Functions, in my opinion, when :-

  • Simple and self-contained task. It can be so basic that there won't be any tests.
  • The task will be carried out irregularly and a variable number of times.
  • There won't be many updates to the task.

What was the issue?

As we are discussing Google Cloud Platform, I think I will explain various issues I faced in that regard. As there are similar offerings from other providers, I think this discussion may not apply to them.

In the previous section, I laid out some use cases for Cloud Functions. So you understand, for Cloud Functions, I am advocating for use cases that don't change very often. This is mainly because of three reasons.

  • There is zero flexibility in image production. You always end up with a large image that is roughly 300 MB or more in size as a price for simplicity.
  • Deployment times are incredibly lengthy as compared to Cloud Run. You might have to wait for 2 to 5 minutes only to deploy.
  • My deployments tends to be inexpensive, and Cloud Function will maintain a minimum of two versions at once. This adds to the cost of Cloud Storage.

Cloud Run

This innovation was provided as an App Engine upgrade. It can be described as "Managed Kubernetes". Web services benefit most from this. Everything else will be taken care of for you; all you have to do is design a container for your web service.

Most of my services are running on top of Cloud Run. I will now lay out some use cases for this technology.

  • Web service like Next.JS, Express.JS, GraphQL or TRPC servers
  • Any code that can be exposed as a web service through container

Now let's discuss what the major plus points are for Cloud Run.

  • You get full control over the image generation process. Some of my production backend services are just around 60 MB.
  • Although a pre-GA offering, the Cloud Run service can be linked to a domain (or subdomain) really easily.
  • After deployment, you can delete the Cloud Run image, as the latest revision keeps an internal copy of your container.
  • There is a nice free tier offered with Cloud Run (something similar is available with Cloud Functions).
  • Cloud Build based deployment takes less time than Cloud Functions.

So if your service can be wrapped as a container and it is handling serious production logic, I would recommend Cloud Run instead of Cloud Functions.