Slide 1

Slide 1 text

Contexts in Context Matt Heath

Slide 2

Slide 2 text

➡ ➡ ❓ ➡

Slide 3

Slide 3 text

Hi, I’m Matt

Slide 4

Slide 4 text

@mattheath

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Nov 2015 Apr 2019

Slide 12

Slide 12 text

Nov 2015 Apr 2019

Slide 13

Slide 13 text

Nov 2015 Apr 2019

Slide 14

Slide 14 text

Nov 2015 Apr 2019

Slide 15

Slide 15 text

Nov 2015 Fastest growing UK Bank 1.8M Customers Apr 2019

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

package context type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} }

Slide 22

Slide 22 text

package context type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} }

Slide 23

Slide 23 text

package context type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} }

Slide 24

Slide 24 text

package context type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} }

Slide 25

Slide 25 text

package context type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} }

Slide 26

Slide 26 text

package context type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} }

Slide 27

Slide 27 text

ctx := context.Background()

Slide 28

Slide 28 text

ctx := context.Background() ctx = context.WithValue(ctx, "TraceID", "boop")

Slide 29

Slide 29 text

ctx := context.Background() ctx = context.WithValue(ctx, "TraceID", "boop") ctx.Value("TraceID") => "boop"

Slide 30

Slide 30 text

ctx := context.Background() ctx = context.WithValue(ctx, "TraceID", "boop") ctx.Value("TraceID") => "boop" Contexts are immutable

Slide 31

Slide 31 text

func(ctx, ...)

Slide 32

Slide 32 text

func(ctx, ...) func(ctx, ...)

Slide 33

Slide 33 text

func(ctx, ...) func(ctx, ...)

Slide 34

Slide 34 text

func(ctx, ...) func(ctx, ...) func(ctx, ...) func(ctx, ...) func(ctx, ...)

Slide 35

Slide 35 text

func(ctx, ...) func(ctx, ...) func(ctx, ...) func(ctx, ...) func(ctx, ...)

Slide 36

Slide 36 text

func(ctx, ...) func(ctx, ...) func(ctx, ...) func(ctx, ...) func(ctx, ...)

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

func(ctx, ...) req.WithContext(ctx)

Slide 39

Slide 39 text

func(ctx, ...) req.WithContext(ctx) Remote API

Slide 40

Slide 40 text

func(ctx, ...) req.WithContext(ctx) Remote API ⏱

Slide 41

Slide 41 text

func(ctx, ...) req.WithContext(ctx) Remote API ⏱ Deadline Exceeded err

Slide 42

Slide 42 text

package context type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} }

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

LICENCE WITH RESTRICTIONS WE ARE HERE A UK banking licence is authorised by the PRA and regulated by the PRA and FCA1, allowing deposit- taking and balance sheet lending. Once granted, it allows firms to passport across Europe, accessing This is followed by a “mobilisation” phase during which final capital is raised and IT systems are completed, before launching to the public. We received a UK banking licence in August 2016! FEB 2015 JAN 2016 JAN 2017 MAR FEB FEB APR MAR MAR APR MAY JUN JUL MAY APR JUN MAY JUL JUN AUG JUL SEP AUG NOV SEP DEC NOV DEC PRE APPLICATION APPLICATION MOBILISATION LAUNCH LICENCE WITH RESTRICTIONS 50K MAX DEPOSIT WE ARE HERE A UK banking licence is authorised by the PRA and regulated by the PRA and FCA1, allowing deposit- taking and balance sheet lending. Once granted, it allows firms to passport across Europe, accessing This is followed by a “mobilisation” phase during which final capital is raised and IT systems are completed, before launching to the public. We received a UK banking licence in August 2016! JAN 2016 JAN 2017 MAR FEB FEB APR MAR MAR APR MAY JUN JUL MAY APR JUN MAY JUL JUN AUG JUL SEP AUG NOV SEP DEC NOV DEC E APPLICATION APPLICATION MOBILISATION LAUNCH LICENCE WITH RESTRICTIONS WE ARE HERE AUG
 2016 JAN
 2016 Feb
 2015 APR
 2017 A UK banking licence is authorised by the PRA and regulated by the PRA and FCA1, allowing deposit- taking and balance sheet lending. Once granted, it allows firms to passport across Europe, accessing This is followed by a “mobilisation” phase during which final capital is raised and IT systems are completed, before launching to the public. We received a UK banking licence in August 2016! FEB 2015 JAN 2016 JAN 2017 MAR FEB FEB APR MAR MAR APR MAY APR JUN MAY JUL JUN AUG JUL SEP AUG NOV SEP DEC NOV DEC PRE APPLICATION APPLICATION MOBILISATION LICENCE WITH RESTRICTIONS WE ARE HERE

