gRPC or REST
Why not both?
@m_m_murad
Mohammad Murad
Backend and Mobile engineer
Agenda
Agenda
● What is REST?
Agenda
● What is REST?
● Disadvantages of REST.
Agenda
● What is REST?
● Disadvantages of REST.
● What is gRPC?
Agenda
● What is REST?
● Disadvantages of REST.
● What is gRPC?
● Advantages of gRPC.
Agenda
● What is REST?
● Disadvantages of REST.
● What is gRPC?
● Advantages of gRPC.
● gRPC and REST services from a single codebase.
REST
REST
● Representational State Transfer.
REST
● Representational State Transfer.
● Not a protocol but an Design Pattern.
REST
● Representational State Transfer.
● Not a protocol but an Design Pattern.
● Uses HTTP protocol.
REST
● Representational State Transfer.
● Not a protocol but an Design Pattern.
● Uses HTTP protocol.
● Objects that are send or saved are called resources.
REST
● Representational State Transfer.
● Not a protocol but an Design Pattern.
● Uses HTTP protocol.
● Objects that are send or saved are called resources.
● Operations are described by HTTP verbs.
REST
● Representational State Transfer.
● Not a protocol but an Design Pattern.
● Uses HTTP protocol.
● Objects that are send or saved are called resources.
● Operations are described by HTTP verbs.
● Transfers data as XML or JSON or Protocol Buffers.
A REST API can be -
A REST API can be -
XML + HTTP
A REST API can be -
XML + HTTP
JSON + HTTP
A REST API can be -
XML + HTTP
JSON + HTTP
Protocol Buffers + HTTP
XML + HTTP
JSON + HTTP
Protocol Buffers + HTTP
What does a REST call look like?
Is this a REST call?
GET http://0.0.0.0:8081/updateBook
Content-Type: application/json
Authorization: Basic asfd34hk82hg32j==
{
"id": 12,
"author": "Prem"
}
PUT http://0.0.0.0:8081/books
Content-Type: application/json
Authorization: Basic asfd34hk82hg32j==
{
"id": 12,
"author": "Prem"
}
RESTish
RESTful API
PUT http://0.0.0.0:8081/api/v1/books/12
Content-Type: application/json
Authorization: Basic asfd34hk82hg32j==
{
"author": "Prem"
}
Disadvantages of REST?
Disadvantages of REST?
● The data being transferred is heavily bloated.
{
"id": 12,
"name": "Developing in Node",
"author": "Prem Pratap Tomar",
"rating": 3.2,
"release_date": "2019-06-17T19:12:43.905Z",
"units_sold": 123456,
"publisher": "Leaf publications",
"best_sell": true
}
Bloated data
{
"id": 12,
"name": "Developing in Node",
"author": "Prem Pratap Tomar",
"rating": 3.2,
"release_date": "2019-06-17T19:12:43.905Z",
"units_sold": 123456,
"publisher": "Leaf publications",
"best_sell": true
}
Message size - 190 bytes (Ignoring the white spaces)
Bloated data
{
"id": 12,
"name": "Developing in Node",
"author": "Prem Pratap Tomar",
"rating": 3.2,
"release_date": "2019-06-17T19:12:43.905Z",
"units_sold": 123456,
"publisher": "Leaf publications",
"best_sell": true
}
Message size - 190 bytes (Ignoring the white spaces)
Useful info - 107 bytes (56.3%)
Unnecessary info describing useful info - 83 bytes (43.7%)
Bloated data
{"books":[{"id": 12,"name":"Developing in Node", "author":"Prem Pratap
Tomar","rating":3.2,"release_date": "2019-06-17T19:12:43.905Z", "units_sold":1
23456,"publisher":"Leaf
publications", "best_sell":true},{"id":13,"name":"Developing in
Node","author":"Prem Pratap
Tomar","rating":3.2,"release_date": "2019-06-17T19:12:43.930Z", "units_sold":1
23456,"publisher":"Leaf
publications", "best_sell":true},{"id":14,"name":"Developing in
Node","author":"Prem Pratap
Tomar","rating":3.2,"release_date": "2019-06-17T19:12:43.955Z", "units_sold":1
23456,"publisher":"Leaf publications", "best_sell":true}]}
Message size - 190 * 3 = 570 bytes (approx)
Useful info - 107 * 3 = 321 bytes (approx)
Unnecessary info - 83 * 3 = 249 bytes (approx)
Bloated data
Disadvantages of REST?
● The data being transferred is heavily bloated.
● There is no formal contract to communicate.
Disadvantages of REST?
● The data being transferred is heavily bloated.
● There is no formal contract to communicate.
● HTTP verbs don’t always describe your API.
Disadvantages of REST?
● The data being transferred is heavily bloated.
● There is no formal contract to communicate.
● HTTP verbs don’t always describe your API.
● Streaming is difficult.
Disadvantages of REST?
● The data being transferred is heavily bloated.
● There is no formal contract to communicate.
● HTTP verbs don’t always describe your API.
● Streaming is difficult.
● GraphQL problems.
What if not REST?
gRPC
gRPC
● gRPC Remote Procedure Call
gRPC
● gRPC Remote Procedure Call
● Open source version of an internal Google product.
gRPC
● gRPC Remote Procedure Call
● Open source version of an internal Google product.
● High performance RPC framework.
gRPC
● gRPC Remote Procedure Call
● Open source version of an internal Google product.
● High performance RPC framework.
● Uses HTTP/2 protocol.
gRPC
● gRPC Remote Procedure Call
● Open source version of an internal Google product.
● High performance RPC framework.
● Uses HTTP/2 protocol.
● Uses Protocol Buffers for message interchange.
gRPC
● gRPC Remote Procedure Call
● Open source version of an internal Google product.
● High performance RPC framework.
● Uses HTTP/2 protocol.
● Uses Protocol Buffers for message interchange.
● Can use other formats like JSON, XML.
Writing a gRPC service.
Writing a gRPC service. (3 step process)
Messages
and
Services
Define
Writing a gRPC service. (Step 1/3)
Messages
and
Services
compiler
Define Compile
Generated Code
Writing a gRPC service. (Step 2/3)
Messages
and
Services
compiler Generated Code
Servers
and
Clients
ImplementDefine
Writing a gRPC service. (Step 3/3)
Compile
Let’s make a calculator services
Protocol Buffer
Protocol Buffer
● Interface Definition Language (IDL).
Protocol Buffer
● Interface Definition Language (IDL).
● Message interchange format.
Protocol Buffer
● Interface Definition Language (IDL).
● Message interchange format.
● Platform neutral mechanism for serializing structured data.
Protocol Buffer
● Interface Definition Language (IDL).
● Message interchange format.
● Platform neutral mechanism for serializing structured data.
● Similar to XML or JSON but very small and fast.
Protocol Buffer
● Interface Definition Language (IDL).
● Message interchange format.
● Platform neutral mechanism for serializing structured data.
● Similar to XML or JSON but very small and fast.
3-10x smaller than XML
10-100X faster than XML
Let’s make a calculator services
Messages
and
Services
.proto
Compiler
protoc
Generated
Code
Servers
and
Clients
Define Compile Implement
Messages
and
Services
.proto
Compiler
protoc
Define Compile Implement
Servers
and
Clients
Generated code
C#
C++
Dart
Go
Java
Node
Objective-C
PHP
Python
Ruby
Go Service
gRPC server
Mobile client
Desktop client
gRPC Stub
gRPC Stub
Ruby Service
gRPC server
Go Service
gRPC server
gRPC
Stub
Java Service
gRPC
Stub
Python
Service
gRPC server
gRPC
Stub
Mobile client
Desktop client
gRPC
Stub
gRPC
Stub
gRPC server
Why gRPC? What do I get?
Why gRPC? What do I get?
● Generated code in 10 languages.
Why gRPC? What do I get?
● Generated code in 10 languages.
● Strict contract between servers and clients.
Why gRPC? What do I get?
● Generated code in 10 languages.
● Strict contract between servers and clients.
● Performant and efficient on the wire
Small binary messages.
Faster serialisation and deserialisation.
Multiplexing and Header Compression.
● Generated code in 10 languages.
● Strict contract between servers and clients.
● Performant and efficient on the wire
Small binary messages.
Faster serialisation and deserialisation.
Multiplexing and Header Compression.
● In addition to traditional unary APIs you also get
Client Streaming APIs
Server Streaming APIs
Bi-Directional streaming APIs
Why gRPC? What do I get?
But...
What about our existing APIs?
But...
What about our existing APIs?
Do we
● Develop both flavors parallelly?
● Deprecate existing APIs and ask clients to update their code.
grpc-gateway
grpc-gateway
● A plugin for protoc compiler.
grpc-gateway
● A plugin for protoc compiler.
● An open source tool by grpc-ecosystem.
grpc-gateway
● A plugin for protoc compiler.
● An open source tool by grpc-ecosystem.
● Generates a reverse proxy which can communicate to your gRPC server
using JSON over HTTP.
Using grpc-gateway
Using grpc-gateway (3 step process)
Using grpc-gateway (Step 1/3)
1. Annotating your proto file.
Using grpc-gateway (Step 2/3)
1. Annotating your proto file.
2. Generate reverse proxy and OpenAPI definitions.
Annotated Messages
and Services
.proto
Compiler
protoc
Server interfaces and
client stubs
Annotated Messages
and Services
.proto
Compiler
protoc + grpc-gateway
Server interfaces and
client stubs
Open API specification
your-service.json
Reverse proxy in Go
your-service.pb.gw.go
Using grpc-gateway (Step 3/3)
1. Annotating your proto file.
2. Generate reverse proxy and OpenAPI definitions.
3. Write an entry point for the reverse proxy.
DEMO
Thank you
More about gRPC and grpc-gateway
● https://grpc.io
● https://github.com/grpc
● https://github.com/grpc-ecosystem/grpc-gateway
● https://grpc-ecosystem.github.io/grpc-gateway

gRPC or Rest, why not both?