Build high performance services using gRPC

REST and gRPC are two different architectural styles for building APIs. API development landscape is dominated by REST now, be it web applications, mobile backends or microservices. With gRPC gaining popularity, REST is no longer the default choice for API development.

Both REST and gRPC have their own advantages. However, there are use cases where one is better than the other. It is important to understand when gRPC vs REST performance will be best in an API.

REST (Representational State Transfer) is an architecture style for applications that typically use multiple web services. A RESTful service provides an application access to its web resources in a textual representation and supports reading and modification of them with a stateless protocol. OpenAPI Specification (OAS) is a standard to describe the features of an API there by making it easy for the developers. There are many testing solutions available which can crawl through the OAS description and generate automated test suites for testing and validating the APIs .

Advantages

  • Easy to understand and widely used
  • Wide range of tools available for testing and development
  • The client and server is loosely coupled thereby making changes is easy
  • Most of the programming languages have frameworks built-in for REST API development
  • The http status codes are standardized and help in defining the API responses

Downsides

  • REST does not support streaming in many of the languages with duplex streaming impossible
  • If the API contract has to be changed, it has to be versioned
  • When there is a API contract change, the client library has to be updated to the new version

gRPC is an open-source RPC framework developed at Google. It uses HTTP/2 protocol and an Interface Description Language (IDL) called Protocol Buffers (or protobuf). gRPC uses contract-first approach and protobuf allow you to define the interface to be used in service-to-service communication independent of the programming language. Most of the popular programming languages provide tools to translate the protobuf into code. gRPC enables services to communicate with each other independent of the programming language. With features like selective message compression, client side or server-side load balancing gRPC enables faster communication between services.

Advantages

  • Http/2 protocol is multiplexed there by giving better performance
  • gRPC supports different types of RPC:
    • Unary RPC – Client sends the request message to server and receives the response
    • Server streaming RPC – Client sends one request message to the server and in turn receives a sequence of responses
    • Client streaming RPC – Client sends a sequence of messages to server and receives a single response
    • Bidirectional streaming RPC – Client and the server exchanges messages in both directions
  • Load Balancing: gRPC has built-in intelligence to choose the server to send traffic. It supports server side load balancing also in addition to the client side load balancing.
  • Client code generation is easy: Most of the popular programming languages provide tools to translate the protobuf into code (eg: protoc)
  • Reliably Optimized: gRPC library is undergoing various checks and benchmarks to ensure there are no performance regressions
  • Connection Pool: With help of managed channels, persistent connection to server can be created
  • Selective message compression: If mixed contents (text/image/..) are being transmitted over the same stream, compression can be enabled just for text/image as needed

Downsides

  • Currently, browsers do not support gRPC and cannot be used with clients who rely on browsers to view the data. Might have to create a duplicate REST service also in that case.
  • No URL end points or routes like REST – gRPC cannot be tested using API testing tools like postman or curl. gRPC specific tools like gRPC UI or gRPC curl need to be used.
  • Unlike REST there are no predefined status codes

Summary

REST has been a popular API framework for about a decade, and it offers benefits of standardization and ease of use. gRPC has started to become a reliable choice, especially for streaming use cases which is difficult to do in REST, or to move to binary data instead of json. While both REST and gRPC offer specific benefits, developers should choose the one that is best suited for their project. With microservices and cloud technologies emerging, gRPC could become a preferred choice very soon. Ease of development and improved performance are some advantages that will increase the acceptance of gRPC.

Do share your comments.

Author Details

Deepu Prabhakaran Vasantha Kumari

Deepu is a Senior Technology Architect at Infosys Digital Experience. He is a technocrat with expertise in architecting digital transformation projects for customers. He supports organizations across the globe in their cloud adoption journey with knowledge in a range of digital and middleware technologies.

COMMENTS(1)

Leave a Reply to Krishna kalesh Balakrishnan Cancel reply

Your email address will not be published.