Slide 1

Slide 1 text

SERVERLESS FOR MERE MORTALS NIKOLAY SVERCHKOV @ssnickolay

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

evilmartians.com

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

SERVERLESS FOR MERE MORTALS

Slide 6

Slide 6 text

OR

Slide 7

Slide 7 text

HOW I REDUCED COSTS BY 250 TIMES USING SERVERLESS

Slide 8

Slide 8 text

PAST

Slide 9

Slide 9 text

IN-HOUSE SERVER A LONG TIME AGO

Slide 10

Slide 10 text

A LONG TIME AGO MAINTAINABILITY IN-HOUSE SERVER

Slide 11

Slide 11 text

DEDICATED SERVER NOT SO LONG AGO ovh.com

Slide 12

Slide 12 text

DEDICATED SERVER NOT SO LONG AGO SCALABILITY

Slide 13

Slide 13 text

CLOUDS NOT SO LONG AGO

Slide 14

Slide 14 text

CLOUDS NOT SO LONG AGO

Slide 15

Slide 15 text

CLOUDS NOT SO LONG AGO

Slide 16

Slide 16 text

CLOUDS NOT SO LONG AGO ? CPU ? RAM ? DISK

Slide 17

Slide 17 text

http://bit.ly/2X9qnp5

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

?

Slide 20

Slide 20 text

KANYE WEST

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

~66.500 RUB

Slide 23

Slide 23 text

LOAD 0 25 50 75 100 0 1 2 3 4 5 6 7

Slide 24

Slide 24 text

LOAD 0 25 50 75 100 0 1 2 3 4 5 6 7

Slide 25

Slide 25 text

CAPACITY PLANNING

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

CLOUDS NOT SO LONG AGO CAPACITY PLANNING

Slide 28

Slide 28 text

MORE ABSTRACTION! move away from server assessment models

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

2014

Slide 31

Slide 31 text

HOW IT WORKS

Slide 32

Slide 32 text

; ; ; AWS Lambda 1. SLEEP SOURCE_CODE.ZIP S3

Slide 33

Slide 33 text

AWS Lambda 1. SLEEP 2. EVENT

Slide 34

Slide 34 text

AWS Lambda 1. SLEEP 2. EVENT 3. HANDLE

Slide 35

Slide 35 text

function(event, context) { console.log(event.key1); return “Success”; }

Slide 36

Slide 36 text

AWS Lambda 1. SLEEP 2. EVENT 3. HANDLE 4. RESPONSE

Slide 37

Slide 37 text

; ; ; AWS Lambda 1. SLEEP 2. EVENT 3. HANDLE 4. RESPONSE 5. -> 1

Slide 38

Slide 38 text

HTTP ❤ EVENT

Slide 39

Slide 39 text

BLOG + STATIC PAGES RAILS 3; DO; NGINX ASSETS PIPELINE

Slide 40

Slide 40 text

User Lambda Markdown -> HTML

Slide 41

Slide 41 text

User S3 Lambda Assets Markdown -> HTML CloudFront

Slide 42

Slide 42 text

User S3 Lambda Assets Markdown -> HTML + CloudFront

Slide 43

Slide 43 text

NOTE 1: THE RIGHT SOLUTION (JAMstack) +

Slide 44

Slide 44 text

NOTE 2: CAUTION OPEN FLAME

Slide 45

Slide 45 text

NOTE 2: CAUTION OPEN FLAME

Slide 46

Slide 46 text

CAUTION OPEN FLAME

Slide 47

Slide 47 text

⏱ DEC 2018 % +

Slide 48

Slide 48 text

DEC 2018 MARCH 2019 % +

Slide 49

Slide 49 text

VS http://bit.ly/2Dd7R7X

Slide 50

Slide 50 text

SERVERLESS

Slide 51

Slide 51 text

SERVERLESS $ serverless create !--template aws-go !!==> serverless.yml

Slide 52

Slide 52 text

SERVERLESS $ serverless deploy $ serverless rollback !--timestamp timestamp $ serverless invoke -f functionName $ serverless logs -f functionName $ serverless create !--template aws-go !!==> serverless.yml

Slide 53

Slide 53 text

SERVERLESS $ serverless deploy $ serverless rollback !--timestamp timestamp $ serverless invoke -f functionName $ serverless logs -f functionName $ serverless ???? $ serverless create !--template aws-go !!==> serverless.yml

Slide 54

Slide 54 text

SAM(-CLI) https://github.com/lambci/docker-lambda Docker images and test runners that replicate the live AWS Lambda environment

Slide 55

Slide 55 text

TEMPLATES https://github.com/serverless/examples

Slide 56

Slide 56 text