Slide 48

Slide 48 text

LICENCE WITH RESTRICTIONS WE ARE HERE A UK banking licence is authorised by the PRA and regulated by the PRA and FCA1, allowing deposit- taking and balance sheet lending. Once granted, it allows firms to passport across Europe, accessing This is followed by a “mobilisation” phase during which final capital is raised and IT systems are completed, before launching to the public. We received a UK banking licence in August 2016! FEB 2015 JAN 2016 JAN 2017 MAR FEB FEB APR MAR MAR APR MAY JUN JUL MAY APR JUN MAY JUL JUN AUG JUL SEP AUG NOV SEP DEC NOV DEC PRE APPLICATION APPLICATION MOBILISATION LAUNCH LICENCE WITH RESTRICTIONS 50K MAX DEPOSIT WE ARE HERE A UK banking licence is authorised by the PRA and regulated by the PRA and FCA1, allowing deposit- taking and balance sheet lending. Once granted, it allows firms to passport across Europe, accessing This is followed by a “mobilisation” phase during which final capital is raised and IT systems are completed, before launching to the public. We received a UK banking licence in August 2016! JAN 2016 JAN 2017 MAR FEB FEB APR MAR MAR APR MAY JUN JUL MAY APR JUN MAY JUL JUN AUG JUL SEP AUG NOV SEP DEC NOV DEC E APPLICATION APPLICATION MOBILISATION LAUNCH LICENCE WITH RESTRICTIONS WE ARE HERE AUG
 2016 JAN
 2016 Feb
 2015 APR
 2017 A UK banking licence is authorised by the PRA and regulated by the PRA and FCA1, allowing deposit- taking and balance sheet lending. Once granted, it allows firms to passport across Europe, accessing This is followed by a “mobilisation” phase during which final capital is raised and IT systems are completed, before launching to the public. We received a UK banking licence in August 2016! FEB 2015 JAN 2016 JAN 2017 MAR FEB FEB APR MAR MAR APR MAY APR JUN MAY JUL JUN AUG JUL SEP AUG NOV SEP DEC NOV DEC PRE APPLICATION APPLICATION MOBILISATION LICENCE WITH RESTRICTIONS WE ARE HERE 2 YEARS AGO!!

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

?

Slide 51

Slide 51 text

Slide 52

Slide 52 text

Slide 53

Slide 53 text

Slide 54

Slide 54 text

Application

Slide 55

Slide 55 text

Application

Slide 56

Slide 56 text

APPLICATION Application

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Feb 2015 Apr 2019 1000 services

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

package slog
 
 func Error(ctx context.Context, msg string, params ...interface{}) { if l := DefaultLogger(); l != nil { l.Log(Eventf(DebugSeverity, ctx, msg, params...)) } }

Slide 69

Slide 69 text

package slog
 
 func Error(ctx context.Context, msg string, params ...interface{}) { if l := DefaultLogger(); l != nil { l.Log(Eventf(DebugSeverity, ctx, msg, params...)) } }

Slide 70

Slide 70 text

func thing(ctx context.Context, ...) {
 // do things... 
 slog.Error(ctx, “Oh no!”, err)
 
 }

Slide 71

Slide 71 text

func thing(ctx context.Context, ...) {
 // do things... 
 slog.Error(ctx, “Oh no!”, err)
 
 }

Slide 72

Slide 72 text

