Rethink platform engineering and developer impact in the age of AI. Tune in to our webinar on Thursday, May 22.

gRPC Explained: High-Performance APIs, Streaming, and Deployment using an API Gateway

Kay James
Kay James

What is gRPC?

gRPC is an RPC protocol built on HTTP/2 (gRPC Remote Procedure Calls) and is an open-source, high-performance framework developed by Google that allows communication between applications or services. It's particularly popular for microservices and distributed systems because of its efficiency, scalability, and support for multiple languages. We’re seeing more and more companies move their APIs from HTTP/JSON to gRPC. gRPC offers several benefits over HTTP/JSON:

  • Performance. gRPC uses HTTP/2, with support for streaming, multiplexing, and more (see the difference in action). In addition, gRPC has native support for protobuf, which is much faster at serialization/deserialization than JSON.
  • Streaming. Despite its name, gRPC also supports streaming, which opens up a much wider range of use cases.
  • Code generation of endpoints. gRPC uses code generation from API definitions (.proto files) to stub out your endpoints.

gRPC does have a few downsides, the biggest of which is probably that it’s much newer than HTTP/REST, and as such, the maturity of tools and libraries around gRPC isn’t anywhere close to HTTP.

One of these gaps is in API Gateways. Many API Gateways don’t support gRPC. Fortunately, Edge Stack API Gateway does support gRPC, thanks to the fact that it uses Envoy Proxy as its core proxying engine.

API Gateways and gRPC

An API Gateway implements cross-cutting functionality such as authentication, logging, rate limiting, and load balancing. By using an API Gateway with your gRPC APIs, you are able to deploy this functionality outside of your core gRPC service(s). Moreover, Ambassador is able to provide this functionality for both your HTTP and gRPC services.

Deploy a gRPC with Edge Stack API Gateway

Deploying a gRPC service with Edge Stack API Gateway is straightforward. After installing Edge Stack, create an Ambassador mapping for your gRPC service. An example mapping would look like the following:

apiVersion: getambassador.io/v3alpha1

kind: Mapping

metadata:

 name: grpc_mapping

spec:

 grpc: true

 prefix: /helloworld.Greeter/

 rewrite: /helloworld.Greeter/

 service: grpc-greet

Note that gRPC services are not routed like HTTP services. Instead, gRPC requests include the package and service name. This information is used to route the request to the appropriate service. We set the prefix and rewrite fields accordingly based on the .proto file. Also, note that we set grpc: true to tell Ambassador that the service speaks gRPC.

Deploying a gRPC service

We can deploy a simple Hello, World gRPC service to illustrate all this functionality. Copy-and-paste the full YAML below into a file called helloworld.yaml:

---

apiVersion: v1

kind: Service

metadata:

labels:

  service: grpc-greet

name: grpc-greet

spec:

type: ClusterIP

ports:

- port: 80

  name: grpc-greet

  targetPort: grpc-api

selector:

  service: grpc-greet

---

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

name: grpc-greet

spec:

replicas: 1

template:

  metadata:

    labels:

      service: grpc-greet

  spec:

    containers:

    - name: grpc-greet

      image: enm10k/grpc-hello-world

      ports:

      - name: grpc-api

        containerPort: 9999

      env:

        - name: PORT

          value: "9999"

      command:

        - greeter_server

    restartPolicy: Always

Then, run

kubectl apply -f helloworld.yaml

.

The Hello World client

To test that Edge Stack and the service are working properly, we’ll need to run a gRPC client. First, get the external IP address of Ambassador. You can do this with kubectl get svc ambassador.

We’ll use a Docker image that already contains the appropriate Hello World gRPC client. Type, where $AMBASSADOR_IP is set to the external IP address above, and $AMBASSADOR_PORT is set to 80 (for HTTP-based Ambassador) or 443 (for a TLS-configured Ambassador).

docker run -e ADDRESS=${AMBASSADOR_IP}:${AMBASSADOR_PORT} enm10k/grpc-hello-world greeter_client

You should see something like the following:

$ docker run -e ADDRESS=35.29.51.15:80 enm10k/grpc-hello-world greeter_client

2018/02/02 20:34:35 Greeting: Hello world

Summary

Edge Stack makes it easy to publish gRPC services to your consumers, thanks to Envoy’s robust gRPC support. By publishing your services through Ambassador, you’re able to add authentication, rate limiting, and other functionality to your gRPC services.

Edge Stack API Gateway

Experience high-performance API Gateway with gRPC