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

Microservices: grpc vs rest

Microservices: grpc vs rest

Charla impartida en la TLP Innova 2018: https://tlp-tenerife.com/agenda/evento/grpc-rest/

Esteban Dorado Roldan

July 20, 2018
Tweet

More Decks by Esteban Dorado Roldan

Other Decks in Programming

Transcript

  1. Scenario - A currency service Currency Service Currency Lookup Mobile

    Android Mobile iOS Backend Processes Desktop Report tool Website Backend Android Java/Kotlin Objective-C Java, Python, Scala C#, Java NodeJS
  2. The Problem • Multiple business systems • Bandwidth-constrained access (mobile)

    • Real-time data access • Different platforms and languages • Need to feature-scale with versioning • Maintain backward compatibility Mobile Android Mobile iOS Backend Processes Desktop Report tool Website Backend Android Java/Kotlin Objective-C Java, Python, Scala C#, Java NodeJS
  3. RESTful API non-RESTful API GET /user/69 { “Id” : 123456,

    “name” : ”Esteban”, . . . } GET /last_user?page=15 { “items” : [ . . . ] . . . }
  4. REST • HTTP-JSON-REST APIs/Microservices • Transport: HTTP/1.1 • Wire format:

    JSON (Text) • Service definition: ◦ REST, Swagger, API BluePrint ◦ JSON Schema
  5. What is gRPC? • Google open sourced in Feb 2015

    • Transport: HTTP/2 • Wire format: Protocol Buffers v3 (Binary) • Service definition: Protocol Buffers IDL • Libraries in ~10 languages (native C, Go, Java) • Microservices framework • Part of Cloud Native Computing Foundation cncf.io • Pre-defined error status codes
  6. Go uses a native Go driver Java uses a native

    Java driver Python, Ruby, NodeJS… uses a C driver for gRPC
  7. Comparative solutions Idiomatic Client/Server Curl Friendly Framework Serialization Efficient Predictable

    SOAP via Frameworks No By language XML No Somewhat RPC over HTTP/JSON No Yes Custom None No No Custom REST No Yes Custom * No No REST via Frameworks Yes Swagger, others Likely JSON No Somewhat gRPC Yes No Included Binary Yes Yes
  8. JSON vs Protobuf Plaintext vs binary Human readable vs machine

    readable Repetitive vs compressed Fast (de)serialization vs faster (de)serialization* Everything supports it vs limited support
  9. HTTP 1.1 vs HTTP/2 Plaintext vs binary No pipelining vs

    native pipelining New connection per request vs persistent TCP connection Repetitive vs compressed Polling vs streaming Non-secure by default vs TLS by default Everything supports it vs limited support
  10. HTTP/2 - Multiplexed • multiple reqs/resps can be in-flight over

    one conn • avoid multiple TCP conns to make parallel requests
  11. • 'independent, bidirectional sequence of frames exchanged between the client

    and server within an HTTP/2 connection' • beyond request/response • effectively supersedes 'websockets' HTTP/2 - Streams
  12. • 'independent, bidirectional sequence of frames exchanged between the client

    and server within an HTTP/2 connection' • beyond request/response • effectively supersedes 'websockets' HTTP/2 - Streams
  13. Protocol Buffers (a.k.a. protobuf) • Mechanism for serializing structured data

    • Interface Definition Language (IDL) • Binary, compact, fast • Versioned
  14. Protocol Buffers • strongly typed. .proto type -> Go type:

    ◦ float -> float32 ◦ bool -> bool ◦ string -> string • defaults, enums, nested types, Any, OneOf, maps • proto3 easily transformed to JSON (for debugging!) • https://developers.google.com/protocol-buffers/
  15. Protobuf workflow Protobuf definitions Protoc compiler Python Golang Java gRPC

    server gRPC client 1 - Define 2 - Compile 3 - Implement Code generated protoc --go_out=plugins=grpc
  16. Example - Generate Server and Client • Download compiler protoc

    via Github releases • Install Go implementation of gRPC: • go get -u google.golang.org/grpc • protoc --go_out=plugins=grpc:. service.proto
  17. gRCP Streaming gRCP supports streaming where client can stream data

    to server, server to client and bi-directionally
  18. Streaming • Large request can be streamed to server •

    Server can stream large response to client • Strategy to keep client or server from running out of memory • Easy to declare in the IDL • Framework provides API support to use stream
  19. Secure gRCP Service The gRPC framework makes it easy to

    add secure TLS transport between client and servers.
  20. Request Timeout Use gRPC request timeouts to prevent client make

    from waiting an excessively long time for responses
  21. gRPC errors • All errors have an associated numeric status

    code • Errors can contain complex objects • Wrap your server errors in status.Error() • Server-side anti-pattern: don’t return naked errors
  22. Backwards Compatibility • Run a reverse proxy JSON API in

    front of gRPC server • Generate via github.com/gengo/grpc-gateway
  23. Additional gRPC Features • Extensible middleware API using interceptors and

    tap handlers • Intercept requests to implement retry/backoff logic • Add tracing using interceptors • Add HTTP stream taps to implement rate limits • Support for pluggable authorization • Add monitoring and metrics using interceptors (e.g. Prometheus)
  24. Rate limits options • Using interceptors • Limit numbers of

    connections for unary and stream calls • Limit numbers of connections for grpc-gateway • Ability to limit message size for both request and response
  25. Load balancing options • Proxy (support limited) • Client side?

    (Custom pool connections) • Lookaside Load Balancing (Custom) • Service mesh ◦ Use built-in LB with Istio, or Envoy https://grpc.io/blog/loadbalancing
  26. Q&A