Slide 1

Slide 1 text

Go Backends for Frontends with and @__timakin__

Slide 2

Slide 2 text

Seiji Takahashi Software Engineer Gunosy Inc. Tokyo, Japan.

Slide 3

Slide 3 text

BFF

Slide 4

Slide 4 text

Backends For Frontends

Slide 5

Slide 5 text

BFF (Web) BFF (Native) User Micro Service Article Micro Service Payment Micro Service Access Controled Web Browser iOS/Android

Slide 6

Slide 6 text

Pros of BFF ● Each microservices can focus on only their own responsibility. ● Customize networking protocol for each clients. ● Flexibly change the response fields.

Slide 7

Slide 7 text

BFF Use-cases 1. API Aggregation 2. Session Management 3. SSR 4. File Upload 5. WebSocket

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Chunked file upload BFF Use-cases 4. File Upload BFF External File Storage Internal API Client Send a request with filePath Store file

Slide 12

Slide 12 text

Connect with WS BFF Use-cases 5. WebSocket/Polling BFF Message Queue Internal API Client Pub/Sub

Slide 13

Slide 13 text

Micro Service Today’s CaseStudy BFF Micro Service Micro Service Web Client

Slide 14

Slide 14 text

● 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

Slide 15

Slide 15 text

● 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

Slide 16

Slide 16 text

GraphQL

Slide 17

Slide 17 text

● 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

Slide 18

Slide 18 text

Client

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

● 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`.

Slide 21

Slide 21 text

● In-browser playground to explore GraphQL calls. GraphiQL

Slide 22

Slide 22 text

● Packages ○ graphql-go/graphql ○ graphql-go/relay ○ samsarahq/thunder ○ 99designs/gqlgen GraphQL in Go

Slide 23

Slide 23 text

● Packages ○ graphql-go/graphql ○ graphql-go/relay ○ samsarahq/thunder ○ 99designs/gqlgen GraphQL in Go

Slide 24

Slide 24 text

● github.com/99designs/gqlgen ○ Active development ○ Type-Safe ○ Schema Driven ○ Custom middleware appendable GraphQL in Go

Slide 25

Slide 25 text

● github.com/99designs/gqlgen ○ Active development ○ Type-Safe ○ Schema Driven ○ Custom middleware appendable GraphQL in Go

Slide 26

Slide 26 text

● github.com/99designs/gqlgen-contrib ○ package of middlewares for performance tracing ■ apollo-tracing ■ opencensus ● datadog ■ opentracing ■ prometheus GraphQL in Go

Slide 27

Slide 27 text

GraphQL in Go ● Other packages ○ Cannot share schema with frontend ○ Reflection based schema ■ A negative effect to latency

Slide 28

Slide 28 text

schema.graphql Prepare schemas Instead of auto-generated code, you can use defined structs. gqlgen.yml

Slide 29

Slide 29 text

$ gqlgen Code generate

Slide 30

Slide 30 text

The implementation inside functions are not automatically generated. Write your own code. grpc client

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

gRPC

Slide 33

Slide 33 text

● 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

Slide 34

Slide 34 text

● github.com/golang/protobuf/proto ● github.com/golang/protobuf/protoc-gen-go ● google.golang.org/grpc Required packages ● github.com/ktr0731/evans ● github.com/uber/prototool Optional tools

Slide 35

Slide 35 text

blog.proto Schema

Slide 36

Slide 36 text

uber/prototool strictly checks and formats *.proto Linter

Slide 37

Slide 37 text

Linter Define the rules on prototool.yaml Run commands

Slide 38

Slide 38 text

Code Generation github.com/golang/protobuf/protoc-gen-go

Slide 39

Slide 39 text

Client

Slide 40

Slide 40 text

Client send a request to internal gRPC API

Slide 41

Slide 41 text

Server

Slide 42

Slide 42 text

Server

Slide 43

Slide 43 text

Server Call the logic implemented by yourself e.g. DB access, external API-call

Slide 44

Slide 44 text

After implementation ktr0731/evans helps you to explore your gRPC server locally

Slide 45

Slide 45 text

Conclusion ● Go is one of the best candidate language for BFF. ● GraphQL + gRPC will empower you with schema-driven-development and low-latency protocol.

Slide 46

Slide 46 text

Thank you for listening