Slide 1

Slide 1 text

Building better microservices with GRPC Pycon India 2018

Slide 2

Slide 2 text

Narendran @DudeWhoCode 
 www.dudewho.codes Software Consultant, Tarka Labs

Slide 3

Slide 3 text

Netflix microservices traffic diagram [https://www.honeycomb.io/microservices/]

Slide 4

Slide 4 text

REST APIs

Slide 5

Slide 5 text

What’s wrong with REST ?

Slide 6

Slide 6 text

REST is good… ✤ Text based protocol ✤ Human readable API contracts ✤ Great tooling for testing and inspection ✤ Postman, FTW. ✤ Well defined HTTP status codes

Slide 7

Slide 7 text

…but

Slide 8

Slide 8 text

…but ✤ Systems needs machine readable API contract ✤ Streaming is not easy ✤ Duplex communication is impossible ✤ Inconsistent patterns ✤ HTTP/1.1

Slide 9

Slide 9 text

Inconsistent patterns and payload ?

Slide 10

Slide 10 text

HTTP methods ✤ GET ✤ POST ✤ PUT ✤ DELETE ✤ PATCH ✤ HEAD ✤ OPTIONS

Slide 11

Slide 11 text

Q: Why isn’t the demo working ?

Slide 12

Slide 12 text

Q: Why isn’t the demo working ? A: The API broke

Slide 13

Slide 13 text

HTTP/1.1

Slide 14

Slide 14 text

1990 1995 2005 2005 2010 2015 2018 HTTP/0.9 HTTP/1.0 HTTP/1.1 HTTP/2

Slide 15

Slide 15 text

HTTP/1.1 is slow. Why ?

Slide 16

Slide 16 text

Connection overload 
 New TCP connection for every HTTP request HTTP/1.1 is slow. Why ?

Slide 17

Slide 17 text

Connection overload 
 New TCP connection for every HTTP request Header overload 
 Uncompressed, repeating plain text headers for every http request HTTP/1.1 is slow. Why ?

Slide 18

Slide 18 text

HTTP/1.1 is slow. Why ? Connection overload 
 New TCP connection for every HTTP request Header overload 
 Uncompressed, repeating plain text headers for every http request HOL blocking 
 A line of packets is held up by the first packet

Slide 19

Slide 19 text

REST on HTTP/1.1

Slide 20

Slide 20 text

What is gRPC ?

Slide 21

Slide 21 text

gRPC is a protocol where a client application can directly call methods on a server application on a different machine as if it was a local object, making it easier for you to create distributed applications and services. www.grpc.io

Slide 22

Slide 22 text

Companies that uses GRPC in production

Slide 23

Slide 23 text

Such Innovashun Much Scale So New Wow So Distributed Developer Doge

Slide 24

Slide 24 text

gRPC The idea of treating network operations as remote procedure calls goes back at least to the 1970s in early ARPANET documents.

Slide 25

Slide 25 text

gRPC The idea of treating network operations as remote procedure calls goes back at least to the 1970s in early ARPANET documents. First practical implementation was called Lupine at Xerox PARC. One of the first business uses of RPC was by Xerox under the name "Courier" in 1981

Slide 26

Slide 26 text

gRPC The idea of treating network operations as remote procedure calls goes back at least to the 1970s in early ARPANET documents. First practical implementation was called Lupine at Xerox PARC. One of the first business uses of RPC was by Xerox under the name "Courier" in 1981 The first popular implementation of RPC on Unix was Sun's RPC (now called ONC RPC), used as the basis for Network File System (NFS).

Slide 27

Slide 27 text

How RPC works ?

Slide 28

Slide 28 text

www.grpc.io

Slide 29

Slide 29 text

Why gRPC(is faster) ?

Slide 30

Slide 30 text

gRPC uses HTTP/2 ✤ Minimizes header overhead ✤ Minimizes HOL blocking ✤ Minimizes number of connections ✤ Thereby, minimizing API latency

Slide 31

Slide 31 text

Headers in HTTP/1.1 [plain text] :method GET :scheme Https :host tarkalabs.com :path /team user-agent Chrome auth_token 3286354f34s3546 :method GET :scheme Https :host tarkalabs.com :path /projects user-agent Chrome auth_token 3286354f34s3546 Request #1 Request #2

Slide 32

Slide 32 text

Headers in HTTP/2 [HPAC] :method GET :scheme Https :host tarkalabs.com :path /team user-agent Chrome auth_token 3286354f34s3546 Index of repeating fields + :path /projects Request #1 Request #2

Slide 33

Slide 33 text

Optimized connections https://docs.google.com/presentation/d/1r7QXGYOLCh4fcUq0jDdDwKJWNqWK1o4xMtYpKZCJYjM/present?slide=id.p19 ✤ All the data is sent as frames over single TCP connection ✤ Duplex communication happened between server and client in a connection

Slide 34

Slide 34 text

gRPC uses ProtocolBuffers ✤ gRPC uses protocol buffers for seriailzation ✤ Protocol buffers: A structured and faster way for data serialization

Slide 35

Slide 35 text

Protobufs are Schema Oriented message Person { required string name = 1; required int32 id = 2; optional string email = 3; }

Slide 36

Slide 36 text

Protobufs are backward compatible if (version == 3) { ... } else if (version > 4) { if (version == 5) { ... } ... } // No ugly versioning

Slide 37

Slide 37 text

Protobufs are faster https://auth0.com/blog/beating-json-performance-with-protobuf/

Slide 38

Slide 38 text

Even more faster between microservices https://auth0.com/blog/beating-json-performance-with-protobuf/

Slide 39

Slide 39 text

gRPC basics ✤ Define your function ✤ Service Definition using protobuf ✤ Generate server stub, extend it and start the server ✤ Generate client stub

Slide 40

Slide 40 text

Connections ✤ Unary ✤ Response stream ✤ Request stream ✤ Bi-directional stream Server Client

Slide 41

Slide 41 text

Supported Languages ✤ Python ✤ C++ ✤ C# ✤ Dart ✤ Go ✤ Node.js ✤ Objective-C ✤ PHP ✤ Java ✤ Ruby

Slide 42

Slide 42 text

Demo

Slide 43

Slide 43 text

Drawbacks ✤ No browser support ✤ Not enough documentation ✤ No standardisation across languages ✤ Smaller community ✤ No out of the box Load balancers from cloud providers

Slide 44

Slide 44 text

gRPC or REST …?

Slide 45

Slide 45 text

…depends ✤ Choose gRPC if for server to server communication and services that follows same development cycle (mircoservices) ✤ Choose REST for public facing services or the service with interacts with your browser

Slide 46

Slide 46 text

There is no such thing as best solution. There can only be systems that are more appropriate in a particular set of circumstances. – Ancient Chinese Saying https://en.wikipedia.org/wiki/Wise_old_man

Slide 47

Slide 47 text

There is no such thing as best solution. There can only be systems that are more appropriate in a particular set of circumstances. – The Pragmatic Programmer

Slide 48

Slide 48 text

gRPC Resources ✤ gRPC python Greeter Quickstart [https://grpc.io/docs/ quickstart/python.html] ✤ gRPC python docs [https://grpc.io/docs/tutorials/basic/ python.html] ✤ Protocol Buffers Introduction [https://developers.google.com/ protocol-buffers/docs/overview] ✤ Protocol Buffers python reference [https:// developers.google.com/protocol-buffers/docs/reference/ python-generated]

Slide 49

Slide 49 text

Naren
 @DudeWhoCode www.dudewho.codes/talks