Lightning Fast,
Self-Documenting
APIs
An overview of HTTP/2, gRPC and its advantages
Presented by: Mohammed Ali
○ What is HTTP/2?
○ Advantages of HTTP/2
○ What is gRPC?
○ Advantages of gRPC
○ Types of gRPC
○ Building a gRPC service
○ Demo of gRPC in a polyglot
microservice architecture
○ Overview
Contents
HTTP/2
Source: http://www.http2demo.io/
What is HTTP/2? ● New version of HTTP.
● Derived from SPDY.
● Primarily focused on performance.
● Same core semantics as HTTP/1.1.
● Results in better UX and faster client
applications.
Advantages of HTTP/2
Fully multiplexed Binary
Header Compression Server Push
Bidirectional Streaming
gRPC Remote
Procedure Calls
gRPC
gRPC Remote Procedure Calls
Uses HTTP/2
Based on protocol
buffers
Fast & Efficient
What is a Remote Procedure Call?
○ invocation of a procedure / method from the client to perform an action
on the server
What is gRPC?
○ an open source RPC framework developed by google, that uses
protobufs
Advantages of gRPC
Multi Language and cross platform Protocol Buffers
Smaller and Faster Great support
DevX + Easy Setup
Types of RPC
Unary RPC
1. Login request.
2. Getting details of a single resource, education details for employeeNumber = 10
1
Android
Client
Go Server
Client makes a single request
Server responds with a single message
Types of RPC
Client Streaming RPC
1. Streaming analytics data.
2. Syncing GPS data.
2
iOS Client
Python
Server
Stream of messages from client
Single Message from server after all
messages from the client are done
Types of RPC
Server Streaming RPC
1. Fetching a list of items that need to displayed on the client
2. Propagation of new data
3
iOS Client
Node.js
Server
Single message from client
Streams of messages from server
Types of RPC
Bidirectional Streaming RPC
1. Syncing events between services.
2. Chat application
4
Go Client
Java
Server
Streams of messages from client
Streams of messages from server
Building a gRPC service 101
Defining a .proto file
message Movie {
int64 id = 1;
string movie_name = 2;
string genre = 3;
string client_id = 4;
}
a. Define the type of message that you want to send.
b. Tags should not be overridden when writing new features
1
Building a gRPC service 101
Design APIs2
a. Designing and writing your protocol buffer is one of the most important tasks in
gRPC
b. For designing your API right, your protocol buffer needs to be written very well.
service MovieLibrary {
// Bidirectional streaming rpc to allow user to get and add movies to collection
rpc getAndUpdateMovieLibrary (stream Movie) returns (stream Movie) {}
// Client side streaming rpc to allow user to fetch movies from the server
rpc listMovies (ListMoviesRequest) returns (stream Movie) {}
}
Building a gRPC service 101
Generate server and client stubs using protocol buffer compiler3
./gradlew build
python -m grpc_tools.protoc -I=src/main/proto/ --python_out=clients/python-client/
--grpc_python_out=clients/python-client/ src/main/proto/movie.proto
Building a gRPC service 101
Extend and implement the interfaces created in this .proto in the
server class
public class MovieService extends MovieLibraryGrpc.MovieLibraryImplBase {
@Override
public StreamObserver<Movie> getAndUpdateMovieLibrary(StreamObserver<Movie> responseObserver) {
// handle returning new stream and updating movie library here
}
@Override
public void listMovies(ListMoviesRequest request, StreamObserver<Movie> responseObserver) {
// handle returning new stream here
}
}
4
gRPC in a polyglot micro-service architecture
Java
Client
NodeJS
Client
Python
Client
Java
Server
○ HTTP/2 and its advantages.
○ gRPC, its types and advantages.
○ Building a gRPC service
○ gRPC in a polyglot architecture
Key Takeaways
The code for the demo can be found at:
https://github.com/alichherawalla/grpc-movie-client-server

