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

Codemotion Spain Meetup -Microservices: gRPC vs...

Codemotion Spain Meetup -Microservices: gRPC vs Rest

Construir APIs para microservicios puede ser un reto. Las nuevas tecnologías open source para el stack de APIs son HTTP/2, Profobuf y gRPC. Sin embargo, migrar a estas nuevas tecnologías es un reto, especialmente en el mundo de los microservicios para entender cómo funcionan y qué ganamos con dichas tecnologías. En esta charla mostraré cómo puedes construir APIs gRPC sobre HTTP/2 mientras simultáneamente sirves JSON/REST sobre HTTP/1.1 en el mismo puerto, con los benchmarks de dicha configuración. Además, de los costes que tiene migrar a estas nuevas tecnologías.

Esteban Dorado Roldan

November 07, 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. 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 • Secure Connection by default
  5. HTTP/2 - Multiplexed • multiple reqs/resps can be in-flight over

    one conn • avoid multiple TCP conns to make parallel requests
  6. • '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
  7. Protocol Buffers (a.k.a. protobuf) • Mechanism for serializing structured data

    • Interface Definition Language (IDL) • Binary, compact, fast • Versioned • strongly typed
  8. Protobuf workflow Protobuf definitions Protoc compiler Python Golang Java gRPC

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

    via Github releases and Install Go implementation of gRPC: $ go get -u google.golang.org/grpc • Build protos: $ protoc --go_out=plugins=grpc:. service.proto
  10. Example - Generate Server and Client • Install Python of

    gRPC: $ pip install grpcio • Build protos: $ python -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. ./api.proto
  11. gRCP Streaming gRCP supports streaming where client can stream data

    to server, server to client and bi-directionally
  12. 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
  13. Secure gRCP Service The gRPC framework makes it easy to

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

    from waiting an excessively long time for responses
  15. 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
  16. Backwards (HTTP) Compatibility • Run a reverse proxy JSON API

    in front of gRPC server • Generate via github.com/gengo/grpc-gateway
  17. Command generate gateway • Run a reverse proxy JSON API

    in front of gRPC server • Generate via github.com/gengo/grpc-gateway • Build steps: 1. Regenerate protos: $ protoc -I . --go_out=plugins=grpc:${GOPATH}/src proto/service.proto 2. Generate reverse-proxy: $ protoc -I . -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_part y/googleapis --go_out=plugins=grpc:${GOPATH}/src --grpc-gateway_out=logtostderr=true:${GOPATH}/src proto/service.proto
  18. gRPC/REST API Swagger document • Generate a swagger.json • Build

    command: $ protoc -I . -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/th ird_party/googleapis --go_out=plugins=grpc:${GOPATH}/src --grpc-gateway_out=logtostderr=true:${GOPATH}/src --swagger_out=logtostderr=true:. proto/service.proto
  19. 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
  20. 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 • Add monitoring and metrics using interceptors (e.g. Prometheus)
  21. Q&A