[RubyConf 2018] High-Speed Cables for Ruby

52cc8a838bf44a589d2572833b2dd1b9?s=47 Vlad Dem
November 16, 2018

[RubyConf 2018] High-Speed Cables for Ruby

52cc8a838bf44a589d2572833b2dd1b9?s=128

Vlad Dem

November 16, 2018
Tweet

Transcript

  1. HIGH-SPEED CABLES FOR RUBY Vladimir Dementyev @evilmartians

  2. None
  3. CABLES Tools for building real-time applications

  4. palkan_tula palkan RubyConf 2018 The real-time web is a web

    … that enable users to receive information as soon as it is published by its authors, rather than … check a source periodically for updates. 4 * https://en.wikipedia.org/wiki/Real-time_web BORING DEFINITION
  5. palkan_tula palkan RubyConf 2018 5 Request-Response REAL-TIME

  6. palkan_tula palkan RubyConf 2018 5 Request-Response REAL-TIME

  7. palkan_tula palkan RubyConf 2018 6 Bi-Directional Persistent REAL-TIME

  8. palkan_tula palkan RubyConf 2018 6 Bi-Directional Persistent REAL-TIME

  9. palkan_tula palkan RubyConf 2018 7 The opening of the first

    McDonalds in Russia, Moscow, 1991 REAL-TIME Limited number of concurrent requests (request queue)
  10. palkan_tula palkan RubyConf 2018 8 Large number of concurrent clients

    REAL-TIME Opening gala, Millennium Biltmore, 1923
  11. palkan_tula palkan RubyConf 2018 EXAMPLES 9 Messaging (i.e. chats) Notifications

    Live updates Online games Other
  12. palkan_tula palkan RubyConf 2018 WEBSOCKETS 10

  13. palkan_tula palkan RubyConf 2018 CONCURRENCY 11

  14. © https://twitter.com/erneestoc/status/974485805770072064

  15. palkan_tula palkan RubyConf 2018 THE RED RUBY PILL? 13

  16. palkan_tula palkan RubyConf 2018 14 Vladimir Dementyev

  17. palkan_tula palkan RubyConf 2018 15 @palkan @palkan_tula Vladimir Dementyev 789

    645 330 415
  18. palkan_tula palkan RubyConf 2018 ACTION CABLE TESTING https://github.com/rails/rails/pull/33659 16

  19. palkan_tula palkan RubyConf 2018 17 TEAM LID Team Me

  20. palkan_tula palkan RubyConf 2018 18

  21. palkan_tula palkan RubyConf 2018 19

  22. palkan_tula palkan RubyConf 2018 evilmartians.com 20

  23. None
  24. palkan_tula palkan RubyConf 2018 evilmartians.com/blog 22

  25. palkan_tula palkan RubyConf 2018 23 evilmartians.com Brooklyn, NY

  26. THIS TALK

  27. palkan_tula palkan RubyConf 2018 THE TALK 25 What is “cable”?

    ✅ The tale of Action Cable, Rack & Ruby 3x3 From Action to Any: another point of view
  28. RUBY & CABLES RailsConf 2015

  29. palkan_tula palkan RubyConf 2018 IN A NUTSHELL 27 Server Client

    WebSocket Client WebSocket
  30. palkan_tula palkan RubyConf 2018 IN A NUTSHELL 27 Server Client

    WebSocket Client WebSocket stream C Broadcaster stream B stream A stream B
  31. palkan_tula palkan RubyConf 2018 IN A NUTSHELL 27 Server Client

    WebSocket Client WebSocket stream C Broadcaster stream B stream A stream B channel X channel Y channel Z
  32. palkan_tula palkan RubyConf 2018 CHANNELS 28 class AnswersChannel < ApplicationCable

    ::Channel def subscribed reject_subscription unless current_user.admin? end def follow(params) stream_from "questions/ #{params['id']}" end end
  33. palkan_tula palkan RubyConf 2018 CHAT IN 5MIN 29

  34. palkan_tula palkan RubyConf 2018 CHAT IN 5MIN 29

  35. palkan_tula palkan RubyConf 2018 BENCHMARKS 30 TODAY’S SPECIALS HashRocket RTT

    $9.99 RAM (all-you-can-eat) $19.59 CPU Steak 8’’ $20.18
  36. palkan_tula palkan RubyConf 2018 SHOOTOUT 31 Client Server broadcast to

    all message send message back https://hashrocket.com/blog/posts/websocket-shootout
  37. palkan_tula palkan RubyConf 2018 SHOOTOUT 32 Broadcast RTT 0,0s 1,2s

    2,4s 3,6s 4,8s 6,0s 7,2s 8,4s 9,6s 10,8s 12,0s Number of connections 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Go Erlang Action Cable (8x) https://github.com/anycable/anycable/tree/master/benchmarks
  38. None
  39. CABLE THEOREM Action Cable Low Latency Crowded Streams Low Latency

    Action Cable
  40. High Resources Usage CABLE THEOREM Action Cable Low Latency Crowded

    Streams Low Latency Action Cable
  41. palkan_tula palkan RubyConf 2018 CPU 36 * running WebSocket shootout

  42. palkan_tula palkan RubyConf 2018 CPU 36 * running WebSocket shootout

  43. palkan_tula palkan RubyConf 2018 MEMORY 37 RSS 0MB 160MB 320MB

    480MB 640MB 800MB 960MB 1120MB 1280MB 1440MB 1600MB Number of connections 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Go Erlang Action Cable (8x)
  44. WHAT ABOUT REAL LIFE? …or don’t trust benchmarks I WANT

    TO BELIEVE IN BENCHMARKS
  45. palkan_tula palkan RubyConf 2018 39 “With Action Cable, we could

    easily have 20+ 1GB dynos running during the weekends, with every growing memory” –Jon Stenqvist, CEO, Equipe equipe.com NOTE: having ~5-10k conns at peak time
  46. WHYYYY???!!

  47. palkan_tula palkan RubyConf 2018 ACTION CABLE 41 rack.hijack

  48. palkan_tula palkan RubyConf 2018 rack.hijack 42 Server Application

  49. palkan_tula palkan RubyConf 2018 rack.hijack 42 Server Application IO

  50. palkan_tula palkan RubyConf 2018 rack.hijack 42 Server Application

  51. palkan_tula palkan RubyConf 2018 rack.hijack 42 Server Application IO

  52. palkan_tula palkan RubyConf 2018 THE hijack PRICE 43 https://bowild.wordpress.com/2018/05/01/rubys-rack-push-decoupling-the-real-time-web-application-from-the-web/ –Bo

    “Due to code duplication and extra work, the memory consumption for hijack based solutions is higher and their performance is slower (more system calls, more context switches, etc’)..” https://bowild.wordpress.com/2018/05/01/rubys-rack-push-decoupling-the-real-time-web-application-from-the-web/
  53. palkan_tula palkan RubyConf 2018 THE hijack PRICE 44 Separate IO

    loop (server) WebSockets protocol implementation overhead
  54. palkan_tula palkan RubyConf 2018 plezi.io github.com/boazsegev/iodine WHO IS BO? 45

    Boaz Segev (Bo) musician (now), developer (past)
  55. palkan_tula palkan RubyConf 2018 Broadcast RTT 0,0s 1,2s 2,4s 3,6s

    4,8s 6,0s 7,2s 8,4s 9,6s 10,8s 12,0s Number of connections 1000 3000 5000 7000 9000 Go Iodine Action Cable (8x) IODINE 46 https://github.com/anycable/anycable/tree/master/benchmarks RSS 0MB 160MB 320MB 480MB 640MB 800MB 960MB 1120MB 1280MB 1440MB 1600MB Number of connections 1000 3000 5000 7000 9000 Go Iodine Action Cable (8x)
  56. palkan_tula palkan RubyConf 2018 RACK API: FUTURE 47 https://github.com/rack/rack/pull/1272

  57. palkan_tula palkan RubyConf 2018 RACK API: FUTURE 48 rack.hijack Direct

    access to IO (socket) rack.upgrade Public interface to “abstracted” IO managed by web server
  58. palkan_tula palkan RubyConf 2018 RACK API IN RAILS 49 https://github.com/rails/rails/pull/33295

  59. palkan_tula palkan RubyConf 2018 RACK API IN RAILS 49 https://github.com/rails/rails/pull/33295

    NO PERFORMANCE IMPROVEMENTS
  60. palkan_tula palkan RubyConf 2018 ACTION CABLE 50 rack.hijack Long-lived objects

  61. palkan_tula palkan RubyConf 2018 ALLOCATIONS 51 Retained objects for one

    connection: websocket ~40 actioncable ~640 other ~100 ~60kB
  62. palkan_tula palkan RubyConf 2018 ACTION CABLE 52 rack.hijack Long-lived objects

    Heap fragmentation
  63. palkan_tula palkan RubyConf 2018 HEAPY HEAPY SHAKE 53

  64. palkan_tula palkan RubyConf 2018 HEAPY HEAPY SHAKE 53 Heap fragmentation?

    Are you sure?
  65. RUBY 3X3 The future we’re waiting for

  66. None
  67. palkan_tula palkan RubyConf 2018 56 GENERATIONAL HYPOTHESIS

  68. palkan_tula palkan RubyConf 2018 RUBY 3X3 57 Compacting GC

  69. palkan_tula palkan RubyConf 2018 RUBY 3X3 58 Compacting GC Guilds

  70. palkan_tula palkan RubyConf 2018 RUBY DOLL v2 59 Thread Fiber

  71. palkan_tula palkan RubyConf 2018 WHAT IS FIBER? https://twitter.com/avdi/status/1057256814721556482 60

  72. palkan_tula palkan RubyConf 2018 RUBY DOLL v2 61 Thread Fiber

    1MB 128KB
  73. palkan_tula palkan RubyConf 2018 RUBY DOLL v3 62 http://www.atdot.net/~ko1/activities/2018_rubyconf2018.pdf Thread

    Fiber Guild 1MB 128KB
  74. palkan_tula palkan RubyConf 2018 –Julian Coolesh (@raventid) “Guilds are for

    parallelism, not for concurrency.” 63
  75. palkan_tula palkan RubyConf 2018 RUBY 3X3 64 Compacting GC Guilds

    Dynamic thread stack size https://rubykaigi.org/2018/presentations/sugiyama-k.html
  76. palkan_tula palkan RubyConf 2018 RUBY DOLL v3 65 Thread Threadlet

    Guild Fiber https://bugs.ruby-lang.org/issues/13618
  77. palkan_tula palkan RubyConf 2018 RUBY 3X3 66 Compacting GC Guilds

    Dynamic thread stack size Threadlets
  78. palkan_tula palkan RubyConf 2018 RUBY 3X3 66 Compacting GC Guilds

    Dynamic thread stack size Threadlets
  79. palkan_tula palkan RubyConf 2018 binding.pry 67

  80. palkan_tula palkan RubyConf 2018 CABLE RACE 68

  81. palkan_tula palkan RubyConf 2018 CABLE RACE 69

  82. AnyCable One cable to rule them all

  83. None
  84. palkan_tula palkan RubyConf 2018 Client Channels Broadcaster Server ACTION CABLE

    72
  85. palkan_tula palkan RubyConf 2018 Action Cable 73 Client Channels Broadcaster

    Server AnyCable
  86. palkan_tula palkan RubyConf 2018 AnyCable 74 ? WS Server

  87. palkan_tula palkan RubyConf 2018 75 https://grpc.io gRPC

  88. palkan_tula palkan RubyConf 2018 76 https://grpc.io gRPC = universal RPC

    framework
  89. palkan_tula palkan RubyConf 2018 77 https://grpc.io gRPC = HTTP/2 +

    protobuf
  90. palkan_tula palkan RubyConf 2018 AnyCable 78 syntax = "proto3"; package

    anycable; service RPC { rpc Connect (ConnectionRequest) returns (ConnectionResponse) {} rpc Command (CommandMessage) returns (CommandResponse) {} rpc Disconnect (DisconnectRequest) returns (DisconnectResponse) {} } https://docs.anycable.io/#/rpc_proto
  91. palkan_tula palkan RubyConf 2018 AnyCable 79 Go WS

  92. None
  93. palkan_tula palkan RubyConf 2018 AnyCable 81 anycable-go erlycable WebSocket Servers

  94. palkan_tula palkan RubyConf 2018 Broadcast RTT 0,0s 1,2s 2,4s 3,6s

    4,8s 6,0s 7,2s 8,4s 9,6s 10,8s 12,0s Number of connections 1000 3000 5000 7000 9000 AnyCable-Go Iodine Action Cable (8x) AnyCable 82 https://github.com/anycable/anycable/tree/master/benchmarks RSS 0MB 160MB 320MB 480MB 640MB 800MB 960MB 1120MB 1280MB 1440MB 1600MB Number of connections 1000 3000 5000 7000 9000 AnyCable-Go Iodine Action Cable (8x)
  95. palkan_tula palkan RubyConf 2018 CPU 83 anycable-go erlycable actioncable

  96. palkan_tula palkan RubyConf 2018 CPU 83 anycable-go erlycable actioncable

  97. palkan_tula palkan RubyConf 2018 equipe.com 84 ActionCable: 20+ 2X (1GB)

    dynos AnyCable: 4 X (0.5GB) dynos $1000 $100
  98. palkan_tula palkan RubyConf 2018 SHORT-LIVED OBJECTS 85 WS RPC connect

    identifiers (JSON + GlobalID) command (subscribe/perform) with identifiers transmissions, streams to subscribe
  99. palkan_tula palkan RubyConf 2018 AnyCable 86 Go WS Bottleneck?

  100. palkan_tula palkan RubyConf 2018 gRPC RPS 87 RPC type \

    Concurrency 1 10 50 AnyCable RPC 1600 2200 2400 Noop RPC with connection object building* 1500 1900 2300 Noop RPC 3000 4600 5500 * Only build Action Cable connection object without performing an action https://github.com/anycable/anycable/blob/master/benchmarks/2018-05-27-rpc-bench.md
  101. palkan_tula palkan RubyConf 2018 MRUBY CACHE https://rubykaigi.org/2018/presentations/palkan_tula.html 88

  102. palkan_tula palkan RubyConf 2018 PLUG-N-PLAY 89 # Gemfile gem 'anycable-rails'

    # config/cable.yml production: type: :any_cable # config/environments/production.rb config.action_cable.url = 'ws: //example.com:3334' # install WebSocket server $ brew install anycable/anycable/anycable-go $ bundle exec anycable # => Run gRPC server $ anycable-go --port=3334 # => Run WebSocket server
  103. palkan_tula palkan RubyConf 2018 MORE FEATURES 90 Zero-disconnect deployment

  104. palkan_tula palkan RubyConf 2018 DISCONNECT 91 WebSocket Server App App

    gRPC App App App App Envoy Proxy * https://www.envoyproxy.io
  105. palkan_tula palkan RubyConf 2018 MORE FEATURES 92 Zero-disconnect deployment Metrics

    & Stats
  106. palkan_tula palkan RubyConf 2018 METRICS 93

  107. palkan_tula palkan RubyConf 2018 MORE FEATURES 94 Zero-disconnect deployment Metrics

    & Stats Rails-free
  108. LiteCable github.com/palkan/litecable Rails No More

  109. palkan_tula palkan RubyConf 2018 MORE FEATURES 96 Zero-disconnect deployment Metrics

    & Stats Rails-free NEW Supports GraphQL subscriptions
  110. palkan_tula palkan RubyConf 2018 GRAPHQL ANYCABLE 97 github.com/Envek/graphql-anycable

  111. palkan_tula palkan RubyConf 2018 github.com/anycable https://anycable.io AnyCable 98

  112. palkan_tula palkan RubyConf 2018 docs.anycable.io 99

  113. palkan_tula palkan RubyConf 2018 anycable.io/pro 100

  114. RUBY OR NOT © RailsClub Moscow

  115. palkan_tula palkan RubyConf 2018 RUBY OR NOT 102 Real-time in

    Ruby is possible… …but more efficient with the help of other languages …and could become much better with Ruby 3
  116. THANK YOU! Vladimir Dementyev anycable.io evilmartians.com @palkan @palkan_tula @evilmartians