# AnyCable: Action Cable on steroids

> This article tells the story of the AnyCable project that aims to increase Action Cable performance and capabilities.

- Date: 2016-12-20T00:00:00.000Z
- Authors: Vladimir Dementyev
- Categories: Performance
- URL: https://evilmartians.com/chronicles/anycable-actioncable-on-steroids

---

[Action Cable](http://edgeguides.rubyonrails.org/action_cable_overview.html) is one of the most attractive new features in Rails 5. WebSockets out of the box—sounds pretty cool, doesn't it?

**New!** _The code examples in this article were updated in July 2020 to reflect the changes in AnyCable APIs and configuration. Check out the official [documentation website](https://docs.anycable.io) for up-to-date information_.

Action Cable shows plenty of good parts:

*Read also Evil Martians' article*: https://evilmartians.com/chronicles/anycable-1-0-four-years-of-real-time-web-with-ruby-and-go

- Astonishingly easy configuration.
- Application logic access through channels—definitely the killer feature.
- JavaScript client that works with no additional effort.

Everything you need to build yet another [Chat on Rails](https://www.google.com/?q=actioncable+chat#newwindow=1&q=actioncable+chat) in minutes!

Now, to the troubling part. In my opinion, besides [bugs](https://github.com/rails/rails/issues?q=is%3Aopen+is%3Aissue+label%3Aactioncable), the only problem is Ruby itself—the language I'm fond of, but not considering it a technology fit for writing scalable concurrent applications. Not yet, at least (yeah, everyone heard about Ruby 3x3 and Matz's [plans](https://twitter.com/yukihiro_matz/status/495219763883163648)).

## Benchmarks: Action Cable vs. Erlang vs. Golang

So what exactly is wrong with Action Cable? The answer is performance.

Let's consider several metrics: memory usage, CPU pressure, and broadcasting time. We built our benchmarks on top of the famous [WebSocket Shootout](https://hashrocket.com/blog/posts/websocket-shootout) by HashRocket.

We compared Action Cable with servers written in Erlang and Golang. The hardware we used to run the benchmarks was AWS EC2 `4.xlarge` (16 vCPU, 30Gib memory). Let's take a look at the results.

First, the RAM usage:

*Image: **Benchmark:** handle 20 thousand idle clients to the server (no subscriptions, no transmissions).*

The results are pretty self-explaining: Action Cable requires much more memory. Because it's Ruby. And, of course, Rails.

For the next two measurements we used _WebSocket Shootout_ broadcasting benchmark which can be described as follows:

1000 clients connect, 40 of them send a message to a server, which is then re-transmitted to all clients. The sender-client measures the time it takes the message to make a round-trip (RTT).

Look how the CPU reacts during the benchmark:

And, last but not least, the broadcasting performance:

*Image: Action Cable RTT*

About a second for one thousand clients and more than ten seconds for ten thousand! Doesn't look _real-time_, does it?

But can we use the good parts of Action Cable with the power of Erlang/Golang/Whatever-you-like altogether? The answer is "Yes, we can". And here comes [AnyCable](http://anycable.io).

## The Idea

When [@dhh](http://twitter.com/dhh) announced Action Cable on April 2015, my initial thought was "Wow, this is Omakase I like!" The second thought was: "Oh, no, Ruby wasn't meant for that." And finally, the third thought: "I guess I know how to fix this!"

I've already had experience writing WebSocket-based applications in Erlang, and I knew that Erlang is one of the best players in this game, so I wrote down a simple architecture sketch:

*Image: AnyCable sketch*

Looks pretty obvious except one thing: the communication between an Erlang server and a Rails app (Action Cable channels). The straightforward solution is using HTTP and REST. But don't forget that we want to improve performance, so that's not an option.

But aren't we able to develop our own RPC protocol with raw TCP transport and binary encoding, maybe, Protocol Buffers? We sure are!

Luckily, I found a better option—[gRPC](http://grpc.io). The puzzle was solved and [AnyCable](https://anycable.io) was born.

## Introducing AnyCable

Take a look at the figure below:

*Image: AnyCable architecture.*

AnyCable WebSocket server is responsible for handling clients, or _sockets_. That includes:

- Low-level connections (sockets) management.
- Subscriptions management.
- Broadcasting messages to clients.

The server doesn't know anything about your business logic. It's a kind of a **logicless proxy server**, just like NGINX for HTTP traffic.

WebSocket server should include **gRPC client** built from AnyCable [`rpc.proto`](https://github.com/anycable/anycable/blob/1-1-stable/protos/rpc.proto), that describes the following RPC service:

```protobuf
service RPC {
  rpc Connect (ConnectionRequest) returns (ConnectionResponse) {}
  rpc Command (CommandMessage) returns (CommandResponse) {}
  rpc Disconnect (DisconnectRequest) returns (DisconnectResponse) {}
}
```

RPC server is a connector between Rails application and WebSocket server. It's an instance of your application with a gRPC endpoint which implements [`rpc.proto`](https://github.com/anycable/anycable/blob/1-1-stable/protos/rpc.proto).

This server is a part of the [`anycable`](https://github.com/anycable/anycable) gem. Another gem, [`anycable-rails`](https://github.com/anycable/anycable-rails), makes it possible to use all Action Cable goodies (channels, streams) without any tweaks (see below for more information).

By default, we use Redis pub/sub to send messages from the application to the WS server, which then broadcasts the messages to clients. For test and development you can use an alternative HTTP pub/sub implementation. Other adapters might get included in the future versions, if there is a [demand from the community](https://github.com/anycable/anycable/issues/2).

## How AnyCable works?

After a brief look at the AnyCable architecture let's go deeper and see what's going on—from both a Rails application and a client point of view.

Consider a simple example—yet another Action Cable chat application. We want to implement the following features:

- Only authenticated users can join rooms.
- A user can join the _specific_ chat room (using ID).
- A user can send a message to everyone in the room.

Each feature requires client-server communication. Let's discuss them one by one.

### Authentication

So how does authentication happen in Action Cable? The same way we usually handle it in our controllers: with [cookies](http://edgeguides.rubyonrails.org/action_cable_overview.html#server-side-components-connections).

Every time a client connects to Action Cable the application instantiates an instance of the `ApplicationCable::Connection` class and invokes the `#connected` method on it. The instance is added to the list of all connections and stays in memory until the client disconnects.

The connection allows you to access the underlying request object and, of course, its headers, including cookies:

```ruby
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connected
      self.current_user = User.find_by(id: cookies[:user_id])
      reject_unauthorized_connection unless current_user
    end
  end
end
```

What about AnyCable? The connection itself occurs outside of the Rails application, on our WebSocket server. How can we authenticate it, or, more precisely, how to invoke our `#connected` method?

This is how AnyCable WebSocket server handles a client connection:

- Accepts a connection.
- Invokes the `Connect` RPC method and pass the request headers (some of them, e.g. `Cookie`) to it.
- Receives a response from an RPC server describing connection identifiers (JSON encoded) and status (whether it should be disconnected or not).
- Disconnects the client if necessary.
- Transmits messages to the client if the response object contains any.

And that is what happens within the `Connect` method realization on our RPC server:

- Create instance of the [slightly patched](https://github.com/anycable/anycable-rails/blob/1-0-stable/lib/anycable/rails/actioncable/connection.rb) `ApplicationCable::Connection` class.
- Invoke `#handle_open` on this connection.
- Reply with the connection information (identifiers) and status.

Note that AnyCable doesn't store any objects (e.g., connections) in memory. They're all disposable.

### Subscriptions

Ok, now we're connected to the server. To make this connection useful we have to subscribe to a channel.

To do so, the client has to send a message in a specific form: `{"command": "subscribe", "identifier": "..."}`.

Consider our example chat channel code:

```ruby
class ChatChannel < ApplicationCable::Channel
  def subscribed
    stream_from "chat_#{params[:id]}"
  end
end
```

The client sends the message (JS code):

```javascript
App.cable.subscriptions.create({channel: 'ChatChannel', id: 1}, ...)

```

AnyCable server transforms this message into a `Command` RPC call and then:

- Adds the client (socket) to the `chat_1` _broadcasting group_.
- Sends a subscription confirmation to the client.

What's a _broadcasting group_? Implementations may vary and depend on a language or platform you choose to build your WebSocket server. But to one degree or another it _quacks_ like a hash that maps streams to connections.

### Performing Actions

We're almost done. The only question remaining is, how do we send a message to the clients?

Let's add one more method to our channel:

```ruby
class ChatChannel < ApplicationCable::Channel
  # ...

  def speak(data)
    ActionCable.server.broadcast(
      "chat_#{params[:id]}",
      text: data['text'], user_id: current_user.id
    )
  end
end
```

To perform an action the client should send another command: `{"command": "message", "identifier": "..."}`. We can handle it the same way we do with the "subscribe" command.

The interesting thing here is the `#broadcast` call. Why? Because it's the weakest part of Action Cable (see broadcasting benchmark above).

In a simplified form, broadcasting can be implemented like this:

```ruby
subscribers_map[stream_id].each do |channel|
  channel.transmit message
end
```

What's wrong with it? First, [Rails itself](https://github.com/rails/rails/issues/26999). The second suspect is Ruby.

AnyCable moves this functionality out of Ruby and Rails and leverages the power of other technologies.

I think, it's time to repeat our benchmarks with the new kid on the block—AnyCable.

## Benchmarks: AnyCable

AnyCable shows almost the same resources usage as _raw_ Erlang or Golang servers.

The latest benchmarks could be found in the [main AnyCable repository](https://github.com/anycable/anycable/tree/master/benchmarks) on GitHub.

Memory usage is more than acceptable:

*Image: **Benchmark:** handle 20 thousand idle clients to the server (no subscriptions, no transmissions).*

And CPU doesn't feel any pressure too:

Broadcasting time is perfect:

*Image: Action Cable RTT*

## Compatibility

AnyCable aims to be as much compatible with Action Cable, as it possible to use _standard_ Action Cable in development and test environments. But some features are rather hard to implement without loss of the performance gain.

See [compatibility table](https://docs.anycable.io/rails/compatibility) for further information.

## How to use?

Want to give AnyCable a try? It's pretty easy. Assuming you want to use AnyCable with a Rails app (if not—check out the docs for [non-Rails usage](https://docs.anycable.io/ruby/non_rails)):

- Add [anycable-rails](https://github.com/anycable/anycable-rails) gem to your Gemfile:

```ruby
gem "anycable-rails"
```

- Run an interactive setup generator:

```shell
rails g anycable:setup
```

- Install and run compatible WebSocket server, e.g., [anycable-go](https://docs.anycable.io/anycable-go/getting_started).

- Run AnyCable along with the Rails web server.

Here is an example `Procfile` for [Hivemind or Overmind](https://evilmartians.com/chronicles/introducing-overmind-and-hivemind):

```yaml
web: bundle exec rails s
rpc: bundle exec anycable
go:  anycable-go --port=8080
```

Do not have a Rails application with Action Cable yet? Feel free to play with our [demo application](https://github.com/anycable/anycable_rails_demo)!

## What's next?

We've learned one aspect of AnyCable—the plug and play performance upgrade for Action Cable.

This approach—extracting low-level functionality from business logic or separation of concerns—can help us solve many problems:

- Support transport fallbacks (e.g. long-polling) or custom transports.
- Build shareable WebSocket servers (i.e., one WebSocket server for several different applications).
- ~~add enhanced analytics and monitoring to your WebSocket server~~—See [AnyCable-Go instrumentation](https://docs.anycable.io/anycable-go/instrumentation)
- ~~And even use Action Cable (as a protocol) outside of Rails~~—See [Non-Rails usage](https://docs.anycable.io/ruby/non_rails).

And, of course, you can easily extend your _cable_ with custom features, that are not supported (and unlikely to be) by Action Cable.

---

[AnyCable](http://anycable.io) is a mature open source project, which has already hit the 1.0 release and started the 2.0 development cycle (see the [AnyCable 1.0 announcement post](https://evilmartians.com/chronicles/anycable-1-0-four-years-of-real-time-web-with-ruby-and-go#from-any-to-many-the-future-of-cables-and-how-you-can-help)).

Check out our [official website](https://anycable.io) and [GitHub page](https://github.com/anycable/anycable) for more information. Looking for help in building real-time features for your app? Evil Martians offer [commercial support](https://anycable.io/#custom-solutions), feel free [to drop us a line](https://evilmartians.com/contact-us)!

---

This article was written by [Evil Martians](https://evilmartians.com), a product development consultancy trusted by teams from early-stage startups to public companies. If you're working through a hard problem, [we can help](https://evilmartians.com/contact-us)
