ProtoBuffer Faster than JSON

gRPC 는 HTTP/2 위에서 Protocol Buffers(protobuf)를 사용해 직렬화된 바이트 스트림으로 통신하므로 JSON 기반의 통신보다 속도가 빠르다. Protocol Buffers are language-neutral, platform-neutral extensible mechanisms for serializing structured data.

Protobuf 의 장점은 성능적인 부분도 있지만 API 들의 source of truth 가 된다는 장점이 있다. 또한 Proto File 만 배포하면 환경과 프로그램 언어에 구애받지 않고 서로 간의 데이터 통신이 가능하다.

Stub

RPC 의 핵심 개념은 Stub 이다.

In distributed computing, a stub is a piece of code that converts parameters passed between the client and server during a remote procedure call (RPC). The main purpose of an RPC is to allow a local computer (client) to invoke procedures on a remote computer (server).

Interface Definition Language 을 사용하여 호출 Spec 을 정의하고 함수명, 인자, 반환값에 대한 데이터형이 정의된 IDL 파일을 rpcgen 으로 컴파일하면 stub code 가 자동으로 생성된다.

banksalad의 경우에는 idl 리포지토리에 protobuf 파일뿐 아니라 protoc 를 통해 변환된 Go, Java, Python, Swagger, Swift 파일 등이 있는 gen 폴더까지 git 에 포함해 관리하고 있다고 한다.

FieldMask

Practical API Design at Netflix:

FieldMask

Grpc Gateway

grpc gateway 를 사용하여 gRPC 서비스가 기존 서비스들(HTTP JSON) 과의 호환을 지원하도록 할 수 있다.