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

[RubyConf Malaysia 2017] AnyCable

[RubyConf Malaysia 2017] AnyCable

Vladimir Dementyev

October 12, 2017
Tweet

More Decks by Vladimir Dementyev

Other Decks in Programming

Transcript

  1. ANYCABLE
    Vladimir Dementyev

    View Slide

  2. Vladimir
    Dementyev

    View Slide

  3. Vladimir
    Dementyev

    View Slide

  4. Vladimir
    Dementyev
    John

    View Slide

  5. ! Moscow
    # Cyberjaya

    View Slide

  6. @palkan
    @palkan_tula

    View Slide

  7. Rubyist
    Rails Commiter
    Ruby Commiter

    View Slide

  8. Rubyist
    Rails Commiter
    mRuby Commiter

    View Slide

  9. View Slide

  10. EVIL MARTIANS

    View Slide

  11. Let’s Go!

    View Slide

  12. ANYCABLE

    View Slide

  13. Part 1
    RailsConf 2015
    ACTION
    CABLE

    View Slide

  14. ACTION CABLE
    RailsConf 2015

    View Slide

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

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

  17. CABLE
    HYPE

    View Slide

  18. CABLE HYPE

    View Slide

  19. PROS
    &
    CONS

    View Slide

  20. CABLE PROS
    Easy configuration

    View Slide

  21. CABLE PROS
    Easy configuration
    Channels framework

    View Slide

  22. CABLE PROS
    Easy configuration
    Channels framework
    JS client that just works

    View Slide

  23. CABLE CONS

    View Slide

  24. CABLE CONS
    Lack of transport fallbacks

    View Slide

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

    View Slide

  26. CABLE CONS
    Lack of transport fallbacks
    Memory usage

    View Slide

  27. MEMORY
    20k idle connections
    MB
    0
    375
    750
    1 125
    1 500
    1 875
    2 250
    2 625
    3 000
    Go Erlang Action Cable (2x) Action Cable (16x)

    View Slide

  28. CABLE PROS
    Lack of transport fallbacks
    Memory usage
    Performance

    View Slide

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

    View Slide

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

    View Slide

  31. SHOOTOUT
    Broadcast RTT
    0s
    1s
    2s
    3s
    4s
    5s
    6s
    7s
    8s
    9s
    10s
    Number of connections
    1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
    Go Erlang Action Cable (2x) Action Cable (16x)
    Running on AWS EC2 c3.4xlarge (16 vCPU, 30 GiB RAM)

    View Slide

  32. View Slide

  33. Action
    Cable
    Low
    Latency
    Crowded
    Channels
    CABLE
    THEOREM

    View Slide

  34. CABLE CONS
    Lack of transport fallbacks
    Memory usage
    Performance
    CPU usage

    View Slide

  35. CPU

    View Slide

  36. IS
    THERE
    A WAY
    OUT?

    View Slide

  37. LAZY WAY

    View Slide

  38. Part 2
    ANYCABLE

    View Slide

  39. Client (protocol)
    Channels
    Streams
    Server
    ACTION CABLE

    View Slide

  40. Client (protocol)
    Channels
    Streams
    Server
    ACTION CABLE

    View Slide

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

    View Slide

  42. INITIAL IDEA
    ?

    View Slide

  43. gRPC
    https://grpc.io

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  47. https://grpc.io

    View Slide

  48. Erlang?
    https://grpc.io
    OOPS!

    View Slide

  49. ANYCABLE
    Go WS

    View Slide

  50. View Slide

  51. ANYCABLE SERVERS
    anycable-go
    erlycable

    View Slide

  52. 20k idle connections
    MB
    0
    500
    1 000
    1 500
    2 000
    2 500
    3 000
    3 500
    4 000
    anycable-go erlycable Action Cable (16x)
    MEMORY

    View Slide

  53. ANYCABLE-GO

    View Slide

  54. ERLYCABLE

    View Slide

  55. ACTION CABLE

    View Slide

  56. SHOOTOUT
    Broadcast RTT
    0s
    1s
    2s
    3s
    4s
    5s
    6s
    7s
    8s
    9s
    10s
    Number of connections
    1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
    anycable-go erlycable Action Cable (2x) Action Cable (16x)
    Running on AWS EC2 c3.4xlarge (16 vCPU, 30 GiB RAM)

    View Slide

  57. View Slide

  58. HOW TO
    gem 'anycable-rails', group: :production
    rails generate anycable
    ./bin/anycable # => Run RPC server
    brew install anycable/anycable/anycable-go
    anycable-go -addr=0.0.0.0:3334
    config.action_cable.url = 'ws: //0.0.0.0:3334'

    View Slide

  59. 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 +
    Custom stream callbacks -
    Broadcasting +

    View Slide

  60. http://anycable.io

    View Slide

  61. github.com/anycable
    @any_cable
    ANYCABLE

    View Slide

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

    View Slide

  63. NON-RAILS

    View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide

  68. github.com/palkan/litecable
    LITE CABLE

    View Slide

  69. Part 4
    FUTURE

    View Slide

  70. CABLE CONS
    Lack of transport fallbacks
    Memory & CPU usage
    Performance
    Consistency

    View Slide

  71. CABLE CONS
    Lack of transport fallbacks
    Memory & CPU usage
    Performance
    Consistency

    View Slide

  72. CONSISTENCY

    View Slide

  73. HISTORY STREAM
    class ChatChannel < ApplicationCable ::Channel
    def subscribed
    stream_from "chat_ #{id}",
    history: { time: 5.minutes }
    end
    end
    class ChatChannel < ApplicationCable ::Channel
    def subscribed
    stream_from "chat_ #{chat_id}",
    history: { size: 100 }
    end
    end

    View Slide

  74. MORE FEATURES
    Disconnect-less deployment

    View Slide

  75. DISCONNECT
    WebSocket Server
    Client WebSocket
    Client WebSocket App
    App
    RPC
    RPC

    View Slide

  76. MORE FEATURES
    Disconnect-less deployment
    Data compression (MsgPack?)
    Better instrumentation (WIP)
    Sharing WebSocket server
    between applications

    View Slide

  77. THANK
    YOU!
    anycable.io
    evilmartians.com
    @palkan
    @palkan_tula
    Vladimir Dementyev

    View Slide