gRPC - Our Web Service Future?

gRPC is a modern RPC framework and it looks like the future for our web services.

A couple of recent announcements are exciting times for developing web services:

I’ve been writing HTTP/1.1 web services using Go for about eighteen months. Along the way we started using Protocol Buffers and found them to be great for defining and documenting the message format. gRPC adds service definitions for Remote Proceedure Calls over HTTP/2 along with plenty of other niceness for writing services. I wrote a test project to see what gRPC might look like for our uses. Here’s what I really liked:

  • A complete client for my test gRPC service is 42 lines of code. It’s got token based auth, a single connection to the server with retries and back off, as well as message encoding and decoding. It uses two services (end points).
  • Comments in the protobuf service and message definition get turned into documentation in the generated libraries. The task of documenting an api just got that much easier.
  • Implementing the server is not a lot more complicated than the client. The greatest thing for me is how easy it is to add telemetry (metrics and logging) - there are interceptor types that can be implemented and added to the server e.g., to add method timing
// telemetry is a UnaryServerInterceptor.
func telemetry(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
	t := mtrapp.Start()
	i, err := handler(ctx, req)
	t.Track(info.FullMethod)
	return i, err
}

Which is added to the server config:

...
s := grpc.NewServer(grpc.UnaryInterceptor(telemetry))
...

In the test project I also added the option to read TLS certificates from the file system (mounted into the container at run time) or generate a self signed TLS certificate on the fly. The option to generate self signed certificates on the fly also makes it easy to do integration testing. It should also give options for deployment with the AWS Application Loadbalancer using either of:

  • TLS termination with a valid certificate on the EC2 instances and TCP passthrough on the balancer.
  • TLS termination with a valid certificate on the balancer and rencryption to the EC2 instances with a self signed certificate for End-to-End Encryption

Next steps for us are to pick a small service and try a full implementation and deployment. Something I’m excited to see happen soon.

I found the following resourses really helpful:

  • the gRPC docs
  • Kelsey Hightower’s grpc-hello-service repo was really useful, especially for understanding credentials (well worth a look for how to use JWT as well).
  • this CoreOS blog about handing gRPC and HTTP/1.1 JSON services in the same service.

Thanks to the gRPC team and AWS for their awesome recent releases. Also thanks to Richard Guest for discussions about HTTP/2 and TLS. The future is faster, will have less lines of code, and will be here soon.

Written on September 5, 2016