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

Serverless for mere mortals

Serverless for mere mortals

Nikolay Sverchkov

April 22, 2019
Tweet

More Decks by Nikolay Sverchkov

Other Decks in Programming

Transcript

  1. SERVERLESS
    FOR
    MERE MORTALS
    NIKOLAY SVERCHKOV
    @ssnickolay

    View Slide

  2. View Slide

  3. evilmartians.com

    View Slide

  4. View Slide

  5. SERVERLESS
    FOR
    MERE MORTALS

    View Slide

  6. OR

    View Slide

  7. HOW I REDUCED COSTS BY
    250 TIMES
    USING SERVERLESS

    View Slide

  8. PAST

    View Slide

  9. IN-HOUSE SERVER
    A LONG TIME AGO

    View Slide

  10. A LONG TIME AGO
    MAINTAINABILITY
    IN-HOUSE SERVER

    View Slide

  11. DEDICATED SERVER
    NOT SO LONG AGO
    ovh.com

    View Slide

  12. DEDICATED SERVER
    NOT SO LONG AGO
    SCALABILITY

    View Slide

  13. CLOUDS
    NOT SO LONG AGO

    View Slide

  14. CLOUDS
    NOT SO LONG AGO

    View Slide

  15. CLOUDS
    NOT SO LONG AGO

    View Slide

  16. CLOUDS
    NOT SO LONG AGO
    ? CPU
    ? RAM
    ? DISK

    View Slide

  17. http://bit.ly/2X9qnp5

    View Slide

  18. View Slide

  19. ?

    View Slide

  20. KANYE
    WEST

    View Slide

  21. View Slide

  22. ~66.500 RUB

    View Slide

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

    View Slide

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

    View Slide

  25. CAPACITY PLANNING

    View Slide

  26. View Slide

  27. CLOUDS
    NOT SO LONG AGO
    CAPACITY
    PLANNING

    View Slide

  28. MORE
    ABSTRACTION!
    move away from server assessment models

    View Slide

  29. View Slide

  30. 2014

    View Slide

  31. HOW IT WORKS

    View Slide

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

    View Slide

  33. AWS Lambda
    1. SLEEP
    2. EVENT

    View Slide

  34. AWS Lambda
    1. SLEEP
    2. EVENT
    3. HANDLE

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  38. HTTP ❤
    EVENT

    View Slide

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

    View Slide

  40. User Lambda
    Markdown -> HTML

    View Slide

  41. User
    S3
    Lambda
    Assets
    Markdown -> HTML
    CloudFront

    View Slide

  42. User
    S3
    Lambda
    Assets
    Markdown -> HTML
    +
    CloudFront

    View Slide

  43. NOTE 1: THE RIGHT SOLUTION
    (JAMstack)
    +

    View Slide

  44. NOTE 2: CAUTION OPEN FLAME

    View Slide

  45. NOTE 2: CAUTION OPEN FLAME

    View Slide

  46. CAUTION OPEN FLAME

    View Slide


  47. DEC 2018
    %
    +

    View Slide

  48. DEC 2018
    MARCH 2019
    %
    +

    View Slide

  49. VS
    http://bit.ly/2Dd7R7X

    View Slide

  50. SERVERLESS

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  57. 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)
    }

    View Slide

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

    }

    View Slide

  59. 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
    }

    }

    View Slide

  60. 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)
    }

    View Slide

  61. REDUCED
    x250
    0.02$ vs 5$

    View Slide

  62. +VAT = 6$!!!
    REDUCED
    x300

    View Slide

  63. SERVERLESS
    UNDER
    THE
    MICROSCOPE

    View Slide

  64. LOG
    $ serverless invoke -f index -l

    View Slide

  65. 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

    View Slide

  66. 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

    View Slide

  67. 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

    View Slide

  68. WHAT ABOUT
    CPU?

    View Slide

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

    View Slide

  70. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  74. FREE!

    View Slide

  75. CST
    https://amzn.to/2Ix2t2O

    View Slide

  76. COST
    !!<=>
    LANGUAGE

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  80. 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)

    View Slide

  81. 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)

    View Slide

  82. 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)

    View Slide

  83. 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)

    View Slide

  84. 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)

    View Slide

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

    View Slide

  86. COST
    !!<=>
    PACKAGE
    SIZE

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  90. http://rubyonjets.com/

    View Slide

  91. COST
    !!<=>
    DEPENDENCIES

    View Slide

  92. MINIMIZE THE COMPLEXITY OF
    YOUR DEPENDENCIES

    View Slide

  93. MINIMIZE THE COMPLEXITY OF
    YOUR DEPENDENCIES

    View Slide

  94. MINIMIZE THE COMPLEXITY OF
    YOUR DEPENDENCIES

    View Slide

  95. WAIT
    A MINUTE

    View Slide

  96. https://stackoverflow.com/q/54110697

    View Slide

  97. 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

    View Slide

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

    View Slide

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

    View Slide

  100. 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

    View Slide

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

    View Slide

  102. 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

    View Slide

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

    View Slide

  104. COST
    SPEED ⏫

    View Slide

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

    View Slide

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

    View Slide

  107. 0
    15000
    30000
    45000
    60000
    0 1 2 4 5
    PROTECTION 2: AWS SHIELD

    50347 requests in 20.00s
    Requests/sec: 2517.30

    View Slide

  108. 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

    View Slide

  109. PREDICTABLY

    View Slide

  110. PREDICTABLY

    View Slide

  111. PREDICTABLY

    View Slide

  112. RECIPE

    View Slide

  113. 1. TAKE SERVERLESS

    View Slide

  114. 1. TAKE SERVERLESS
    2. FOLLOW RULES

    View Slide

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

    View Slide

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

    View Slide

  117. WHAT ABOUT
    PRODUCTION?

    View Slide

  118. WHAT ABOUT
    PRODUCTION?
    Tung Nguyen

    View Slide

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

    View Slide