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 full-size slide

  2. Vladimir
    Dementyev

    View full-size slide

  3. ! Moscow

    # Wrocław

    View full-size slide

  4. @palkan
    @palkan_tula

    View full-size slide

  5. EVIL MARTIANS

    View full-size slide

  6. EVIL MARTIANS

    View full-size slide

  7. WHY I’M HERE

    View full-size slide

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

    View full-size slide

  9. CABLES
    Tools for building
    real-time applications
    Intro

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. THE RED RUBY PILL?

    View full-size slide

  14. Part 1
    RailsConf 2015
    ACTION
    CABLE

    View full-size slide

  15. IN A NUTSHELL
    Server
    Client
    WebSocket
    Client
    WebSocket

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. CABLE PROS
    Zero-configuration

    View full-size slide

  19. CABLE PROS
    Zero-configuration
    Channels framework

    View full-size slide

  20. 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 full-size slide

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

    View full-size slide

  22. CABLE CONS
    Lack of transport fallbacks

    View full-size slide

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

    View full-size slide

  24. CABLE CONS
    Lack of transport fallbacks
    Memory usage

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  29. 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 full-size slide

  30. Action
    Cable
    Low
    Latency
    Crowded
    Channels
    CABLE
    THEOREM

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  39. X = AnyCable

    View full-size slide

  40. Part 3
    ANYCABLE

    View full-size slide

  41. Client (protocol)
    Channels
    Streams
    Server
    ACTION CABLE

    View full-size slide

  42. Client (protocol)
    Channels
    Streams
    Server
    ACTION CABLE

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  45. gRPC
    https://grpc.io

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  49. ANYCABLE
    Go WS

    View full-size slide

  50. ANYCABLE SERVERS
    anycable-go
    erlycable

    View full-size slide

  51. CPU
    anycable-go erlycable action_cable

    View full-size slide

  52. CPU
    anycable-go erlycable action_cable

    View full-size slide

  53. 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 full-size slide

  54. 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 full-size slide

  55. 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 full-size slide

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

    View full-size slide

  57. MORE
    FEATURES

    View full-size slide

  58. MORE FEATURES
    Zero-disconnect deployment

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  62. MORE FEATURES
    Zero-disconnect deployment
    Metrics & Stats

    View full-size slide

  63. http://anycable.io

    View full-size slide

  64. http://anycable.io

    View full-size slide

  65. github.com/anycable
    @any_cable
    ANYCABLE

    View full-size slide

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

    View full-size slide

  67. github.com/palkan/litecable
    LITE CABLE

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  74. Outro
    RUBY
    OR NOT
    © RailsClub Moscow

    View full-size slide

  75. RUBY OR NOT
    WebSockets in Ruby are possible…

    View full-size slide

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

    View full-size slide

  77. ActionCable is usable…
    RUBY OR NOT

    View full-size slide

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

    View full-size slide

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

    View full-size slide