This content originally appeared on Bits and Pieces - Medium and was authored by Abderraouf Benchoubane
What is gRPC? And why you should consider it for your next application.
Today's applications are large and complex. Sometimes, the application is split into smaller functional parts deployed in different computers and regions worldwide, which is why we have APIs. APIs are essential for building complex, distributed applications that need to communicate with each other. By providing a set of standardized interfaces for accessing and manipulating data, APIs make it possible for different parts of an application to work together seamlessly, regardless of where they are located or what technologies they use.
You may have heard of RESTful (Representational State Transfer) APIs if you're a software developer. REST APIs are everywhere. Most services like OpenWeather offer a REST API for us developers to fetch weather data.
What if I told you that REST is not the only way to build APIs and that there is another way to make APIs using a different approach called RPC? and that there is a framework called gRPC that makes it easy to use?
What is RPC?
RPC stands for Remote Procedure Call. In this approach, a client gets the information from the server by causing a remote method invocation meaning that calling the API is as simple as a function call from the client. From the developer's point of view, there is no HTTP GET or POST request to make. You only have to call a function that will execute on the server and get you the information. What does this mean? Let's look at the figure below, which showcases how a REST API works and how an RPC API would do the same. We will use the example of weather API.
Figure 1 shows a basic RESTful API for fetching the weather in Montreal. The mobile application makes a GET request to the server, and once the information is available, the server replies by sending the response in JSON or XML format. An example of a JSON response is this.
{
"temperature": "-10",
"unit": "celcius"
}
On the other hand, an RPC API like the one in Figure 2 below
The client calls a method named getWeather(cityName) , and it returns an integer representing the temperature. The client's request would resemble something like this.
const cityName: string = "Montreal";
final temperature = getWeather(cityName); // this is called on the server directly
This sounds great, but how can a client application call a procedure on the server? After all, the client application might be written in Swift while the server is in Java or Python.
How does RPC work?
While RPC is a way of building APIs, it does not say how the client's request should be mapped to a function on the server.
One way to accomplish this is to use a stub or proxy on the client side. The stub object represents the procedure on the server, and when the client calls a method on the stub, it passes the necessary parameters. The stub then transforms the input parameters into a message format that can be transmitted over the network, typically by serializing them into a binary or text-based format.
The message is transported over the network using a TCP, HTTP, or other custom protocol. Once the server receives the request, it unpacks the input parameters from the message and executes the specified function or method, producing a result or output parameters.
The server then serializes the output parameters or returns value into a message format that can be transmitted to the client and sends the message back over the network. The client receives the message, unpacks the output parameters or return value, and can use these results as needed.
Now that we have discussed the idea behind RPC and had a top-level view of how RPC APIs work, it is time to look into how to build an RPC API.
What is gRPC?
gRPC stands for "gRPC Remote Procedure Call," a high-performance, open-source RPC framework. Google developed it in 2016, and it is used by many organizations and companies (Netflix, Cisco and others). gRPC is used to communicate between micro-services and last-mile between server and client applications. It utilizes HTTP/2 as the default network transport layer, which makes the network messages compact and efficient.
gRPC uses protocol buffers to declare the data schema of the API, so what are protocol buffers?
What are protocol buffers?
Protocol Buffers can be thought of as an intermediate definition language that is used to describe the data schema of the API. Once the schema is defined using Protocol Buffers, it can be compiled into code for various programming languages such as C++, Go, or Python. This generated code provides an easy and efficient way to serialize, deserialize, and transmit data between different services, making it a critical component of the gRPC ecosystem.
Figure 3 below shows a protocol buffer file for a weather API service.
In the image above, we declare a service named WeatherService. It has a remote procedure called getWeather. The procedure takes a WeatherRequest message and returns the result in a Weather message. The WeatherRequest has a cityName as a string, and the cityName has a field number of 1, meaning it is the first field of the message. The Weather message follows a similar format.
From this definition, you can use proto CLI to generate the source code of C++, Go, Python, and many other languages.
Now that we introduced gRPC and how it uses protocol buffers to define its data schema, let's talk about the advantages of using gRPC over REST.
Advantages of gRPC
gRPC offers critical advantages over other technologies like REST.
- Performance, gRPC uses protocol buffers, a binary format for serializing data. A protocol buffer is much more efficient and performant than JSON or XML used by REST APIs.
- Type safety, gRPC uses a strongly typed IDL, which reduces the risks of the runtime error caused by casting to the wrong data type.
- Multi-language support, gRPC supports many programming languages, which makes it easier to use the adequate programming language for each microservice.
- Code generation, gRPC can generate the client code and the server code to serialize the messages, which reduces the amount of boilerplate the developer has to write and speeds up the development process.
- Streaming, gRPC offers bi-directional streaming between the client and server.
While gRPC is an excellent piece of technology, it is not without its downsides, and these should be considered before deciding to use gRPC for your API.
Downsides of gRPC
- Complexity: gRPC can be more complex to set up and use than REST, especially for developers unfamiliar with Protocol Buffers and other concepts used by gRPC.
- Learning Curve: Developers need to learn new concepts and tools, such as Protocol Buffers and the gRPC ecosystem, which can add to the learning curve.
- Debugging: Debugging gRPC communication issues can be challenging, as it involves network communication and serialization/deserialization of binary data.
- Versioning: Updating the gRPC API can be difficult, especially when it involves changing the data schema or breaking backward compatibility.
Now that we have discussed the ups and downs of gRPC, it is good to mention when to use it.
When to use gRPC?
gRPC is a good choice when you want to build a high-performance, scalable and distributed system or microservices that require efficient communications. Use cases of gRPC include the following
- Microservices architecture: gRPC's efficient communication, streaming support, and language-agnostic design make it well-suited for building microservices that communicate with each other
- IoT and mobile devices: gRPC's lightweight and efficient communication makes it a good choice for communication between IoT devices and mobile devices and the backend systems.
- High-performance data processing: gRPC's binary encoding and streaming support make it a good choice for high-performance data processing scenarios such as real-time data analytics, image processing, and machine learning.
- Cross-platform applications: gRPC's multi-language support and platform-agnostic design make it a good choice for building applications that run on different platforms and use other programming languages.
Conclusion
gRPC is a powerful and efficient RPC framework that offers many benefits over REST and other RPC frameworks. Using Protocol Buffers, binary encoding, and streaming support, gRPC provides a high-performance and scalable communication layer that can handle complex communication patterns between different parts of a distributed system or microservices architecture.
While gRPC does have some downsides and challenges, such as its complexity and learning curve, these are outweighed by its benefits in many use cases. gRPC's multi-language support, code generation, and strong type safety make it a good choice for building modern cross-platform applications requiring fast and efficient communication between different system parts.
Whether you're building microservices, IoT devices, or high-performance data processing systems, gRPC offers a powerful and flexible communication layer to help you achieve your goals. By taking advantage of gRPC's features and benefits, you can build faster, more efficient, and more scalable systems that adapt and evolve with your needs.
From monolithic to composable software with Bit
Bit’s open-source tool help 250,000+ devs to build apps with components.
Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.
Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:
→ Micro-Frontends
→ Design System
→ Code-Sharing and reuse
→ Monorepo
Learn more
- How We Build Micro Frontends
- How we Build a Component Design System
- How to reuse React components across your projects
- 5 Ways to Build a React Monorepo
- How to Create a Composable React App with Bit
The Power of gRPC: Efficient, Cross-Platform Communication was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Abderraouf Benchoubane
Abderraouf Benchoubane | Sciencx (2023-03-06T15:10:08+00:00) The Power of gRPC: Efficient, Cross-Platform Communication. Retrieved from https://www.scien.cx/2023/03/06/the-power-of-grpc-efficient-cross-platform-communication/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.