Slide 1

Slide 1 text

Go + microservices = Go kit @peterbourgon · Cloud Native Con 2017 · Berlin

Slide 2

Slide 2 text

Go is a good language
 for microservices I want to convince you that and maybe Go kit will help

Slide 3

Slide 3 text

What is Go?

Slide 4

Slide 4 text

History Date: Sun, 23 Sep 2007 23:33:41 -0700
 From: "Robert Griesemer" 
 To: "Rob 'Commander' Pike" , ken@google.com
 Subject: prog lang discussion
 ...
 *** General:
 Starting point: C, fix some obvious flaws, remove crud, add a few missing features
 - no includes, instead: import
 - no macros (do we need something instead?)
 - ideally only one file instead of a .h and .c file, module interface 
 should be extracted automatically
 - statements: like in C, though should fix 'switch' statement
 - expressions: like in C, though with caveats (do we need ',' expressions?)
 - essentially strongly typed, but probably w/ support for runtime types
 - want arrays with bounds checking on always (except perhaps in 'unsafe mode'-see section on GC)
 - mechanism to hook up GC (I think that most code can live w/ GC, but for a true systems
 programming language there should be mode w/ full control over memory allocation)
 - support for interfaces (differentiate between concrete, or implementation types, and abstract,
 or interface types)
 - support for nested and anonymous functions/closures (don't pay if not used)
 - a simple compiler should be able to generate decent code
 - the various language mechanisms should result in predictable code
 ...

Slide 5

Slide 5 text

Go properties • Statically typed • Compiled — fast! • Native binaries • Garbage collected • Looks like C • Big standard library • Baked-in concurrency

Slide 6

Slide 6 text

Go IMO • A breath of fresh air from “kitchen sink” languages — cf. Scala • Simple, orthogonal features that aren't surprising — cf. Node • Efficient by default — cf. Python, Ruby • Predictable runtime behavior — cf. JVM languages • Familiar heritage, syntax, and paradigm — cf. Haskell, Elixir

Slide 7

Slide 7 text

“Simple”

Slide 8

Slide 8 text

“No magic”

Slide 9

Slide 9 text

https://talks.golang.org/2012/splash.article

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Mindshare

Slide 12

Slide 12 text

Mindshare

Slide 13

Slide 13 text

Microservices

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Microservices solve organizational problems
 ~
 Microservices cause
 technical problems

Slide 16

Slide 16 text

Problems solved • Team is too large to work effectively on shared codebase • Teams are blocked on other teams, can't make progress • Communication overhead becomes gigantic • Product velocity stalled

Slide 17

Slide 17 text

Problems caused • Need well-defined business domains for stable APIs • No more shared DB — distributed transactions? • Testing becomes really hard • Require dev/ops culture: devs deploy & operate their work • Job (service) scheduling — manually works, for a while...

Slide 18

Slide 18 text

Problems caused • Addressability i.e. service discovery • Monitoring and instrumentation — tail -f? Nagios & New Relic? Ha! • Distributed tracing? • Build pipelines?? • Security???

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Concerns for a single service, Sean Treadway, SoundCloud

Slide 24

Slide 24 text

Enter Go kit

Slide 25

Slide 25 text

Initial goals • A standard library for microservices • Something like Finagle for Go • Adapters, bindings, etc. to common infrastructure components • Play nice in your existing, heterogeneous infrastructure • Structure to tame the beast of incidental complexity

Slide 26

Slide 26 text

Current goals • Mostly the same • Even less opinionated about infrastructure than anticipated • More opinionated about application architecture than anticipated

Slide 27

Slide 27 text

Non-goals • Messaging patterns other than RPC • Requiring specific bits of infrastructure or tooling to work • Acting as an all-in service framework • Re-implementing existing, good solutions to problems

Slide 28

Slide 28 text

Comparisons • Micro (Go) — very opinionated, all-in, framework-ish • Finagle (Scala) — original inspiration, lower-level than Go kit • Spring Boot (Java) — similar abstractions, far more magical • Tokio (Rust) — explicitly a clone of Finagle, lower-level than Go kit

Slide 29

Slide 29 text

I don’t care if you use Go kit; I do care if you use Go I really want to emphasize

Slide 30

Slide 30 text

Example service

Slide 31

Slide 31 text

type AddService interface { Sum(a, b int) int Concat(a, b string) string }

Slide 32

Slide 32 text

type AddService interface { Sum(a, b int) (int, error) Concat(a, b string) (string, error) }

Slide 33

Slide 33 text

Naïve first draft

Slide 34

Slide 34 text

type basicService struct{} func (s basicService) Sum(a, b int) (int, error) { return a + b, nil } func (s basicService) Concat(a, b string) (string, error) { return a + b, nil } Core Business Logic

Slide 35

Slide 35 text

Transport Core Business Logic

Slide 36

Slide 36 text

func (s basicService) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/sum":

Slide 37

Slide 37 text

func (s basicService) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/sum": var req struct { A int `json:"a"` B int `json:"b"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return }

Slide 38

Slide 38 text

func (s basicService) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/sum": var req struct { A int `json:"a"` B int `json:"b"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } v, err := s.Sum(req.A, req.B) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }

Slide 39

Slide 39 text

func (s basicService) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/sum": var req struct { A int `json:"a"` B int `json:"b"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } v, err := s.Sum(req.A, req.B) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]int{"v": v})

Slide 40

Slide 40 text

case "/concat":

Slide 41

Slide 41 text

case "/concat": var req struct { A string `json:"a"` B string `json:"b"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return }

Slide 42

Slide 42 text

case "/concat": var req struct { A string `json:"a"` B string `json:"b"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } v, err := s.Concat(req.A, req.B) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }

Slide 43

Slide 43 text

case "/concat": var req struct { A string `json:"a"` B string `json:"b"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } v, err := s.Concat(req.A, req.B) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]string{"v": v})

Slide 44

Slide 44 text

Transport Core Business Logic App Logging

Slide 45

Slide 45 text

if err := json.NewDecoder(r.Body).Decode(&req); err != nil { code := http.StatusBadRequest log.Printf("%s: %s: %d", r.RemoteAddr, r.URL, code) http.Error(w, err.Error(), code) return } v, err := s.Sum(req.A, req.B) if err != nil { code := http.StatusInternalServerError log.Printf("%s: %s: %d", r.RemoteAddr, r.URL, code) http.Error(w, err.Error(), code) return } w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]int{"v": v}) log.Printf("%s: %s: %d", r.RemoteAddr, r.URL, 200)

Slide 46

Slide 46 text

type basicService struct{} func (s basicService) Sum(a, b int) (v int, err error) { defer func() { log.Printf(“Sum(%d,%d)=(%d,%v)”, a, b, v, err) }() return a + b, nil } func (s basicService) Concat(a, b string) (string, error) { defer func() { log.Printf(“Concat(%q,%q)=(%q,%v)”, a, b, v, err) }() return a + b, nil }

Slide 47

Slide 47 text

Transport Core Business Logic App Logging Metrics

Slide 48

Slide 48 text

if err := json.NewDecoder(r.Body).Decode(&req); err != nil { code := http.StatusBadRequest log.Printf("%s: %s: %d", r.RemoteAddr, r.URL, code) errorCount.Add(1) http.Error(w, err.Error(), code) return } v, err := s.Sum(req.A, req.B) if err != nil { code := http.StatusInternalServerError log.Printf("%s: %s: %d", r.RemoteAddr, r.URL, code) errorCount.Add(1) http.Error(w, err.Error(), code) return } w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]int{"v": v}) log.Printf("%s: %s: %d", r.RemoteAddr, r.URL, 200)
 successCount.Add(1)

Slide 49

Slide 49 text

Transport Core Business Logic App Logging Metrics Tracing Circuit Breaking Rate Limiting Audit Logging Service Discovery Deploy Strategy Contract Testing Alerting Caching Strategy Security Service Registry :( :( :( ;( :( :(

Slide 50

Slide 50 text

The onion model

Slide 51

Slide 51 text

Transport Operational Metrics Balancing, Limiting, Safety Business Analytics Application Logging Service Metrics Business Logic Service Endpoint Transport

Slide 52

Slide 52 text

http://fideloper.com/hexagonal-architecture

Slide 53

Slide 53 text

The central rule of The Clean Architecture is the Dependency Rule, which says source code dependencies can only point inwards. — http://appliedgo.net/di

Slide 54

Slide 54 text

Service layer

Slide 55

Slide 55 text

type Service interface { Sum(ctx context.Context, a, b int) (int, error) Concat(ctx context.Context, a, b string) (string, error) }

Slide 56

Slide 56 text

type Service interface { Sum(ctx context.Context, a, b int) (int, error) Concat(ctx context.Context, a, b string) (string, error) } type basicService struct{}

Slide 57

Slide 57 text

type Service interface { Sum(ctx context.Context, a, b int) (int, error) Concat(ctx context.Context, a, b string) (string, error) } type basicService struct{} func (s basicService) Sum(_ context.Context, a, b int) (int, error) { if a == 0 && b == 0 { return 0, ErrTwoZeroes } if (b > 0 && a > (intMax-b)) || (b < 0 && a < (intMin-b)) { return 0, ErrIntOverflow } return a + b, nil }

Slide 58

Slide 58 text

type Service interface { Sum(ctx context.Context, a, b int) (int, error) Concat(ctx context.Context, a, b string) (string, error) } type basicService struct{} func (s basicService) Sum(_ context.Context, a, b int) (int, error) { if a == 0 && b == 0 { return 0, ErrTwoZeroes } if (b > 0 && a > (intMax-b)) || (b < 0 && a < (intMin-b)) { return 0, ErrIntOverflow } return a + b, nil } func (s basicService) Concat(_ context.Context, a, b string) (string, error) { if len(a)+len(b) > maxLen { return "", ErrMaxSizeExceeded } return a+b, nil }

Slide 59

Slide 59 text

type Middleware func(Service) Service

Slide 60

Slide 60 text

type Middleware func(Service) Service type loggingMiddleware struct { logger log.Logger next Service }

Slide 61

Slide 61 text

type Middleware func(Service) Service type loggingMiddleware struct { logger log.Logger next Service } func NewLoggingMiddleware(logger log.Logger) Middleware { return func(next Service) Service { return loggingMiddleware{logger, next} } }

Slide 62

Slide 62 text

type Middleware func(Service) Service type loggingMiddleware struct { logger log.Logger next Service } func NewLoggingMiddleware(logger log.Logger) Middleware { return func(next Service) Service { return loggingMiddleware{logger, next} } } func (mw loggingMiddleware) Sum(ctx context.Context, a, b int) (v int, err error) { defer func() { mw.logger.Log("method", "Sum", "a", a, "b", b, "v", v, "err", err) }() return mw.next.Sum(ctx, a, b) } // Concat is the same

Slide 63

Slide 63 text

type Middleware func(Service) Service

Slide 64

Slide 64 text

type Middleware func(Service) Service type instrumentingMiddleware struct { ints metrics.Counter chars metrics.Counter next Service }

Slide 65

Slide 65 text

type Middleware func(Service) Service type instrumentingMiddleware struct { ints metrics.Counter chars metrics.Counter next Service } func NewInstrumentingMiddleware(ints, chars metrics.Counter) Middleware { return func(next Service) Service { return instrumentingMiddleware{ ints: ints, chars: chars, next: next, } } }

Slide 66

Slide 66 text

type Middleware func(Service) Service type instrumentingMiddleware struct { ints metrics.Counter chars metrics.Counter next Service } func NewInstrumentingMiddleware(ints, chars metrics.Counter) Middleware { return func(next Service) Service { return instrumentingMiddleware{ ints: ints, chars: chars, next: next, } } } func (mw instrumentingMiddleware) Sum(ctx context.Context, a, b int) (int, error) { v, err := mw.next.Sum(ctx, a, b) mw.ints.Add(v) return v, err }

Slide 67

Slide 67 text

And a lot more...!

Slide 68

Slide 68 text

Endpoint layer

Slide 69

Slide 69 text

type Endpoint func(req) resp

Slide 70

Slide 70 text

type Endpoint func(req interface{}) (resp interface{})

Slide 71

Slide 71 text

type Endpoint func(ctx context.Context, req interface{}) (resp interface{}, err error)

Slide 72

Slide 72 text

type Endpoint func(ctx context.Context, req interface{}) (resp interface{}, err error) func MakeSumEndpoint(s Service) Endpoint { return func(ctx context.Context, req interface{}) (resp interface{}, err error) { request := req.(SumRequest) v, err := s.Sum(ctx, request.A, request.B) return SumResponse{V: v, Err: err}, nil } }

Slide 73

Slide 73 text

type Endpoint func(ctx context.Context, req interface{}) (resp interface{}, err error) func MakeSumEndpoint(s Service) Endpoint { return func(ctx context.Context, req interface{}) (resp interface{}, err error) { request := req.(SumRequest) v, err := s.Sum(ctx, request.A, request.B) return SumResponse{V: v, Err: err}, nil } } type SumRequest struct { A, B int } type SumResponse struct { V int Err error }

Slide 74

Slide 74 text

type Middleware func(Endpoint) Endpoint

Slide 75

Slide 75 text

type Middleware func(Endpoint) Endpoint func NewCircuitBreakingMiddleware(cb *gobreaker.CircuitBreaker) Middleware { return func(next Endpoint) Endpoint { return func(ctx context.Context, req interface{}) (interface{}, error) { return cb.Execute(func() (interface{}, error) { return next(ctx, req) }) } } }

Slide 76

Slide 76 text

type Middleware func(Endpoint) Endpoint func NewCircuitBreakingMiddleware(cb *gobreaker.CircuitBreaker) Middleware { return func(next Endpoint) Endpoint { return func(ctx context.Context, req interface{}) (interface{}, error) { return cb.Execute(func() (interface{}, error) { return next(ctx, req) }) } } } func NewThrottlingMiddleware(b *ratelimit.Bucket) Middleware { return func(next Endpoint) Endpoint { return func(ctx context.Context, req interface{}) (interface{}, error) { time.Sleep(tb.Take(1)) return next(ctx, request) } } }

Slide 77

Slide 77 text

Transport layer

Slide 78

Slide 78 text

Endpoint HTTP, gRPC, etc. Decode
 Request Encode
 Response

Slide 79

Slide 79 text

HTTP func NewServer( e endpoint.Endpoint, dec DecodeRequestFunc, enc EncodeResponseFunc, options ...ServerOption, ) *Server { // ...
 } func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { request, err := s.dec(ctx, r) if err != nil { return }
 response, err := s.e(ctx, request) if err != nil { return } if err := s.enc(ctx, w, response); err != nil { return } }

Slide 80

Slide 80 text

gRPC func NewServer( e endpoint.Endpoint, dec DecodeRequestFunc, enc EncodeResponseFunc, options ...ServerOption, ) *Server { // ...
 } func (s Server) ServeGRPC(ctx Context, req interface{}) (Context, interface{}, request, err := s.dec(ctx, req) if err != nil { s.logger.Log("err", err) return ctx, nil, err } response, err := s.e(ctx, request) if err != nil { return ctx, nil, err } grpcResp, err := s.enc(ctx, response) if err != nil { return ctx, nil, err } return ctx, grpcResp, nil }

Slide 81

Slide 81 text

HTTP Mux HTTP Server HTTP Server CB CB RL RL Concat Sum Logging Service

Slide 82

Slide 82 text

HTTP Mux gRPC Mux gRPC Server gRPC Server HTTP Server HTTP Server CB CB RL RL Concat Sum Logging Service

Slide 83

Slide 83 text

Simple & non-magical We embrace and extend Go’s philosophy, to be in service to software engineering in the large * *

Slide 84

Slide 84 text

Maintainability Optimize for above all else!

Slide 85

Slide 85 text

Integration with the
 Cloud Native stack

Slide 86

Slide 86 text

Prometheus type metricsMiddleware struct { ints metrics.Counter chars metrics.Counter next Service } func (mw metricsMiddleware) Sum(ctx context.Context, a, b int) (int, error) { v, err := mw.next.Sum(ctx, a, b) mw.ints.Add(v) return v, err } func (mw metricsMiddleware) Concat(ctx context.Context, a, b string) (string, error) { v, err := mw.next.Concat(ctx, a, b) mw.chars.Add(len(v)) return v, err }

Slide 87

Slide 87 text

OpenTracing & Zipkin Transport Operational Metrics Balancing, Limiting, Safety Business Analytics Application Logging Service Metrics Business Logic Extract Inject Annotate & Collect

Slide 88

Slide 88 text

OpenTracing & Zipkin Extract Inject Annotate & Collect

Slide 89

Slide 89 text

Demo

Slide 90

Slide 90 text

Where to Go from here?

Slide 91

Slide 91 text

invite.slack.golangbridge.org appliedgo.net gobyexample.com gokit.io

Slide 92

Slide 92 text

gophercon.com

Slide 93

Slide 93 text

Thanks @peterbourgon · Cloud Native Con 2017 · Berlin