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. Building better microservices with
    GRPC
    Pycon India 2018

    View Slide

  2. Narendran
    @DudeWhoCode

    www.dudewho.codes
    Software
    Consultant,
    Tarka Labs

    View Slide

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

    View Slide

  4. REST APIs

    View Slide

  5. What’s wrong with REST ?

    View Slide

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

    View Slide

  7. …but

    View Slide

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

    View Slide

  9. Inconsistent patterns and payload ?

    View Slide

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

    View Slide

  11. Q: Why isn’t the demo working ?

    View Slide

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

    View Slide

  13. HTTP/1.1

    View Slide

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

    View Slide

  15. HTTP/1.1 is slow. Why ?

    View Slide

  16. Connection overload

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

    View Slide

  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 ?

    View Slide

  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

    View Slide

  19. REST on HTTP/1.1

    View Slide

  20. What is gRPC ?

    View Slide

  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

    View Slide

  22. Companies that uses GRPC in production

    View Slide

  23. Such Innovashun
    Much Scale
    So New
    Wow
    So Distributed
    Developer Doge

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  27. How RPC works ?

    View Slide

  28. www.grpc.io

    View Slide

  29. Why gRPC(is faster) ?

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  42. Demo

    View Slide

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

    View Slide

  44. gRPC or REST …?

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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]

    View Slide

  49. Naren

    @DudeWhoCode
    www.dudewho.codes/talks

    View Slide