serverless.yml functions: post: !<<: *default handler: bin/post events: - http: path: blog/{slug} method: get

Slide 57

Slide 57 text

post.go package main import ( "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-lambda-go/lambda" ) func Handler(ctx context.Context, request Request) (events.APIGatewayProxyResponse, error) { !// put code here } func main() { lambda.Start(Handler) }

Slide 58

Slide 58 text

post.go func Handler(_, request Request) (_, _) { var buf bytes.Buffer var slug = request.PathParameters["slug"] … }

Slide 59

Slide 59 text

post.go func Handler(_, request Request) (_, _) { var buf bytes.Buffer var slug = request.PathParameters["slug"] post !:= lib.NewPost(slug, lambdaF.Config) if err !:= post.Render(&buf, lambdaF.Renderer); err !!= nil { return events.APIGatewayProxyResponse{StatusCode: 404}, err } … }

Slide 60

Slide 60 text

post.go func Handler(_, request Request) (_, _) { var buf bytes.Buffer var slug = request.PathParameters["slug"] post !:= lib.NewPost(slug, lambdaF.Config) if err !:= post.Render(&buf, lambdaF.Renderer); err !!= nil { return events.APIGatewayProxyResponse{StatusCode: 404}, err } return lambdaF.Render(&buf) }

Slide 61

Slide 61 text

REDUCED x250 0.02$ vs 5$

Slide 62

Slide 62 text

+VAT = 6$!!! REDUCED x300

Slide 63

Slide 63 text

SERVERLESS UNDER THE MICROSCOPE

Slide 64

Slide 64 text

LOG $ serverless invoke -f index -l

Slide 65

Slide 65 text

LOG $ serverless invoke -f index -l START RequestId: Version: $LATEST END RequestId: REPORT RequestId: Duration: 1.48 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 31 MB

Slide 66

Slide 66 text

LOG $ serverless invoke -f index -l START RequestId: Version: $LATEST END RequestId: REPORT RequestId: Duration: 1.48 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 31 MB

Slide 67

Slide 67 text

LOG $ serverless invoke -f index -l START RequestId: Version: $LATEST END RequestId: REPORT RequestId: Duration: 1.48 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 31 MB

Slide 68

Slide 68 text

WHAT ABOUT CPU?

Slide 69

Slide 69 text

AWS Lambda allocates CPU power proportional to the memory… (с) Amazon Docs

Slide 70

Slide 70 text

AWS Lambda allocates CPU power proportional to the memory… For example, if you allocate 256 MB memory, your Lambda function will receive twice the CPU share than if you allocated only 128 MB. (с) Amazon Docs

Slide 71

Slide 71 text

https://aws.amazon.com/lambda/pricing/

Slide 72

Slide 72 text

6$ / PRICE(100ms) = 28.8M!

Slide 73

Slide 73 text

MEMORY / CST http://bit.ly/2UZ7xDL Completely Fair Scheduler (CFS)

Slide 74

Slide 74 text

FREE!

Slide 75

Slide 75 text

CST https://amzn.to/2Ix2t2O

Slide 76

Slide 76 text

COST !!<=> LANGUAGE

Slide 77

Slide 77 text

$ wrk -t10 -c400 -d20s -R40000 http://endpoint.url http://github.com/ssnickolay/dump2019

Slide 78

Slide 78 text

$ wrk -t10 -c400 -d20s -R40000 $ curl http://endpoint.url/[ruby/go] !=> {“success”: true} http://github.com/ssnickolay/dump2019

Slide 79

Slide 79 text

0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~80ms BATTLE (128 MB)

Slide 80

Slide 80 text

0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 BATTLE (128 MB)

Slide 81

Slide 81 text

0 250 500 750 1000 0 1 2 4 5 0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~1.8ms AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 50347 requests in 20.00s Requests/sec: 2517.30 BATTLE (128 MB)

Slide 82

Slide 82 text

0 250 500 750 1000 0 1 2 4 5 0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~1.8ms AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 50347 requests in 20.00s Requests/sec: 2517.30 W IN BATTLE (128 MB)

Slide 83

Slide 83 text

0 250 500 750 1000 0 1 2 4 5 0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~1.8ms AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 50347 requests in 20.00s Requests/sec: 2517.30 W IN + BATTLE (128 MB)

Slide 84

Slide 84 text

0 250 500 750 1000 0 1 2 4 5 0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~1.8ms AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 50347 requests in 20.00s Requests/sec: 2517.30 BATTLE (128 MB)

Slide 85

Slide 85 text

BATTLE (128 MB) AVG ~1.8ms AVG ~80ms “Billed Duration: 100 ms” (c) Lambda Log

Slide 86

Slide 86 text

COST !!<=> PACKAGE SIZE

Slide 87

Slide 87 text

5.4MB 140KB COST !!<=> PACKAGE SIZE MIN SIZE

Slide 88

Slide 88 text

Go + WRK2 Milliseconds (AVG) 1,7 1,775 1,85 1,925 2 Megabytes 6 25 150 220

Slide 89

Slide 89 text

5.4MB 140KB COST !!<=> PACKAGE SIZE

Slide 90

Slide 90 text

http://rubyonjets.com/

Slide 91

Slide 91 text

COST !!<=> DEPENDENCIES

Slide 92

Slide 92 text

MINIMIZE THE COMPLEXITY OF YOUR DEPENDENCIES

Slide 93

Slide 93 text

MINIMIZE THE COMPLEXITY OF YOUR DEPENDENCIES

Slide 94

Slide 94 text

MINIMIZE THE COMPLEXITY OF YOUR DEPENDENCIES

Slide 95

Slide 95 text

WAIT A MINUTE

Slide 96

Slide 96 text

https://stackoverflow.com/q/54110697

Slide 97

Slide 97 text

When you write your Lambda function code, do not assume that AWS Lambda automatically reuses the execution context for subsequent function invocations…. (c) AWS Docs AWS LAMBDA EXECUTION CONTEXT

Slide 98

Slide 98 text

func Fibonacci(n uint) uint { if n !<= 1 { return n } return Fibonacci(n-1) + Fibonacci(n-2) } OPTION 1

Slide 99

Slide 99 text

func Fibonacci(n uint) uint { if n !<= 1 { return n } return Fibonacci(n-1) + Fibonacci(n-2) } func main() { lambda.Start(Handler) } OPTION 1

Slide 100

Slide 100 text

func Fibonacci(n uint) uint { if n !<= 1 { return n } return Fibonacci(n-1) + Fibonacci(n-2) } !// Handler is our lambda handler invoked by !// the `lambda.Start` function call func Handler(ctx context.Context) (Response, error) { Fib !:= Fibonacci(33) !// noise here resp !:= Response{ Body: Fib } … } func main() { lambda.Start(Handler) } OPTION 1

Slide 101

Slide 101 text

Milliseconds (AVG) 0 250 500 750 1000 1 2 3 4 Golang (128MB + Fib) ~ 400ms Option1

Slide 102

Slide 102 text

var Fib uint func Fibonacci(n uint) uint {…} func Handler(ctx context.Context) (Response, error) { … } func init() { Fib = Fibonacci(33) !// noise here } func main() {…} OPTION 2

Slide 103

Slide 103 text

Milliseconds (AVG) 0 250 500 750 1000 1 2 3 4 Golang (128MB + Fib) ~ 400ms Option1 Option2

Slide 104

Slide 104 text

COST SPEED ⏫

Slide 105

Slide 105 text

0 15000 30000 45000 60000 0 1 2 4 5 50347 requests in 20.00s Requests/sec: 2517.30 LOAD !!<=> $$$

Slide 106

Slide 106 text

0 15000 30000 45000 60000 0 1 2 4 5 50347 requests in 20.00s Requests/sec: 2517.30 PROTECTION 1: X-API-KEY

Slide 107

Slide 107 text

0 15000 30000 45000 60000 0 1 2 4 5 PROTECTION 2: AWS SHIELD 50347 requests in 20.00s Requests/sec: 2517.30

Slide 108

Slide 108 text

0 15000 30000 45000 60000 0 1 2 4 5 50347 requests in 20.00s Requests/sec: 2517.30 62808 requests in 20.01s Non-2xx or 3xx responses: 62127 1 1000 CONCURRENCY PROTECTION 3: THROTTLING

Slide 109

Slide 109 text

PREDICTABLY

Slide 110

Slide 110 text

PREDICTABLY

Slide 111

Slide 111 text

PREDICTABLY

Slide 112

Slide 112 text

RECIPE

Slide 113

Slide 113 text

1. TAKE SERVERLESS

Slide 114

Slide 114 text

1. TAKE SERVERLESS 2. FOLLOW RULES

Slide 115

Slide 115 text

1. TAKE SERVERLESS 2. FOLLOW RULES 3. ????

Slide 116

Slide 116 text

1. TAKE SERVERLESS 2. FOLLOW RULES 3. ???? 4. PROFIT

Slide 117

Slide 117 text

WHAT ABOUT PRODUCTION?

Slide 118

Slide 118 text

WHAT ABOUT PRODUCTION? Tung Nguyen

Slide 119

Slide 119 text

@ssnickolay Nikolay Sverchkov @ssnickolay THANK YOU! @evilmartians http://evl.ms/blog http://evl.ms/telegram