Building better microservices with GRPC

2357408ed1ee90461022f24f29a38cbd?s=47 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

2357408ed1ee90461022f24f29a38cbd?s=128

Naren

October 07, 2018
Tweet

Transcript

  1. 6.

    REST is good… ✤ Text based protocol ✤ Human readable

    API contracts ✤ Great tooling for testing and inspection ✤ Postman, FTW. ✤ Well defined HTTP status codes
  2. 7.
  3. 8.

    …but ✤ Systems needs machine readable API contract ✤ Streaming

    is not easy ✤ Duplex communication is impossible ✤ Inconsistent patterns ✤ HTTP/1.1
  4. 10.

    HTTP methods ✤ GET ✤ POST ✤ PUT ✤ DELETE

    ✤ PATCH ✤ HEAD ✤ OPTIONS
  5. 13.
  6. 17.

    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 ?
  7. 18.

    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
  8. 21.

    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
  9. 24.

    gRPC The idea of treating network operations as remote procedure

    calls goes back at least to the 1970s in early ARPANET documents.
  10. 25.

    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
  11. 26.

    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).
  12. 30.

    gRPC uses HTTP/2 ✤ Minimizes header overhead ✤ Minimizes HOL

    blocking ✤ Minimizes number of connections ✤ Thereby, minimizing API latency
  13. 31.

    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
  14. 32.

    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
  15. 33.

    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
  16. 34.

    gRPC uses ProtocolBuffers ✤ gRPC uses protocol buffers for seriailzation

    ✤ Protocol buffers: A structured and faster way for data serialization
  17. 35.

    Protobufs are Schema Oriented message Person { required string name

    = 1; required int32 id = 2; optional string email = 3; }
  18. 36.

    Protobufs are backward compatible if (version == 3) { ...

    } else if (version > 4) { if (version == 5) { ... } ... } // No ugly versioning
  19. 39.

    gRPC basics ✤ Define your function ✤ Service Definition using

    protobuf ✤ Generate server stub, extend it and start the server ✤ Generate client stub
  20. 41.

    Supported Languages ✤ Python ✤ C++ ✤ C# ✤ Dart

    ✤ Go ✤ Node.js ✤ Objective-C ✤ PHP ✤ Java ✤ Ruby
  21. 42.
  22. 43.

    Drawbacks ✤ No browser support ✤ Not enough documentation ✤

    No standardisation across languages ✤ Smaller community ✤ No out of the box Load balancers from cloud providers
  23. 45.

    …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
  24. 46.

    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
  25. 47.

    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
  26. 48.

    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]