gRPC on Cloud Run

Grant Timmerman
Google Cloud - Community
3 min readMay 4, 2021
gRPC + Cloud Run

gRPC is a modern, open source, high performance Remote Procedure Call (RPC) framework that can run in any environment. It allows you to directly call a client application as if it was a local service. You can also efficiently stream data between services. Moreover, Google APIs support requests via gRPC, allowing you to build efficient, high-throughput systems.

In this blogpost, we’ll introduce the gRPC protocol and how you can use them in Cloud Run!

Understanding gRPC

gRPC clients and servers can run and talk to each other in a variety of environments — from servers inside Google, to your own desktop — and can be written in any of gRPC’s supported languages. For example, you can easily create a gRPC server in Java with clients in Go, Python, or Ruby.

Here’s a visual:

A C++ gRPC server talking to a Ruby and Android client.

By default, gRPC uses protocol buffers for data serialization. The first step when working with protocol buffers is to define the structure for the data we want to send in a .proto file, with messages including fields like such:

// A Person message.
// Numbers are used to identify fields in the binary encoded data.
message
Person {
string name = 1;
int32 id = 2;
bool likes_dogecoin = 3;
}

The protoc tool can generate language definitions based on this proto file.

Note: protoc can be installed with brew install protobuf and other ways.

Sample Application: Calculator

Let’s build a sample application– a calculator that accepts requests for calculation via gRPC. First, let’s look at the calculator.proto definition:

A sample proto file with messages and a service.

The proto file starts with some definitions and service RPC interface. The Calculate rpc take two floats and the operations of ADD or SUBTRACT, like a normal function call. The service expects a BinaryOperation message and returns a CalculationResult message.

We can then write a Node service like this that listens to gRPC requests (not HTTP requests):

A gRPC server with our Calculator service.

Notice that we use the createInsecure method here. Cloud Run's proxy provides us with a TLS-encrypted proxy that handles the messy business of setting up certs for us. The traffic from the proxy to the container with our gRPC server in it goes through an encrypted tunnel, so we don't need to worry about handling it ourselves. Cloud Run natively handles HTTP/2, so gRPC's transport is well-supported.

When you use server.addService, you’re adding the proto service to the server, which will invoke the calculate function:

The full code is at https://github.com/grpc-ecosystem/grpc-cloud-run-example/tree/master/node

Deploy to Cloud Run

Let’s deploy this service to Cloud Run. Without a Dockerfile, the source code will use Google Cloud Buildpacks, which will auto-detect our language (Node because of the package.json) and build our container.

One of the nice parts of using Cloud Run is that it supports gRPC out of the box, no extra flags are needed when deploying.

Note: If you’d like to stream gRPCs with HTTP/2, add the flag --use-http2🔗.

Deploy the Cloud Run app using the —-source flag:

gcloud beta run deploy --source .

To test the service, we can first get the Run endpoint, then use grpcurl, a tool like cURL, but for gRPC:

A handy script for testing in Cloud Run

Boom. And now you’ve got an auto-scaling calculator gRPC service!

We did it! If you want to learn more, check out the following links:

--

--