Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building better microservices with GRPC

Naren
October 07, 2018

Building better microservices with GRPC

Today, massive systems are running on microservices communicating with each other using REST APIs. HTTP is easy to get started, loosely structured and does good job in exchanging messages. But it's convenience comes with a performance trade-off, which takes us back to other optimal alternative: gRPC

Naren

October 07, 2018
Tweet

More Decks by Naren

Other Decks in Programming

Transcript

  1. REST is good… ✤ Text based protocol ✤ Human readable

    API contracts ✤ Great tooling for testing and inspection ✤ Postman, FTW. ✤ Well defined HTTP status codes
  2. …but ✤ Systems needs machine readable API contract ✤ Streaming

    is not easy ✤ Duplex communication is impossible ✤ Inconsistent patterns ✤ HTTP/1.1
  3. HTTP methods ✤ GET ✤ POST ✤ PUT ✤ DELETE

    ✤ PATCH ✤ HEAD ✤ OPTIONS
  4. 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 ?
  5. 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
  6. 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
  7. gRPC The idea of treating network operations as remote procedure

    calls goes back at least to the 1970s in early ARPANET documents.
  8. 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
  9. 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).
  10. gRPC uses HTTP/2 ✤ Minimizes header overhead ✤ Minimizes HOL

    blocking ✤ Minimizes number of connections ✤ Thereby, minimizing API latency
  11. 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
  12. 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
  13. 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
  14. gRPC uses ProtocolBuffers ✤ gRPC uses protocol buffers for seriailzation

    ✤ Protocol buffers: A structured and faster way for data serialization
  15. Protobufs are Schema Oriented message Person { required string name

    = 1; required int32 id = 2; optional string email = 3; }
  16. Protobufs are backward compatible if (version == 3) { ...

    } else if (version > 4) { if (version == 5) { ... } ... } // No ugly versioning
  17. gRPC basics ✤ Define your function ✤ Service Definition using

    protobuf ✤ Generate server stub, extend it and start the server ✤ Generate client stub
  18. Supported Languages ✤ Python ✤ C++ ✤ C# ✤ Dart

    ✤ Go ✤ Node.js ✤ Objective-C ✤ PHP ✤ Java ✤ Ruby
  19. Drawbacks ✤ No browser support ✤ Not enough documentation ✤

    No standardisation across languages ✤ Smaller community ✤ No out of the box Load balancers from cloud providers
  20. …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
  21. 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
  22. 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
  23. 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]