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

Go Backends for frontends with GraphQL and gRPC

Go Backends for frontends with GraphQL and gRPC

Seiji Takahashi

July 13, 2019

More Decks by Seiji Takahashi

Other Decks in Programming


  1. BFF

  2. BFF (Web) BFF (Native) User Micro Service Article Micro Service

    Payment Micro Service Access Controled Web Browser iOS/Android
  3. Pros of BFF • Each microservices can focus on only

    their own responsibility. • Customize networking protocol for each clients. • Flexibly change the response fields.
  4. BFF Use-cases 1. API Aggregation BFF User Micro Service Article

    Micro Service Comment Micro Service Client Concat every JSON Object from internal APIs Send requests to several services concurrently Request an article entity
  5. BFF Use-cases 2. Session Management BFF User Micro Service Article

    Micro Service Comment Micro Service Client Send requests with token Get data from session store Send session_id Session Store
  6. Page request BFF Use-cases 3. SSR BFF User Micro Service

    Article Micro Service Comment Micro Service Client Concat every JSON and pass them to HTML renderer Data fetching
  7. Chunked file upload BFF Use-cases 4. File Upload BFF External

    File Storage Internal API Client Send a request with filePath Store file
  8. • When they talk about BFF… ◦ JavaScript(Node) is popular

    in most of the cases. ◦ Because it eases the communication between frontend & backend engineers. BFF in Go
  9. • Why Go? ◦ If you are a backend engineer,

    Go BFF will be a good choice from the aspect of performance. ◦ With goroutine, Go would handle the concurrent internal accesses rapidly. ▪ If you threw away REST and changed into RPC-call, you must send request multiple servers everytime. ▪ This means internal latency is critical for UX. ◦ Awesome packages for both of GraphQL and gRPC exist in Go community. BFF in Go
  10. • GraphQL is a simple query language for API which

    has the type system. • Instead of JSON request in like REST API or JSON-RPC, you will send `query` to GraphQL server. • Implementation in JavaScript is the most active. GraphQL introduction
  11. GraphQL vs gRPC • Generally, GraphQL is hotter than gRPC.

    • In a lot of countries GraphQL wins. • However, in East Asia(especially in China), gRPC has more fans.
  12. • Client-side-related words like `apollo`, `react`. • `github` is ranked

    in the top 10. Related words of each protocols • Server-side languages like `java` , `goland`, `python` and the format `protobuf`.
  13. • github.com/99designs/gqlgen-contrib ◦ package of middlewares for performance tracing ▪

    apollo-tracing ▪ opencensus • datadog ▪ opentracing ▪ prometheus GraphQL in Go
  14. GraphQL in Go • Other packages ◦ Cannot share schema

    with frontend ◦ Reflection based schema ▪ A negative effect to latency
  15. When you test GraphQL • Integration test with input &

    golden files. ◦ Record a real request and response ◦ Mock or temporarily boot internal API ◦ Iterate recorded files and check the equality with the actual responses
  16. • HTTP/2 networking • Protobuf serialization ◦ template code generation

    • Go is one of the most active languages for gRPC implementation. • Number of Github stars ◦ Go > Java > Node > Swift > PHP > Haskell
  17. Conclusion • Go is one of the best candidate language

    for BFF. • GraphQL + gRPC will empower you with schema-driven-development and low-latency protocol.