When to use gRPC over REST

  • Easy to understand.
  • Web infrastructure is already built on top of http
  • Tools for in inspection, modification, testing are readily available.
  • Loose coupling between client and server makes changes relatively easy.
  • There are lot of frameworks in most of the widely used languages to create REST Api’s.
  • Http status codes are well defined and helps in identifying cause of problems
  • While creating RESTful services, most of us follow a standard practice of writing client library and all we need to do is update client library whenever there is a change in api contracts.
  • Streaming is difficult and its highly impossible in most of the languages.
  • Duplex streaming is not possible.
  • Hard to get multiple resources in single request.
  • Need semantic versioning whenever the api contract needs to be changed.
  • Language agnostic
  • Machine Readable: Protobuf can be used to exchange messages between services and not over browsers because proto’s are binary or machine readable and its not human readable.
  • Provides Generators to Serialize or Deserialize : Protobuf can be easily compiled to source code (Currently it supports 8 different languages JAVA, Objective c, python, c#, ruby, go , java nano, c++) with protobuf compiler (call it as protoc) and with which serialization or deserialization is easier and there is no need of hand parsing.
  • Supports types and Validations: Unlike json , we can specify field types and add validations for the same in the .proto file.
  • Lesser Boilerplate code : Since it supports types and has source code generators, we don’t need to write much of boilerplate code or hand parsing the response.Supports Interfaces to RPC: If you want to use your message types with an RPC (Remote Procedure Call) system, you can define an RPC service interface in a proto file and the protocol buffer compiler will generate service interface code and stubs in your chosen language.
Comparison of different serialization schemes
sample proto
Simple rpc work flow
  • A client application makes a local procedure call to the client stub containing the parameters to be passed on to the server.The server run-time library receives the request and calls the server stub procedure which unmarshalls (unpacks) the passed parameters and calls the actual procedure.
  • The client stub serializes the parameters through a process called marshalling forwards the request to the local client-time library in the local computer which forwards the request to the server stub.
  • The server stub sends back a response to the client-stub in the same fashion, a point at which the client resumes normal execution.
  • Create the service definition and payload structure in the Protocol Buffer (.proto) file.
  • Generate the gRPC code from the .proto file by compiling it using protoc
  • Implement the server in one of the supported languages.
  • Create the client that invokes the service through the Stub.
  • Run the server and client(s).
  • High performance along with Safety : gRPC is high performance with google protobuf and http/2 protocol which is Multiplexed, single tcp connection , transports data as binary, enables duplex streaming etc.
  • Duplex streaming : Allows clients side and service streaming simultaneously.
  • First Class Load Balancing: gRPC has built in library feature it can intelligently pick which backend to send traffic to.
  • Selective message compression: If you are streaming mixed text and images over a single stream (or really any mixed compressible content), you can turn off compression for the images.
  • Auto generated client code: With protoc we can easily generate the client code and server code.
  • Heavily optimized: gRPC library is under continuous benchmarks to ensure there are no speed regressions.
  • Connection Pool : We can create connection pool containing persistent connection to server through managed channels with states connected or idle.
  • No support for browsers hence cannot be used for external services.
  • No url end points hence can’t be tested with postman or curl to check the response.
  • No predefined status codes and creating custom status code may end up in conflicts.
  • When the microservices is only internal and when one server needs to talk to the other.
  • When your internal services requires duplex streaming with high load of data.
  • When you don’t feel to write client libraries.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Browser Automation with Python and Selenium — 7: Keyboard and Action Chains

Impala Discussion With The Product Manager (Greg Rahn)

All Loops Are a Code Smell

Optimising the Transportation Costs of a Leading Fire Equipment Manufacturer in Singapore with…

SimplivLearning vs Udemy: A Complete Comparison 2021

Pub/Sub: Send a million messages per second and save thousands of $ a month using Avro

[Unity ECS] Enabling more concurrency, more performance with BarrierSystem & EntityCommandBuffer

How Boureima is Thriving While Making His Dream Come True

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sankar P

Sankar P

More from Medium

How to make your consumption faster in Rabbitmq (Prefetch count)

Version Controlling | NoSQL Databases

Percentile Latency in Backend

When Should You Apply CQRS Software Architecture Pattern?