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

From PHP and REST to gRPC and Go Full

Yuriy Tuz
January 29, 2020

From PHP and REST to gRPC and Go Full

Journey from PHP and REST to gRPC and Go.

Yuriy Tuz

January 29, 2020
Tweet

More Decks by Yuriy Tuz

Other Decks in Programming

Transcript

  1. From PHP and REST To gRPC and Go Winnipeg, January

    29 2020 Yuriy Tuz Bold Commerce @yuriytuz
  2. Agenda • Personal experience • Past life • Microservices intro

    • Why gRPC • gRPC in GO ◦ Simple implementation
  3. REST (HTTP + JSON) • Not a bad solution •

    Service to service communications • (De)Serialization performance • JSON is not type safe in PHP • Text protocol
  4. Problem • Technology is driving demand • Languages and platforms

    ◦ Different dev experience • Software architecture is evolving ◦ Microservices and containers ◦ CI/CD
  5. How/why? • Why split? • Why Go? • OG mayhem

    to the rescue • Why not REST (HTTP+JSON)? ◦ No formal contract ◦ Hard to implement correctly
  6. Implementation/Findings • PHP client not fun ◦ Working POC •

    First experience with Go • Refactoring • Benchmarks ◦ ~100x faster
  7. Microservices https://microservices.io • Highly maintainable and testable • Loosely coupled

    • Independently deployable • Organized around business capabilities • Owned by a small team
  8. Organizations which design systems … are constrained to produce designs

    which are copies of the communication structures of these organizations” Conway, Melvin E. (1968)
  9. Stack • Protocol buffers + gRPC ◦ Thanks Google •

    Tim Burks ◦ API’s at Google ◦ “Stop writing interface code for your APIs by hand”
  10. Serialization Mechanism • 0a 05 68 65 6c 6c 6f

    • 68 65 6c 6c 6f -> hello • 05 -> length of “hello” • 0a is “0000 1010”, so • field_number = 1 • wire_type = 2 (length delimited)
  11. Interface Description Language message Person { int64 id = 1;

    string shop_identifier = 2; reserved 3; string platform_type = 4; string first_name = 5; string last_name = 6; string email = 7; repeated Address addresses = 13; repeated Label labels = 14; } Interface for your API
  12. gRPC • Open source messaging system by Google that can

    run anywhere • Low latency • Highly scalable • Great for distributed systems • Accurate, efficient, language independent
  13. Let’s generate • protoc --go_out=:. ./echo.proto ◦ Default protobuf generation

    • protoc --go_out=plugins=grpc:. ./echo.proto ◦ Client and Server interface ◦ gRPC
  14. gRPC middleware • Server or Client • Few exist in

    gRPC ecosystem ◦ Prometheus ◦ Open tracing • https://github.com/grpc-ecosystem/go-grpc-middleware
  15. Directory structure • Client & server implementation • echo.pb.go ◦

    gRPC generated • echo.proto ◦ API definition
  16. gRPC gateway • Have .proto file? ◦ Get free REST

    API (in few simple steps) ◦ Make sure you go get plugin ◦ protoc --go_out=plugins=grpc:. --grpc-gateway_out=logtostderr=true,grpc_api_configuration=./router.yaml :. ./echo.proto
  17. Final thoughts The good • Code generation ◦ Less error

    prone • API spec ◦ OpenAPI • gRPC ◦ HTTP2 + language support • Type safety • Plugins • Dev ramp up speed The bad • Code generation • Model conversions ◦ Nested messages • Hard/scary to get started ◦ Documentation • API versioning • gRPC Gateway silent failure
  18. TL;DR • Use gRPC + Go ◦ Microservices • Generate

    code • Implement stubs • Profit
  19. Credits/Useful links • Gopher art by Ashley McNamara ◦ https://github.com/ashleymcnamara/gophers

    • gRPC ◦ https://grpc.io/ • Google API Design Guide ◦ https://cloud.google.com/apis/design/ • Protobuf Style Guide ◦ https://developers.google.com/protocol-buffers/docs/style • rpcurl and grpcui ◦ https://github.com/fullstorydev • gRPC gateway ◦ https://github.com/grpc-ecosystem/grpc-gateway