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

[Wroc_Love.rb 2018] Cables! Cables! Cables!

[Wroc_Love.rb 2018] Cables! Cables! Cables!

Vladimir Dementyev

March 17, 2018
Tweet

More Decks by Vladimir Dementyev

Other Decks in Programming

Transcript

  1. CABLES! CABLES!
    CABLES!
    Vladimir Dementyev

    View Slide

  2. Vladimir
    Dementyev

    View Slide

  3. ! Moscow

    # Wrocław

    View Slide

  4. @palkan
    @palkan_tula

    View Slide

  5. View Slide

  6. EVIL MARTIANS

    View Slide

  7. EVIL MARTIANS

    View Slide

  8. View Slide

  9. WHY I’M HERE

    View Slide

  10. THE TALK
    Action Cable pros & cons
    The tale of Action Cable
    Alternative cables for Rails and
    non-Rails

    View Slide

  11. CABLES
    Tools for building
    real-time applications
    Intro

    View Slide

  12. REAL-TIME
    Messaging (i.e. chats)
    Notifications
    Live updates
    Online games
    Other

    View Slide

  13. RUBY CABLES
    *based on https://www.ruby-toolbox.com/categories/HTTP_Pub_Sub

    View Slide

  14. RUBY CABLES

    View Slide

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

    View Slide

  16. THE RED RUBY PILL?

    View Slide

  17. Part 1
    RailsConf 2015
    ACTION
    CABLE

    View Slide

  18. IN A NUTSHELL
    Server
    Client
    WebSocket
    Client
    WebSocket

    View Slide

  19. IN A NUTSHELL
    Server
    Client
    WebSocket
    Client
    WebSocket
    stream C
    Broadcaster
    stream B
    stream A
    stream B

    View Slide

  20. IN A NUTSHELL
    Server
    Client
    WebSocket
    Client
    WebSocket
    stream C
    Broadcaster
    stream B
    stream A
    stream B
    channel X
    channel Y
    channel Z

    View Slide

  21. PROS
    &
    CONS

    View Slide

  22. CABLE PROS
    Zero-configuration

    View Slide

  23. CABLE PROS
    Zero-configuration
    Channels framework

    View Slide

  24. CHANNELS
    class AnswersChannel < ApplicationCable ::Channel
    def subscribed
    reject_subscription unless current_user.admin?
    end
    def follow(params)
    stream_from "questions/ #{params['id']}"
    end
    end

    View Slide

  25. CABLE PROS
    Zero-configuration
    Channels framework
    JS client that just works

    View Slide

  26. CABLE CONS

    View Slide

  27. CABLE CONS
    Lack of transport fallbacks

    View Slide

  28. WEBSOCKETS
    http://caniuse.com/#search=websockets

    View Slide

  29. CABLE CONS
    Lack of transport fallbacks
    Memory usage

    View Slide

  30. MEMORY
    20k idle connections
    MB
    0
    200
    400
    600
    800
    1 000
    1 200
    1 400
    1 600
    Go Erlang Action Cable (8x)

    View Slide

  31. CABLE PROS
    Lack of transport fallbacks
    Memory usage
    Real-time performance

    View Slide

  32. SHOOTOUT
    https://hashrocket.com/blog/posts/websocket-shootout

    View Slide

  33. SHOOTOUT
    Client
    Server
    broadcast
    to all
    message
    send message back

    View Slide

  34. SHOOTOUT
    Broadcast RTT
    0,0s
    0,8s
    1,6s
    2,4s
    3,2s
    4,0s
    4,8s
    5,6s
    6,4s
    7,2s
    8,0s
    Number of connections
    1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
    Go Erlang Action Cable (8x) Action Cable (2x)
    See https://github.com/anycable/anycable/tree/master/benchmarks

    View Slide

  35. View Slide

  36. Action
    Cable
    Low
    Latency
    Crowded
    Channels
    CABLE
    THEOREM

    View Slide

  37. CABLE CONS
    Lack of transport fallbacks
    Memory usage
    Real-time performance
    CPU usage

    View Slide

  38. CPU

    View Slide

  39. CPU

    View Slide

  40. WHAT
    ABOUT
    REAL
    LIFE?
    I WANT TO
    BELIEVE IN
    BENCHMARKS

    View Slide

  41. Part 2
    THE
    TALE OF
    ACTION
    CABLE
    © http://www.equipe.com

    View Slide

  42. View Slide

  43. View Slide

  44. –Jon Stenqvist, CEO, Equipe
    “We've used all kind of
    push solutions over the
    years, the latest is faye,
    pusher, actioncable and X.”

    View Slide

  45. EQUIPE
    “With Action Cable, we could
    easily have 20+ 1GB dynos
    running during the weekends,
    with every growing memory”

    View Slide

  46. EQUIPE
    “After switching to X, we have 4
    512MB dynos, very stable at
    ~200MB memory usage”

    View Slide

  47. EQUIPE
    ActionCable: 20+ 2X (1GB) dynos X: 4 X (0.5GB) dynos

    View Slide

  48. EQUIPE
    ActionCable: 20+ 2X (1GB) dynos X: 4 X (0.5GB) dynos
    $1000 $100

    View Slide

  49. View Slide

  50. X = AnyCable

    View Slide

  51. Part 3
    ANYCABLE

    View Slide

  52. ANYCABLE

    View Slide

  53. Client (protocol)
    Channels
    Streams
    Server
    ACTION CABLE

    View Slide

  54. Client (protocol)
    Channels
    Streams
    Server
    ACTION CABLE

    View Slide

  55. ACTION CABLE
    Client (protocol)
    Channels
    Streams
    Server
    ?

    View Slide

  56. ACTION CABLE
    Client (protocol)
    Channels
    Streams
    Server
    ANYCABLE

    View Slide

  57. gRPC
    https://grpc.io

    View Slide

  58. gRPC =
    Google RPC
    https://grpc.io

    View Slide

  59. gRPC =
    universal
    RPC
    framework
    https://grpc.io

    View Slide

  60. gRPC =
    HTTP/2 +
    protobuf
    https://grpc.io

    View Slide

  61. ANYCABLE
    Go WS

    View Slide

  62. View Slide

  63. ANYCABLE SERVERS
    anycable-go
    erlycable

    View Slide

  64. CPU
    anycable-go erlycable action_cable

    View Slide

  65. CPU
    anycable-go erlycable action_cable

    View Slide

  66. SHOOTOUT
    Broadcast RTT
    0,0s
    0,8s
    1,6s
    2,4s
    3,2s
    4,0s
    4,8s
    5,6s
    6,4s
    7,2s
    8,0s
    Number of connections
    1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
    anycable-go erlycable Action Cable (8x)
    See https://github.com/anycable/anycable/tree/master/benchmarks

    View Slide

  67. PLUG-N-PLAY
    gem 'anycable-rails', group: :production
    rails generate anycable
    config.action_cable.url = ‘ws: //example.com:3334'
    ./bin/anycable # => Run RPC server
    brew install anycable/anycable/anycable-go
    anycable-go -p 3334 # => Run WebSocket server

    View Slide

  68. COMPATIBILITY
    Feature Status
    Connection Identifiers +
    Connection Request (cookies, params) +
    Disconnect Handling +
    Subscribe to channels +
    Parameterized subscriptions +
    Unsubscribe from channels +
    Subscription Instance Variables -
    Performing Channel Actions +
    Streaming +
    Remote Disconnect wip (planned for 0.6.0)
    Custom stream callbacks -
    Broadcasting +

    View Slide

  69. COMPATIBILITY
    https://medium.com/@leshchuk/from-action-to-any-1e8d863dd4cf

    View Slide

  70. MORE
    FEATURES

    View Slide

  71. MORE FEATURES
    Zero-disconnect deployment

    View Slide

  72. DISCONNECT
    Client Re-connect & Re-subscribe
    Client Disconnected ActionCable
    ActionCable

    View Slide

  73. DISCONNECT
    WebSocket Server
    Client Connected
    Client Connected App
    App
    RPC
    RPC

    View Slide

  74. DISCONNECT
    WebSocket Server
    App
    App
    gRPC
    App
    App
    App
    App
    Envoy Proxy
    * https://www.envoyproxy.io

    View Slide

  75. MORE FEATURES
    Zero-disconnect deployment
    Metrics & Stats

    View Slide

  76. METRICS

    View Slide

  77. http://anycable.io

    View Slide

  78. http://anycable.io

    View Slide

  79. github.com/anycable
    @any_cable
    ANYCABLE

    View Slide

  80. Part 4
    BEYOND
    RAILS
    © Призрачная Колыма

    View Slide

  81. NON-RAILS

    View Slide

  82. github.com/palkan/litecable
    LITE CABLE

    View Slide

  83. Rails-free Action Cable
    No deps (even ActiveSupport)
    Compatible with AnyCable
    Compatible with Action Cable clients
    LITE CABLE

    View Slide

  84. module Chat
    class Channel < LiteCable ::Channel ::Base
    identifier :chat
    def subscribed
    stream_from "chat_ #{chat_id}"
    end
    end
    end
    LITE CABLE

    View Slide

  85. run Rack ::Builder.new do
    map '/cable' do
    use LiteCable ::Server ::Middleware,
    connection_class: Chat ::Connection
    run proc { |_| [200, {}, ['']] }
    end
    end
    LITE CABLE

    View Slide

  86. HANAMI CABLE
    http://gabrielmalakias.com.br/ruby/hanami/iot/2017/05/26/websockets-connecting-litecable-to-hanami.html

    View Slide

  87. PLEZI CABLE

    View Slide

  88. PLEZI
    Iodine–fast concurrent web server for
    real-time Ruby applications
    Rack-compatible
    CPU efficient
    Less memory than Puma/Passenger
    Stable?

    View Slide

  89. plezi.io
    github.com/boazsegev/iodine
    PLEZI

    View Slide

  90. Outro
    RUBY
    OR NOT
    © RailsClub Moscow

    View Slide

  91. RUBY OR NOT
    WebSockets in Ruby are possible…

    View Slide

  92. RUBY OR NOT
    WebSockets in Ruby are possible…
    …but more efficient with the help of
    other languages

    View Slide

  93. ActionCable is usable…
    RUBY OR NOT

    View Slide

  94. ActionCable is usable…
    …and fixable/replaceable if
    necessary
    RUBY OR NOT

    View Slide

  95. DZIĘKUJĘ!
    Vladimir Dementyev
    anycable.io
    evilmartians.com
    @palkan
    @palkan_tula

    View Slide