APIdays Helsinki 2019 - gRPC: Lightning Fast, Self-Documenting APIs with Mohammed Ali Chherawalla, Ninja Van

  • 1.
    Lightning Fast, Self-Documenting APIs An overviewof HTTP/2, gRPC and its advantages Presented by: Mohammed Ali
  • 2.
    ○ What isHTTP/2? ○ Advantages of HTTP/2 ○ What is gRPC? ○ Advantages of gRPC ○ Types of gRPC ○ Building a gRPC service ○ Demo of gRPC in a polyglot microservice architecture ○ Overview Contents
  • 3.
  • 4.
    What is HTTP/2?● New version of HTTP. ● Derived from SPDY. ● Primarily focused on performance. ● Same core semantics as HTTP/1.1. ● Results in better UX and faster client applications.
  • 5.
    Advantages of HTTP/2 Fullymultiplexed Binary Header Compression Server Push Bidirectional Streaming
  • 6.
  • 7.
    gRPC Remote ProcedureCalls Uses HTTP/2 Based on protocol buffers Fast & Efficient What is a Remote Procedure Call? ○ invocation of a procedure / method from the client to perform an action on the server What is gRPC? ○ an open source RPC framework developed by google, that uses protobufs
  • 8.
    Advantages of gRPC MultiLanguage and cross platform Protocol Buffers Smaller and Faster Great support DevX + Easy Setup
  • 9.
    Types of RPC UnaryRPC 1. Login request. 2. Getting details of a single resource, education details for employeeNumber = 10 1 Android Client Go Server Client makes a single request Server responds with a single message
  • 10.
    Types of RPC ClientStreaming RPC 1. Streaming analytics data. 2. Syncing GPS data. 2 iOS Client Python Server Stream of messages from client Single Message from server after all messages from the client are done
  • 11.
    Types of RPC ServerStreaming RPC 1. Fetching a list of items that need to displayed on the client 2. Propagation of new data 3 iOS Client Node.js Server Single message from client Streams of messages from server
  • 12.
    Types of RPC BidirectionalStreaming RPC 1. Syncing events between services. 2. Chat application 4 Go Client Java Server Streams of messages from client Streams of messages from server
  • 13.
    Building a gRPCservice 101 Defining a .proto file message Movie { int64 id = 1; string movie_name = 2; string genre = 3; string client_id = 4; } a. Define the type of message that you want to send. b. Tags should not be overridden when writing new features 1
  • 14.
    Building a gRPCservice 101 Design APIs2 a. Designing and writing your protocol buffer is one of the most important tasks in gRPC b. For designing your API right, your protocol buffer needs to be written very well. service MovieLibrary { // Bidirectional streaming rpc to allow user to get and add movies to collection rpc getAndUpdateMovieLibrary (stream Movie) returns (stream Movie) {} // Client side streaming rpc to allow user to fetch movies from the server rpc listMovies (ListMoviesRequest) returns (stream Movie) {} }
  • 15.
    Building a gRPCservice 101 Generate server and client stubs using protocol buffer compiler3 ./gradlew build python -m grpc_tools.protoc -I=src/main/proto/ --python_out=clients/python-client/ --grpc_python_out=clients/python-client/ src/main/proto/movie.proto
  • 16.
    Building a gRPCservice 101 Extend and implement the interfaces created in this .proto in the server class public class MovieService extends MovieLibraryGrpc.MovieLibraryImplBase { @Override public StreamObserver<Movie> getAndUpdateMovieLibrary(StreamObserver<Movie> responseObserver) { // handle returning new stream and updating movie library here } @Override public void listMovies(ListMoviesRequest request, StreamObserver<Movie> responseObserver) { // handle returning new stream here } } 4
  • 17.
    gRPC in apolyglot micro-service architecture Java Client NodeJS Client Python Client Java Server
  • 18.
    ○ HTTP/2 andits advantages. ○ gRPC, its types and advantages. ○ Building a gRPC service ○ gRPC in a polyglot architecture Key Takeaways The code for the demo can be found at: https://github.com/alichherawalla/grpc-movie-client-server