2019-04-17 15:38:09.134539276 [DBG] Slog -> Auth request: 
 {"auth_type": "Auth", "merchant": {"merchant_description": "Deliveroo! LONDON UK", "merchant_id": "XXXX"}, "network": “Mastercard" ...}
 {opentracing_id=2855967a9de91afb, trace_id=19ce030f-f652-4b4d-5f35-5d75f472f482, service=service.blah, service_version=d7b742fe5c03998748348fe9d7d81a6b8c7358d, hostname=s- blah-79f9b54887-j62d2}
 
 (handle_auth_message.go:25 handleAuthMessage)

Slide 73

Slide 73 text

2019-04-17 15:38:09.134539276 [DBG] Slog -> Auth request: 
 {"auth_type": "Auth", "merchant": {"merchant_description": “Deliveroo! LONDON UK", "merchant_id": "XXXX"}, "network": “Mastercard" ...}
 {opentracing_id=2855967a9de91afb, trace_id=19ce030f-f652-4b4d-5f35-5d75f472f482, service=service.blah, service_version=d7b742fe5c03998748348fe9d7d81a6b8c7358d, hostname=s- blah-79f9b54887-j62d2}
 
 (handle_auth_message.go:25 handleAuthMessage)

Slide 74

Slide 74 text

API

Slide 75

Slide 75 text

API Service API Service API Service API Service API Service API Service API Service API Service Edge Gateway

Slide 76

Slide 76 text

Accounts Cards Pots Feed Payments … Edge Gateway

Slide 77

Slide 77 text

Cards Pots Payments … Accounts New
 Exciting
 API!! Feed Edge Gateway

Slide 78

Slide 78 text

Cards Pots Payments … Accounts New
 Exciting
 API!! Feed Edge Gateway

Slide 79

Slide 79 text

Edge Gateway Accounts Cards Pots Payments … New
 Exciting
 API!! Feed

Slide 80

Slide 80 text

Load Balancer Edge Gateway

Slide 81

Slide 81 text

Load Balancer Edge Gateway Request ID: 19ce030f-f652-4b4d-5f35-5d75f472f482

Slide 82

Slide 82 text

// Generate new ID // Store value in context tid, err := NewTraceID() // v4 UUID if err == nil { req.Context = context.WithValue(req.Context, "Trace-Id", tid) }

Slide 83

Slide 83 text

// Generate new ID // Store value in context tid, err := NewTraceID() // v4 UUID if err == nil { req.Context = context.WithValue(req.Context, "Trace-Id", tid) }

Slide 84

Slide 84 text

// Generate new ID // Store value in context tid, err := NewTraceID() // v4 UUID if err == nil { req.Context = context.WithValue(req.Context, "Trace-Id", tid) }

Slide 85

Slide 85 text

Load Balancer Edge Gateway Request ID: 19ce030f-f652-4b4d-5f35-5d75f472f482 Request ID: 19ce030f-f652-4b4d-5f35-5d75f472f482 API Service

Slide 86

Slide 86 text

Service A Service B Load Balancer Edge Gateway Request ID: 19ce030f-f652-4b4d-5f35-5d75f472f482 Request ID: 19ce030f-f652-4b4d-5f35-5d75f472f482 Request ID: 19ce030f-f652-4b4d-5f35-5d75f472f482 API Service

Slide 87

Slide 87 text

Service Service

Slide 88

Slide 88 text

Service Service Context to Headers Headers to Context HTTP

Slide 89

Slide 89 text

// Add headers previously stashed in the context if stash, ok := req.Context.Value(ctxKey).(http.Header); ok && stash != nil { for k, v := range stash { if req.Header[k] == nil { req.Header[k] = v } } } // Stash headers in the context stash := http.Header{} for k, v := range req.Header { for _, r := range passedHeaders { if r.MatchString(k) { stash[k] = v } } } if len(stash) > 0 { req.Context = context.WithValue(req.Context, ctxKey, stash) }

Slide 90

Slide 90 text

// Add headers previously stashed in the context if stash, ok := req.Context.Value(ctxKey).(http.Header); ok && stash != nil { for k, v := range stash { if req.Header[k] == nil { req.Header[k] = v } } } // Stash headers in the context stash := http.Header{} for k, v := range req.Header { for _, r := range passedHeaders { if r.MatchString(k) { stash[k] = v } } } if len(stash) > 0 { req.Context = context.WithValue(req.Context, ctxKey, stash) }

Slide 91

Slide 91 text

// Add headers previously stashed in the context if stash, ok := req.Context.Value(ctxKey).(http.Header); ok && stash != nil { for k, v := range stash { if req.Header[k] == nil { req.Header[k] = v } } } // Stash headers in the context stash := http.Header{} for k, v := range req.Header { for _, r := range passedHeaders { if r.MatchString(k) { stash[k] = v } } } if len(stash) > 0 { req.Context = context.WithValue(req.Context, ctxKey, stash) }

Slide 92

Slide 92 text

// Add headers previously stashed in the context if stash, ok := req.Context.Value(ctxKey).(http.Header); ok && stash != nil { for k, v := range stash { if req.Header[k] == nil { req.Header[k] = v } } } // Stash headers in the context stash := http.Header{} for k, v := range req.Header { for _, r := range passedHeaders { if r.MatchString(k) { stash[k] = v } } } if len(stash) > 0 { req.Context = context.WithValue(req.Context, ctxKey, stash) }

Slide 93

Slide 93 text

Service Service Context to Headers Headers to Context HTTP

Slide 94

Slide 94 text

Service Service Context to Headers Headers to Context

Slide 95

Slide 95 text

Service Service ✨ ✨ ✨ HTTP HTTP HTTP

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

Correlate logs Observe database queries Queue publishing Distributed locks

Slide 100

Slide 100 text

Correlate logs Observe database queries Queue publishing Distributed locks Understand request flow What’s slow?! What’s broken?!

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

Service Service

Slide 104

Slide 104 text

Service Service cancel() or ctx.Done()

Slide 105

Slide 105 text

Service Service x cancel() or ctx.Done() Terminate connection (http1)
 Cancel request (http2)

Slide 106

Slide 106 text

Service Service cancel() or ctx.Done() context cancelled Terminate connection (http1)
 Cancel request (http2) x

Slide 107

Slide 107 text

Cancel downstream requests

Slide 108

Slide 108 text

Cancel outstanding work! Decrease tail latencies! Reduce load on your database!

Slide 109

Slide 109 text

Cancel outstanding work! Decrease tail latencies! Reduce load on your database!

Slide 110

Slide 110 text

rspCh := make(chan Response, 1) go dispatchRequest(ctx, rspCh)

Slide 111

Slide 111 text

rspCh := make(chan Response, 1) go dispatchRequest(ctx, rspCh) select { case rsp := <-rspCh: return rsp case <-ctx.Done(): return ctx.Error() }

Slide 112

Slide 112 text

rspCh := make(chan Response, 1) go dispatchRequest(ctx, rspCh) select { case rsp := <-rspCh: return rsp case <-ctx.Done(): return ctx.Error() }

Slide 113

Slide 113 text

rspCh := make(chan Response, 1) go dispatchRequest(ctx, rspCh) select { case rsp := <-rspCh: return rsp case <-ctx.Done(): return ctx.Error() }

Slide 114

Slide 114 text

// check if the context is cancelled! 
 select { case <-ctx.Done(): return ctx.Error() default: } wooooooo… let’s go!

Slide 115

Slide 115 text

Cancel outstanding work! Decrease tail latencies! Reduce load on your database!

Slide 116

Slide 116 text

Cancel outstanding work! Decrease tail latencies! Reduce load on your database!
 
 Unless operations should complete…

Slide 117

Slide 117 text

Service A Service B Load Balancer Edge Gateway API Service

Slide 118

Slide 118 text

Service A Service B Load Balancer Edge Gateway API Service Client disconnects

Slide 119

Slide 119 text

Service A Service B Load Balancer Edge Gateway API Service Client disconnects Cascading failure!! Errors everywhere!! Intense panic!

Slide 120

Slide 120 text

func(ctx, ...) func(ctx, ...) func(ctx, ...) func(ctx, ...) func(ctx, ...)

Slide 121

Slide 121 text

func(ctx, ...) func(ctx, ...) func(context.Background(), ...) func(ctx, ...) func(ctx, ...)

Slide 122

Slide 122 text

func(ctx, ...) func(ctx, ...) func(context.Background(), ...) func(ctx, ...) func(ctx, ...) DANGER ZONE

Slide 123

Slide 123 text

Request scoped variables Dependency Injection Understand your systems.

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

monzo.com/careers