Are Phoenix Controllers and Channels actual Genservers?

Hi all,

In the phoenix framework controllers with their action functions handle http requests and channels handle two way communication.

I would like to know if Controllers and Channels are actual OTP genserver, if so I would like to have some details on how are these created, supervised and pooled in the Phoenix framework.

I am concerned about the fact that Controllers and channels may become contention points in my application and I did not find any resources discussing the aforementioned concern.

Thank you in advance.

1 Like

Channels are genservers (or something very similar), and controllers are plugs (functions).

Maybe TechEmpower Benchmarks Round 14 might help more.

As for how websocket connections are managed, you would want to check cowboy and ranch. (I think they are special processes made with :proc_lib)

1 Like

Hey there!

This is a great question. The architecture for channels vs HTTP requests is pretty different, so let’s handle them separately.

HTTP: Ultimately Phoenix doesn’t really do anything process architecture wise, instead it’s up to whatever actual web server the Phoenix framework is sitting on. By default that’s gonna be cowboy. You can read more about cowboy’s process architecture here: https://medium.com/@kansi/erlang-otp-architectures-cowboy-7e5e011a7c4f. These processes are not genservers, they are created to handle a particular request, and generally die at the end of that request.

Channels: Much like webservers, each individual connection is going to get their own process. Unlike webservers however, these processes are indeed genservers. Don’t think of it like a pool of genservers waiting around for users though. A new genserver is spawned for every web socket connection to a user, as well as for each channel the user joins. You don’t really need to worry about bottlenecks here either, consider: http://www.phoenixframework.org/blog/the-road-to-2-million-websocket-connections

Good luck!

1 Like

Are you looking to limit the number of simultaneous connections?

If so, I think something like this should do the trick:

# config.exs

config :your_app, YourEndpoint,
  http: [
    max_connections: some_number,
    # ...
  ],
  # ...

The default value used by Plug Cowboy adapter is 16384.

2 Likes

Actually no, but thank you for the info. I didn’t know there where a pre-configured limit for the maximum number of connections. This might be useful for me someday.

@idi527 and @benwilson512 thank you for your responses, so if I understand correctly:

  • Controller action functions are executed inside processes managed by the Cowboy webserver, Cowboy creates a new process for each http request.
  • Channels are actual OTP GenServers and a new Channel is created for each client joining a specific topic.

Which means virtually no risk for channels or controllers to become contention points.

3 Likes

You understand correctly!

1 Like