Difference between RPC and Http Library¶
Rpc is a way to communicate between two processes in different hosts, underlying the network. Http also the most popular protocol communicating between server and client. We can also so any network could be a way to communicate between two processes.
However, what’s the differences between a network protocol and a rpc framework? I will try to explain it based on gRPC
and http
protocol. Note that it makes no sense to compare a protocol and a framework, they don’t stay in the same layer. This article mainly focus on what difference between a rpc framework and http library which is treated as a rpc framework.
Key points of gRPC¶
Grpc is a popular rpc framework, see the introduction and core conspts here, it’s no sense to copy-paste the paragrah here, instead, I will try to analyze some ideas based on the previous knowledge.
- protocol buffers: used by grpc as
Interface Definition Language
(IDL) and asunderlying message interchange format
. - rpc hehavior: directly call a method on a different machine as if it were a local object
stub
: provides the same methods as the server , client can then just call those methods on the local object, wrapping the parameters for the call in the appropriate protocol buffer message type - gRPC looks after sending the request(s) to the server and returning the server’s protocol buffer response(s).- protocol buffer: serializing mechansim. Compiler
protoc
generates data access class which has methods to serialize/parse the whole structure to/from raw bytes. - Client sends a request may contain two step,
metadata
and therequest message
. The response also containsstatus details
andoptional metadata
. Metadata
is a particular RPC call information with a list of key-value pairs.
If skim the introduce, we can easily create the client and make a rpc call. Http way afforts more works on creating a rpc client and server to hide complexity.
Here, I can say gRPC does a lot work to hide the network lifespan complexity with the help of grpc
framework and protoc
. In grpc ecosystem, protoc
generates the code to help create client to make rpc call, which means the client needn’t to maintain by users themseleves.
conn, err := grpc.Dial(*addr)
// ignore lines
c := pb.NewGreeterClient(conn)
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
For more details about how gRPC hides the complexity, they are not the topic here, could refer another topic: Todo: track for unfinishing
Key points of HTTP¶
Server side will provide a set of APIs for clients, refer to the IBM authentication restful API:
The server side focus on how to manuplate the data and make a response. It needs to:
- bootstrap a http server
- fetch the data from many places, like header, parameter and body.
- register handlers
If client wants to get the data from the remote, it still needs to do:
- construct request, inclde header, parameter and body.
- send the request and handle the network error.
- deserialize data to the struct.
Those points increase highly work for people who need to do a rpc call. This is duplicated work which means we always need to wrap a http client to expose easy use method outside and hide the network details. The go-github project is such a project, Could check How Go-Github Lib Is Designed.
What’s more, you could find that encapsulate a client(maybe we can call it rpc client
) is a common work, all coders need to do it if they want to do a rpc call.
Conclusion¶
In short, gRPC framework and http library focus on different points. Grpc framework provides a scalable and esay solution for developers to fetch data from remote without additional overload. However, http library mainly implements limited features mentioned in RFC.
As a protocol, it rules the communcating format, so does the implementation of a protocol. Http librariy always implements the RFC rules and conventions, but doesn’t contains further steps for making request to reduce over encapsulation. As a framework, it aims to reduce the complexity for its user, that’s the value of a framework.
Note: After learning more about grpc framework, you will find that this article is so limited as it only covers quite a little about grpc. Need to learn more to find out grpc.