gRPC & gRPC-Web Services on Fly.io
Run gRPC & gRPC-Web services close to users with Fly.
Overview
This application demonstrates how to run a gRPC service on Fly.io. Fly runs tiny virtual machines at edge datacenters close to your users. gRPC is designed for fast, low overhead services with client and server code generation tools for many languages and VMs. gRPC servers run their own HTTP/2-based protocol, so they offer excellent performance benefits. This will not work out of the box with most hosting platforms. In fact, you can’t even call the service directly from a web browser. In this example, we’ll consume the gRPC service with a special web adapter. This allows you to create the client stubs and use them in browser-based JavaScript. This example uses the gRPC libraries for the main gRPC service and grpcwebproxy for browser support.What we’ll deploy
Since this is an example, we’ll write a quick gRPC service definition inhello.proto that has the following methods:
Deploying the gRPC service to Fly
- Install flyctl
- Login to Fly using
fly auth login - Create and deploy the app with
fly launch
Using the gRPC service
With gRPC, you’ll usually use the generated client libraries in the language of your choice. To make sure our service is working, we’ll use thegrpcurl tool:
Clock method. It sends a timestamp every second:
What’s Happening Inside
The gRPC servers that we’re running have one special feature that makes them difficult to deploy on many systems – they use the relatively new HTTP/2 protocol with a special feature called HTTP Trailers. Most HTTP/2 load balancers will terminate the HTTP/2 connection at the load balancer, and then make a more compliant HTTP/1.1 connection on the backend to the application server. This won’t work with gRPC. Luckily, Fly supports connecting to your service using HTTP/2, with the following configuration in yourfly.toml:
Using the Web Proxy
Because the gRPC services uses HTTP/2, you can’t make requests to it from inside a browser — there are currently no browser APIs that allow direct and full control of a HTTP/2 connection. To enable use inside a browser, there’s a gRPC-Web specification that converts a normal HTTP/1.1 request to and from the gRPC HTTP/2 format. Multiple proxy servers that implement the spec are available, like Envoy and grpcwebproxy (which we’ve deployed here). We’ve generated JS code for our gRPC definition using the instructions on the official gRPC-Web example page, so let’s see how to use it. In theweb-proxy folder, the client.js file calls the two methods of our service and writes the output into console.log. You can change the following line:
--server_http_max_write_timeout=3600 and --server_http_max_read_timeout=3600 to the ENTRYPOINT command in web-proxy/Dockerfile.
How Does Fly Fit Into This?
While gRPC works really well when communicating between servers on the same rack or same datacenter, the principles it uses apply equally well to clients on websites or mobile devices. The HTTP/2 communication channel remains open as much as possible to provide a low-latency and low-energy way to speak to your server, and the underlying Protocol Buffers serialization/de-serialization system is very low-overhead, so it’s great for cellular or metered-bandwidth connections. Running your backend services on Fly lets you put your services really close to every user in your global client base. If your data is in a central location, your services can use the local Redis cache that Fly provides at each edge location to cache data, and broadcast changes or invalidations via the global Redis command broadcast. There are also data solutions from other cloud providers like DynamoDB Global Tables (NoSQL), Aurora Multi-Master (SQL) or Google Cloud Spanner (SQL) that can help spread your data across the world and closer to your application servers. The reduced latency from having your servers close to your clients is especially useful in gRPC where you want all your calls to finish as quickly as possible. If your clients are mobile applications, they’ll benefit even more from the latency reduction when they’re running on cellular networks, where bandwidth is low and re-connections are common.Testing gRPC performance with ghz
Because gRPC applications use a special wire protocol, we can’t use cURL, wget or siege like we would on normal servers. Instead, there are special gRPC-aware tools like ghz that work great. After you enable the Fly regions you’d like to run your service in and set the container CPU & memory configuration to the size you need, you can use ghz to test your service: