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

From PHP and REST to gRPC and Go

Yuriy Tuz
December 04, 2019

From PHP and REST to gRPC and Go

Yuriy Tuz

December 04, 2019
Tweet

More Decks by Yuriy Tuz

Other Decks in Programming

Transcript

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

    4 2019 Yuriy Tuz Bold Commerce @yuriytuz
  2. REST (HTTP + JSON) • Not a bad solution •

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

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

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

    First experience with Go • Refactoring • Benchmarks ◦ ~100x faster
  6. 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)
  7. gRPC • Open source messaging system by Google that can

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

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

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

    gRPC generated • echo.proto ◦ API definition
  11. Final thoughts The good • Code generation ◦ Less error

    prone • API spec • 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
  12. TL;DR • Use gRPC + Go • Generate code •

    Implement stubs • Profit
  13. 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