Golang gRPC
Overview
The implementation of the Golang GRPC provides high performance and is an open source. This framework is a general RPC framework that gives first preference to HTTP/2 and mobile. The Golang GRPC package implements an RPC system which is called gRPC.
Introduction to gRPC
The Golang GRPC is also known as the Remote Procedure Call framework based on the action paradigm and is very similar to calling a function remotely from other microservices in gRPC. Due to this, the Golang GRPC is known for the type of inter-process communication protocol which is known as IPC protocol, and is built by using Protobufs which is mainly used for handling the messages or communication between the client and the server side. For intensive and effective/efficient communication, the Golang GRPC is preferred because it supports streaming of both the client and the server streaming side.
REST, on the other hand, is a resource-based protocol that is mainly used for informing the server that the client needs resources that are to be created, updated, read or delete depending upon the request of the body. The difference between Golang GRPC and REST API means that the REST API can’t be translated easily into the Golang GRPC but instead it needs to be updated according to the different protocol.
Is gRPC Faster than the Rest?
The protocol that Golang GRPC uses is HTTP/2 which has various ways for improving the performance:
- Compression of the header to reduce the size of the message
- Sending multiple requests by using multiplexing and then receiving multiple responses simultaneously over a TCP connection which is one-sided
- The persistent connection of TCP for multiple sequential requests and responses for a single connection of TCP
- Support of binary protocol buffer.
In Golang GRPC, the key element is the protocol buffer. They provide a representation of binary data which is more efficient as compared to any other text-based formats such as XML or JSON. Due to this binary format, the size of these messages are smaller and the combination of these smaller messages leads to faster communication that offers strong performance when compared to REST-based APIs.
What is Protobuf?
The protobuf is also known as protocol buffer and is mainly used with Golang GRPC. By using a protocol buffer, we can encode the semantics of the message in a form that is parseable and which can be shared between the client and the server. The protocol buffer, also known as protobuf is a language that is platform neutral and is used for structuring the data with fast built-in serialization and support for schema migration. It is important if we want to change the format of the message without introducing downtime to it. In order to understand this better, let's take an example.
Example of Protobuf File
In this example, the first thing that we have to do is to create a message type. For this, use the following code
Explanation:
In this example, the protobuf version that is being used is proto3 and a package name is specified which is package api.v1; for making things easier to import and generate the code.
After this, we will install the protobuf compiler by using the following command:
Please follow this guide to install protobuf in other OS where brew is not supported. Link
In order to check whether it's installed or not, use the following command:
Once this is done, install the protoc-gen-go by using the following command:
After this, add your path by using the following command:
You will see a code is generated once it is installed. The code that is generated is as shown below:
The package protoc also helps in generating various helper methods in order to work with the protobuf message one of which is field getters:
These are useful if we want to create an interface that can be abstracted across various types of messages. Now that things are working perfectly fine with the message type, let’s define the whole service by using the code given below:
For generating the client and the service code we can use protoc again:
By executing this file, it generates a file named activity_grpc.pb.go that contains all the necessary code for the client and the server.
Please note that instead of go_out and go_opt=paths we have used go-grpc_out and go-grpc_opt=paths and by combining these two, a message can be generated for the client and the server.
gRPC Implementation
In order to implement Golang GRPC, we will build a note-taking app that will use the client to send the note to the server and that can be archived and retrieved by using the keywords. We first have to initialize a new go project. You can keep your repo name the same as mentioned here or anything that you like. For this use the command is given below:
Now install the Golang GRPC go library by using the command given below:
Now install the protocol buffer compiler also known as protocbuf by using the command given below:
For Linux
For MacOS
For windows, use the following command:
Please make sure that the instructions match the operating system that you are using and are according to the documentation. If you are using a Linux system then use the command given below:
Now that we have installed the compiler, let’s install the protocol buffer for generating the code by using the following command:
The binaries for the tools should be the path name and for Linux use:
Creating Golang Project with gRPC
Implementation of Application Logic
The separation of the code logic from that of business requirement is considered a good practice and for this example, we will be creating a notes folder for the business logic of your application. Along with providing a fast and efficient way of data format, the protocol buffer also comes with the tools and the ecosystem, for making the development of applications easier. The declarative file “.proto” is used for describing the data structures which is a message and the interface of the application which is services. The file is simple to write and provides a high-level definition of what the application is doing at the backend. It can also generate the code required for manipulating the data structure since it is language-agnostic.
By creating a file notes.proto in the notes folder, the file gives information about which version of the protocol buffer is being used. The name of the package can also be used for preventing collision and various options to generate the code. For some cases where you only need to provide the name of the go package where the file belongs, use the command given below:
You can see that in the option, go_package, github.com/xNok/go-grpc-demo the name of the module matches with the name of the GitHub repo, and the notes define the package for which the code is generated. Now the next step is to define a data structure
Explanation:
In this example, we can see that the service called Notes is created that is used for performing two actions, namely, save and load. We have also specified that the rpc is needed for using these functions as a communication protocol. In this, the function save takes an argument as Note title and body as an input and returns back NoteSaveReply which contains a boolean value to check if the one was saved or not.
The syntax varies from the struct of the go language to that of the interface. The final step is the attribute of the note which is the title or ID. For example, in the note message, the ID of the title is 1 and that of the body is 2. The definition of data structure is now completed and the next step is to generate a related code by using the protoc compiler which we installed earlier.
By using the command given, it generates two files, namely, notes.pb.go that contains the code used for handling the serialization and deserialization of the data for the protocol buffer. The notes_grpc.pb.go, however, is used for handling the communication protocol of grpc. These files have hundreds of lines of code that we don't have to write. Now we have a structure of the code application but business logic is required. For this, create a file named note.go into the notes folder and copy the code given below:
In the above code given, we can notice that we can reuse the Note that is generated by using the protoc in the file notes.pb.go.
gRPC Server Implementation
For implementing the grpc server, create a folder called, notes_server inside the folder and create a file named main.go into this folder. We can now start implementing the simple server configuration by using the code given below:
By using this code, we can get a working server but there is no action that this server needs to perform. All the structures used for handling the Golang GRPC communication are generated in the file named notes/notes_grpc.pb.go. Now that we have built a server, we can implement the interface notes.NotesServer which is defined in the file generated. Now that we already have business logic in one package, we can create the interface notes.NotesServer by implementing the required methods which are saved and load in this file.
The last step is to register the notesServer that is created by adding a single line of code in the todo comment.
To check how it works, start the server by using the command given below:
gRPC Client Implementation
For implementing the grpc for the client, create a folder called notes_client, and in this folder create a file named main.go. Now we will add a simple client configuration and will then add Golang GRPC to it for creating a command that can save and load the note. Check the code given below to understand better the basic structure of it.
Explanation:
In this, we can add the elements that are required for communicating with the server. In this, the first to-do list is to establish a connection by using a code generated by the protoc and the constructor of the client which is NewNotesClient.
For the next todo, we can use the client for instantiating the remote call and the function which is saved and loaded from the server. For the save command, use the code given below:
For the load command, use the code given below:
Now that the client is ready to use, we can create a folder for the test data in order to hold the information of the note which is hard-coded. go run notes_client/main.go save -title test -content "Lorem ipsum dolor sit amet, consectetur " In order to retrieve the information that we just gave, use the command given below:
Further Information on gRPC
Till now we have only used the unary Golang GRPC for communication which means a single request is sent to the server and a single response is received back. The Golang GRPC also has 3 types of streaming protocols namely, client streaming, server streaming, as well as bidirectional streaming. Here each streaming executes well for large amounts of data.
Now we will be implementing a function SaveLargeNote that will utilize the streaming. But for that, we will be updating the service definition in the file notes.proto and include the following code:
In this, we can notice that the stream keyword is used for indicating the direction of the stream. This stream keyword is on the caller side of the function argument of the client-server streaming. By using protoc we can generate the protocol buffer b using the command given below:
We now have to implement the function SaveLargeNote in the server code:
The following elements are mentioned in the above code:
- stream.Recv()- it allows us to consume the packet without disturbing the order of the packet in the stream.
- io.EOF- it is the error that is used for ending the stream.
- stream.SendAndClose- it is used for closing the stream and sending that response to the client.
For updating the client, a new option is to be used that can instruct the client as to how to use the SaveLargeNote.
Now we have to create a stream by splitting the body of the code into individual chunks and inserting it inside the save code snippet.
Explanation:
Here in this example, the split function is used for creating the chunks in the body. The elements used in the above code are:
- stream.Send- it is used for sending the packet to the stream
- stream.CloseAndRecv- it is used for closing the stream and waiting for the response from the server.
Conclusion
- The implementation of Golang GRPC provides high performance and is an open source. This framework is a general Golang GRPC framework that gives first preference to HTTP/2 and mobile. The Golang GRPC package implements an RPC system which is called gRPC.
- The Golang GRPC is also known as the Remote Procedure Call framework based on the action paradigm and is very similar to calling a function remotely from other microservices in gRPC.
- REST, on the other hand, is a resource-based protocol that is mainly used for informing the server that the client needs resources that are to be created, updated, read, or delete depending upon the request of the body.
- The protobuf is also known as protocol buffer and is mainly used with grpc. By using a protocol buffer, we can encode the semantics of the message in a form that is parseable and which can be shared between the client and the server. The protocol buffer, also known as protobuf is a language that is platform neutral and is used for structuring the data with fast built-in serialization and support for schema migration. It is important if we want to change the format of the message without introducing downtime to it.