gRPC
gRPC is “a high performance, open source universal RPC framework”1. RPC stands for Remote Procedure Call, which is a kind of API that abstracts the complexity of making calls to a remote API by providing to the developer functions that works like if it was local2.
Quote
gRPC uses HTTP/2 under the covers, but HTTP is not exposed to the API designer3
gRPC uses Protocol Buffers as a serialisation format by default, but can use JSON. It works by extending the syntax of a .proto
file.
We need to define the messages (basic Protobuf structure) that will be exchanged whenever it’s an argument or a return value, along with the services
, that describe the procedures available.
Example from official tutorial1
First the messages:
1 2 3 4 5 6 7 8 9
// The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; }
and then the gRPC syntax to declare a service, a set of procedures:
1 2 3 4 5
// The greeter service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} }
A few advantages of gRPC:
- It abstracts away the calls to a remote API
- Compatible with several popular languages
- It makes possible to work with native objects of your current programming language
- It generate the classes and object/messages for you from a
.proto
file
From the dedicated file, it creates a gRPC server to receive requests from clients that themselves use a dynamic library generated via protoc
and a dedicated plugin.
This diagram illustrate how different clients/servers written in different languages can work together with the gRPC server and stubs (a client) created.
# File upload
Although a few users would recommend to use a REST API to handle uploads, other says that it depends on the size of images and your app4.
Implementing file uploading in gRPC seems to be done via stream
s56, one of
its features. The core principle is to send files in small chunk (by default, gRPC limits incoming messages to 4 MB in case the developer hasn’t thought about message size, but it can be increased7) Note that we send the metadata apart from the file, and it’s not a standard so we have to define what’s inside (path, name, extension/type…).
Implementations seem to also use oneof
keyword to avoid sending metadata each time we’re sending a chunk of file.
|
|
Introduction to grpc (2022) gRPC. Available at: https://grpc.io/docs/what-is-grpc/introduction/ (Accessed: January 14, 2023). ↩︎
Remote procedure call (2023) Wikipedia. Wikimedia Foundation. Available at: https://en.wikipedia.org/wiki/Remote_procedure_call (Accessed: January 14, 2023). ↩︎
Nally, M. (2020) GRPC vs rest: Understanding grpc, openapi and rest and when to use them in API design | google cloud blog, Google. Google. Available at: https://cloud.google.com/blog/products/api-management/understanding-grpc-openapi-and-rest-and-when-to-use-them (Accessed: January 14, 2023). ↩︎
r/grpc - grpc is suitable to handle file uploads? (no date) reddit. Available at: https://www.reddit.com/r/grpc/comments/oj2k9n/grpc_is_suitable_to_handle_file_uploads/ (Accessed: January 14, 2023). ↩︎
Foong, N.W. (2022) GRPC file upload and download in Python, Medium. Better Programming. Available at: https://betterprogramming.pub/grpc-file-upload-and-download-in-python-910cc645bcf0 (Accessed: January 14, 2023). ↩︎
vIns (2022) GRPC file upload with client streaming, Vinsguru. Available at: https://www.vinsguru.com/grpc-file-upload-client-streaming/ (Accessed: January 14, 2023). ↩︎
Anderson, E. (2019) Should I transmit large data sets via grpc without manual chunking?, Stack Overflow. Available at: https://stackoverflow.com/a/58435745 (Accessed: January 14, 2023). ↩︎