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

Serverless Is Ruby Future

Serverless Is Ruby Future

#rubyrussia 2019

Nikolay Sverchkov

September 28, 2019
Tweet

More Decks by Nikolay Sverchkov

Other Decks in Programming

Transcript

  1. SERVERLESS
    IS
    UBY
    FUTURE
    NIKOLAY SVERCHKOV
    @ssnickolay

    View Slide

  2. View Slide

  3. evilmartians.com
    New

    View Slide

  4. ME
    Community
    Speech
    TWO
    OPTIONS

    View Slide

  5. ME
    Community
    Speech
    VOTE

    26 %

    74 %

    View Slide

  6. SERVERLESS AXIOMS
    Event-Driven Architecture

    View Slide

  7. SERVERLESS AXIOMS
    Event-Driven Architecture
    Stateless

    View Slide

  8. SERVERLESS AXIOMS
    Event-Driven Architecture
    Stateless
    ♾ Infinite Scaling

    View Slide

  9. SERVERLESS AXIOMS
    Event-Driven Architecture
    Stateless
    ♾ Infinite Scaling
    Pay when the code is running

    View Slide

  10. SERVERLESS AXIOMS
    Event-Driven Architecture
    Stateless
    ♾ Infinite Scaling
    Pay when the code is running

    View Slide

  11. SERVERLESS
    UNDER
    THE
    MICROSCOPE

    View Slide

  12. LOG
    $ serverless invoke -f index -l

    View Slide

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

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

  15. SERVERLESS AXIOMS
    Event-Driven Architecture
    Stateless
    ♾ Infinite Scaling
    Pay when the code is running

    View Slide

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

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

  18. WHAT ABOUT
    CPU?

    View Slide

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

    View Slide

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

  21. (CPU, CST)
    ""<=>
    MEMORY
    http://bit.ly/2UZ7xDL
    Completely Fair Scheduler (CFS)

    View Slide

  22. https://aws.amazon.com/lambda/pricing/
    6$ "=> 28.8M/128mb!

    View Slide

  23. COST
    SPEED ⏫
    https://amzn.to/2Ix2t2O

    View Slide

  24. COST
    ""<=>
    LANGUAGE

    View Slide

  25. $ wrk -t10 -c400 -d20s -R40000
    $ curl http://endpoint.url/[ruby/go]
    "=> {“success”: true}
    VS
    CHALLENGE:

    View Slide

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

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

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

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

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

    View Slide

  31. WHEN YOU REWRITE EVERYTHING ON GO
    EXPECTATION REALITY

    View Slide

  32. View Slide

  33. FREE!
    Free Enterprise

    View Slide

  34. FREE!
    Free Enterprise
    LYING
    http://bit.ly/2lCt2dA

    View Slide

  35. PREDICTABLY

    View Slide

  36. PREDICTABLY

    View Slide

  37. PREDICTABLY

    View Slide

  38. http://serverlesscalc.com/
    $2400
    450M!!
    $918
    1GB

    View Slide

  39. BATTLE (128 MB)
    AVG ~1.8ms
    AVG ~80ms
    ❓❓❓❓❓❓❓❓❓
    ❓❓❓❓❓❓❓❓❓❓





    View Slide

  40. http://rubyonjets.com/

    View Slide

  41. RAILS
    ON
    SERVERLESS
    Tung Nguyen

    View Slide

  42. PURE RUBY LAMBDA
    # handler.rb
    require 'json'
    def heartbeat(event:, context:)
    { statusCode: 200, body: JSON.generate(success: true) }
    end

    View Slide

  43. BATTLE (128 MB)
    AVG ~39ms
    AVG ~80ms

    View Slide

  44. COST
    ""<=>
    DEPENDENCIES

    View Slide

  45. MINIMIZE THE COMPLEXITY OF
    YOUR DEPENDENCIES

    View Slide

  46. MINIMIZE THE COMPLEXITY OF
    YOUR DEPENDENCIES

    View Slide

  47. MINIMIZE THE COMPLEXITY OF
    YOUR DEPENDENCIES

    View Slide

  48. NO RAILS
    GOOD!

    View Slide

  49. CHEAPLY
    NICE
    NO RAILS
    GOOD!

    View Slide

  50. CHEAPLY
    NICE
    EVENTS
    AWESOME
    NO RAILS
    GOOD!

    View Slide

  51. CHEAPLY
    NICE
    EVENTS
    AWESOME
    NO RAILS
    GOOD!
    SCALING
    AMAZING

    View Slide

  52. TALK ABOUT
    THE
    PROBLEMS
    TELL THE TRUTH
    Sponsored by @stacey_om

    View Slide

  53. IS
    INFINITE
    SCALING
    ALWAYS
    GOOD?

    View Slide

  54. # before
    $ puma -t 8:32 -w 1
    # after
    $ puma -t 8:32 -w 2
    HOW RUBY DEV SCALES APP

    View Slide

  55. 500

    View Slide

  56. SERVERLESS AXIOMS
    Event-Driven Architecture
    Stateless
    ♾ Infinite Scaling
    Pay when the code is running

    View Slide

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

  58. EXECUTION CONTEXT
    # 1M symbols
    work = proc { 123_456_789 "** 123_456 }
    def heartbeat(event:, context:)
    { statusCode: 200, body: … }
    end

    View Slide

  59. EXECUTION CONTEXT
    # 1M symbols
    work = proc { 123_456_789 !** 123_456 }
    def heartbeat(event:, context:)
    value = work.() # not cached
    {
    statusCode: 200,
    body: JSON.generate(value: value)
    }
    end

    View Slide

  60. EXECUTION CONTEXT
    # 1M symbols
    work = proc { 123_456_789 !** 123_456 }
    value = work.() # cached
    def heartbeat(event:, context:)
    {
    statusCode: 200,
    body: JSON.generate(value: value)
    }
    end

    View Slide

  61. CONNECTION POOL
    pool = DB"::ConnectionPool.new()
    def heartbeat(event:, context:)
    connection = pool.get()
    # use connection
    connection.release()
    end

    View Slide

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

    View Slide

  63. SOLUTIONS*
    CLASSIC ($)

    View Slide

  64. SOLUTIONS*
    CLASSIC ($) MODERN ($$)
    Serverless

    View Slide

  65. SCALE

    DOWSCALE

    SCALE


    View Slide

  66. BURSTY TRAFFIC
    BURSTY LOAD
    BURSTY ???



    View Slide

  67. EXAMPLE
    ML

    View Slide

  68. EXAMPLE: ETL

    View Slide

  69. EXAMPLE: ETL

    View Slide

  70. EXAMPLE: BLOG

    View Slide

  71. GOTO 0;
    SERVERLESS
    IS RUBY
    FUTURE

    View Slide

  72. GOTO 0;
    SERVERLESS
    IS RUBY
    FUTURE

    View Slide

  73. DREAMS
    ABOUT
    SERVERLESS

    View Slide

  74. DREAMS
    ABOUT
    SERVERLESS
    Incredible
    Processing Power

    View Slide

  75. SERVERLESS
    Massive User Job

    View Slide

  76. SERVERLESS
    Backends
    Massive User Job
    DIFFERENT BACKENDS

    View Slide

  77. SERVERLESS
    Backends
    Massive User Job
    10_000.times do
    Thread.new(job)
    end

    View Slide

  78. SERVERLESS
    Backends
    Burst-Parallel Job

    View Slide

  79. SERVERLESS
    Burst-Parallel Job
    Video Encoding: VP8

    View Slide

  80. Burst-Parallel Job
    Video Encoding: Classic
    encode([, , …, ]) "-> keyframe + interframe(2:n)

    View Slide

  81. Burst-Parallel Job
    Video Encoding: Existing
    encode([, , …, ]) "-> keyframe + interframe(2:n)
    1MB +25kb * n

    View Slide

  82. Burst-Parallel Job
    Video Encoding: Existing
    encode([, , …, ]) "-> keyframe + interframe(2:n)
    decode(keyframe + interframe[2:n]) "-> [, , …, ]
    1MB +25kb * n

    View Slide

  83. Burst-Parallel Job
    Video Encoding: Existing
    encode(i[1:10]) "-> keyframe1 + interframe(2:10)
    encode(i[11:20]) "-> keyframe2 + interframe(12:20)
    encode(i[21:30]) "-> keyframe3 + interframe(22:30)
    1MB * N

    View Slide

  84. VIDEO ENCODING: ALFALA
    http://bit.ly/2lZFrbO

    View Slide

  85. Burst-Parallel Job
    preprocessor(compiler
    (assembler(linker
    (archiver(indexer
    (strip(source_file)…)
    Software Compilation: 7 Steps

    View Slide

  86. Burst-Parallel Job
    Software Compilation

    View Slide

  87. Burst-Parallel Job
    Software Compilation
    Video Encoding

    View Slide

  88. DIRECTED ACYCLIC GRAPH
    BRO NOT A BRO

    View Slide

  89. DIRECTED ACYCLIC GRAPH
    Problem: Different Nodes
    g
    f
    h
    m
    k
    l
    p
    r

    View Slide

  90. DAG "=> FUNCTIONS
    preprocessor(compiler
    (assembler(linker
    (archiver(indexer
    (strip(source_file)…)

    View Slide

  91. f(g(h(p(…)…)

    View Slide

  92. f(g(i)) !== λ(f, λ(g, i))
    THE NEW ABSTRACTION: λ

    View Slide

  93. f(g(i)) !== λ(f, λ(g, i))
    f = proc { |a| a + 1 }
    g = proc { |a| a + 2 }
    f.call(g.call(0))
    # "=> 3
    THE NEW ABSTRACTION: λ

    View Slide

  94. f(g(i)) !== λ(f, λ(g, i))
    f = proc { |a| a + 1 }
    g = proc { |a| a + 2 }
    f.call(g.call(0))
    # !=> 3
    λ = proc { |f, i| f.(i) }
    λ.call(f, λ.call(g, 0))
    # "=> 3
    THE NEW ABSTRACTION: λ

    View Slide

  95. f(g(h(p(…)…) !==
    λ(f, λ(g, λ(h, λ(p(…)…)

    View Slide

  96. f(g(h(p(…)…) !==
    λ(f, λ(g, λ(h, λ(p(…)…)
    λ(ƒ,arg) !=> arg
    WHERE

    View Slide

  97. λ(ƒ,arg) !=> arg

    View Slide

  98. λ(ƒ,arg) !=> arg
    λ(ƒ,arg) "=> Θ,arg

    View Slide

  99. λ(ƒ,arg) !=> arg
    λ(ƒ,arg) !=> Θ,arg
    λ(T) "=> T

    View Slide

  100. DIRECTED ACYCLIC GRAPH
    T2
    T1
    T3
    T5
    T4
    T6
    T7
    T8
    λ(T) "=> T, T{i} "<- T

    View Slide

  101. SERVERLESS
    Backends
    Burst-Parallel Job

    View Slide

  102. SERVERLESS
    Backends
    Burst-Parallel Job
    λ(T) "=> T

    View Slide

  103. SERVERLESS
    Backends
    Burst-Parallel Job
    λ(T) "=> T
    λ

    View Slide

  104. SERVERLESS
    Backends
    Burst-Parallel Job
    λ(T) "=> T
    λ
    IR
    T

    View Slide

  105. SERVERLESS
    Backends
    Burst-Parallel Job
    IR
    λ(T) "=> T
    T
    λ
    STATIC

    View Slide

  106. SERVERLESS
    Backends
    Burst-Parallel Job
    λ
    IR
    T
    Frontends
    T generator
    λ(T) "=> T

    View Slide

  107. SERVERLESS
    Backends
    Burst-Parallel Job
    λ
    IR
    T
    Frontends
    T generator
    λ(T) "=> T

    View Slide

  108. Burst-Parallel Job
    IR
    Frontends
    Coordinator
    HTTP
    λ

    View Slide

  109. SERVERLESS
    Backend
    Burst-Parallel Job
    IR
    Frontends
    Coordinator
    STATELESS
    HTTP

    View Slide

  110. Burst-Parallel Job
    IR
    Frontends
    Coordinator
    S3
    HTTP
    λ

    View Slide

  111. THE FUTURE
    IS HERE!

    View Slide

  112. gg
    Backends
    Burst-Parallel Job
    gg IR
    Frontends
    gg
    Coordinator
    C++
    HTTP
    ProtoBuf
    S3
    Stanford Systems
    and
    Networking Research

    View Slide

  113. Fib(4)
    fib(4)
    fib(3) fib(2)
    fib(2) fib(1)
    fib(1) fib(0)
    fib(1) fib(0)
    +
    +
    +
    +

    View Slide

  114. Fib(4)
    fib(4)
    fib(3) fib(2)
    fib(2) fib(1)
    fib(1) fib(0)
    fib(1) fib(0)
    +
    +
    +
    +
    Hash#fib1
    Hash#fib2

    View Slide

  115. Нихуя не понял
    Но очень интересно!
    чего

    View Slide

  116. gg FRAMEWORK
    https://stanford.io/2mtmiiH

    View Slide

  117. DEMO

    View Slide

  118. DEMO
    http://bit.ly/2m0G3Oo
    https://asciinema.org/a/270154

    View Slide

  119. RESULTS
    gg
    real
    user
    sys
    1m17.670s
    1m49.040s
    0m7.820s
    real
    user
    sys
    0m43.354s
    0m3.830s
    0m5.490s
    real
    user
    sys
    0m29.376s
    0m0.950s
    0m1.400s

    View Slide

  120. RESULTS
    gg
    real
    user
    sys
    1m17.670s
    1m49.040s
    0m7.820s
    real
    user
    sys
    0m43.354s
    0m3.830s
    0m5.490s
    real
    user
    sys
    0m29.376s
    0m0.950s
    0m1.400s

    View Slide

  121. RESULTS
    gg
    Total: 1m56s Total: 0m38s
    ~X3 faster

    View Slide

  122. COMPARISON OF COLD-CACHE BUILD TIMES
    https://cs.stanford.edu/~matei/papers/2019/usenix_atc_gg.pdf

    View Slide

  123. DARK
    MAGIC!

    View Slide

  124. https://github.com/StanfordSNR/gg

    View Slide

  125. GOTO 0;
    SERVERLESS
    IS RUBY
    FUTURE

    View Slide

  126. “As a computing substrate, we suspect cloud
    functions are in a similar position to Graphics
    Processing Units in the 2000s.
    At the time, GPUs were designed solely for 3D
    graphics, but the community gradually
    recognized that they had become programmable
    enough to execute some parallel algorithms
    unrelated to graphics.” (c)

    View Slide

  127. GOTO 0;
    SERVERLESS
    IS RUBY
    FUTURE

    View Slide

  128. SORBET?

    View Slide

  129. IT saṃsāra
    Java
    Erlang
    C
    C++
    "=> Scala
    "=> Elixir
    "=> Go
    "=> Rust

    View Slide

  130. RUBY
    SELF-IDENTIFICATION

    View Slide

  131. RUBY IS
    Fast
    Coding

    View Slide

  132. RUBY IS
    Startups
    (Rails)
    Fast
    Coding

    View Slide

  133. RUBY IS
    Startups
    (Rails)
    GIL
    Sensitive
    Fast
    Coding

    View Slide

  134. RUBY IS
    Startups
    (Rails)
    オブジェクト指向スクリプト⾔言語 Ruby
    GIL
    Sensitive
    Fast
    Coding

    View Slide

  135. RUBY IS
    Startups
    (Rails)
    GIL
    Sensitive
    The Object-oriented
    Scripting Language Ruby
    Fast
    Coding

    View Slide

  136. RUBY IS
    Startups
    (Rails)
    GIL
    Sensitive
    The Object-oriented
    Scripting Language Ruby
    Fast
    Coding

    View Slide

  137. RUBY IS
    Startups
    (Rails)
    GIL
    Sensitive
    The Object-oriented
    Scripting Language Ruby
    Fast
    Coding

    View Slide

  138. RUBY IS
    Startups
    (Rails)
    GIL
    Sensitive
    The Object-oriented
    Scripting Language Ruby
    Fast
    Coding

    View Slide

  139. RUBY IS
    Startups
    (Rails)
    GIL
    Sensitive
    The Object-oriented
    Scripting Language Ruby
    Fast
    Coding

    View Slide

  140. 60 SECNDS
    OF SUMMARY

    View Slide

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

    View Slide