6

Building API with gRPC - Part 1 - Knoldus Blogs

 3 years ago
source link: https://blog.knoldus.com/building-api-with-grpc-part-1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Building API with gRPC – Part 1

Reading Time: 3 minutes

Hey everyone, have you ever faced the problem to create the application/API using gRPC with Scala. If yes then here is the solution to your problem. Here I explained what is gRPC and how to create a simple application or micro-service through it by defining the protobuf and Scala. In this first part, we will know what is gRPC, its cons, and prons. We also know how it is better than others like JSON or XML.

Introduction

Let’s begin with what gRPC stands for, so basically gRPC stands for general-purpose Remote Procedure Calls, of course!
gRPC is a modern, open-source remote procedure call (RPC) framework that can run anywhere. You simply write the gRPC protobuf messages and create the application structure through it which we will discuss in the next part. It enables client and server applications to communicate transparently and makes it easier to build connected systems. Read the longer Motivation & Design Principles post for background on why we created gRPC.

What are the protocol buffers?

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the “old” format.

Why would I want to use gRPC?

The main usage scenarios:

  • Low latency, highly scalable, distributed systems.
  • Developing mobile clients who are communicating to a cloud server.
  • Designing a new protocol that needs to be accurate, efficient and language independent.
  • Layered design to enable extension eg. authentication, load balancing, logging and monitoring, etc.

Who’s using this and why?

gRPC is a Cloud Native Computing Foundation (CNCF) project.

Google has been using a lot of the underlying technologies and concepts in gRPC for a long time. Currently it is being used in several of Google’s cloud products and Google externally facing APIs. gRPC is also being used by Square, Netflix, CoreOS, Docker, CockroachDB, Cisco, Juniper Networks and many other organisations and individuals.

Can I use gRPC with other data formats like (JSON, Protobuf, Thrift, XML)?

Yes. gRPC is designed to be extensible to support multiple content types. The initial release contains support for Protobuf and with external support for other content types such as FlatBuffers and Thrift, at varying levels of maturity.

How does gRPC help in mobile application development?

gRPC and Protobuf provide an easy way to precisely define a service and auto-generate reliable client libraries for iOS, Android and the servers providing the back end. The clients can take advantage of advanced streaming and connection features which help save bandwidth, do moreover fewer TCP connections and save CPU usage and battery life.

Why is gRPC better than any binary blob over HTTP/2?

This is largely what gRPC is on the wire. However, gRPC is also a set of libraries that will provide higher-level features consistently across platforms that common HTTP libraries typically do not. Examples of such features include:

  • interaction with flow-control at the application layer
  • cascading call-cancellation
  • load balancing & failover

Why is gRPC better/worse than REST?

gRPC follows HTTP semantics over HTTP/2 but it explicitly allows for full-duplex streaming. It diverges from typical REST conventions as we use static paths for performance reasons during call dispatch as parsing call parameters from paths, query parameters, and payload body add latency and complexity. gRPC has also formalized a set of errors that are more directly applicable to API use cases than the HTTP status codes.

Difference between gRPC and REST

Grpc-Rust-And-Graph

How gRPC works!

gRPC by Use Cases

It is used in the last mile of computing in mobile and web clients. Since it can generate libraries for iOS and Android and uses standards-based HTTP/2 as transport allowing it to easily traverse proxies and firewalls.
There is also work underway to develop a JS library for use in browsers.
Beyond that, it is ideal as a micro-services interconnect, not just because the core protocol is very efficient but also because the framework has pluggable authentication, load balancing, etc. Google itself is also transitioning to use it to connect micro-services.

Overview

In gRPC, the client application we create can directly call a method on a server application on a different machine. It is possible if it were a local object, making it easier for us to create distributed applications and services.

References:
* gRPC documentation.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK