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

[RubyConf 2018] High-Speed Cables for Ruby

[RubyConf 2018] High-Speed Cables for Ruby

Vladimir Dementyev

November 16, 2018
Tweet

More Decks by Vladimir Dementyev

Other Decks in Programming

Transcript

  1. 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
  2. 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)
  3. palkan_tula palkan RubyConf 2018 8 Large number of concurrent clients

    REAL-TIME Opening gala, Millennium Biltmore, 1923
  4. 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
  5. palkan_tula palkan RubyConf 2018 IN A NUTSHELL 27 Server Client

    WebSocket Client WebSocket stream C Broadcaster stream B stream A stream B
  6. 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
  7. 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
  8. 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
  9. palkan_tula palkan RubyConf 2018 SHOOTOUT 31 Client Server broadcast to

    all message send message back https://hashrocket.com/blog/posts/websocket-shootout
  10. 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
  11. 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)
  12. 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
  13. 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/
  14. palkan_tula palkan RubyConf 2018 THE hijack PRICE 44 Separate IO

    loop (server) WebSockets protocol implementation overhead
  15. 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)
  16. 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
  17. palkan_tula palkan RubyConf 2018 ALLOCATIONS 51 Retained objects for one

    connection: websocket ~40 actioncable ~640 other ~100 ~60kB
  18. palkan_tula palkan RubyConf 2018 RUBY 3X3 64 Compacting GC Guilds

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

    Guild Fiber https://bugs.ruby-lang.org/issues/13618
  20. 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
  21. 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)
  22. palkan_tula palkan RubyConf 2018 SHORT-LIVED OBJECTS 85 WS RPC connect

    identifiers (JSON + GlobalID) command (subscribe/perform) with identifiers transmissions, streams to subscribe
  23. 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
  24. 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
  25. palkan_tula palkan RubyConf 2018 DISCONNECT 91 WebSocket Server App App

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

    & Stats Rails-free NEW Supports GraphQL subscriptions
  27. 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