codekiln
codekiln

Reputation: 3720

Is multiplexing websocket connections an important optimization in Django Channels >= 2?

I maintain a Django Channels v1 app with six Consumers multiplexed over one WebSocket connection. I'm upgrading to Django Channels v2, where connection (de)multiplexing is no longer supported; see GitHub Issue #825 - (Re)Implement Multiplexing. If I want to multiplex, I can use channels-demultiplexer, channelsmultiplexer, or write my own. I'm leaning towards re-implementing multiplexing because I think it would entail changing less of our existing code.

Before doing that, I'm doing some due diligence to find out whether multiplexing is even an important optimization.

Given a Redis-backed Django 2 + Channels 2 app in a load balanced environment behind Nginx with Daphne (currently planning mostly SyncConsumers) and Gunicorn, I think that increasing the number websocket connections by 3x-5x would only negligibly increase Redis RAM, app server RAM, and Daphne CPU, and also negligibly increase browser RAM and CPU, but that's just an educated guess.

  1. How should I think about the cost of each additional WebSocket connection a.) in Django Channels v2 and b.) in the browser?
  2. I have not seen any web apps that were designed to use many WebSocket connections on a single page. Have you? If so, is it performant? How many connections did it utilize?
  3. What would you do?

Stack Overflow posts that address adjacent but different questions:

  1. How to use multiple websocket connections using Django Channels?
  2. Django Channels 2 implementing Multiplexer/Demultiplexer like Channels 1
  3. Reusing an existing websocket in Django Channels

Upvotes: 1

Views: 1098

Answers (1)

Matthaus Woolard
Matthaus Woolard

Reputation: 2408

As the author of channelsmultiplexer I hope i have some useful insights.

There is one key thing to remember with Channelv2 each consumer instance has 1 run-roop so while it is handling a message is will not be able to handle another one (they get queued up). In particular if you have actions on your consumer that take time (db, or other) the consumer will not get other messages while it is doing work. When using channelsmultiplexer the blocking does not affect other multiplexed consumer instances. If there are changes/additions you need to channelsmultiplexer feel free to create an issue on the repo i am happy to make changes as needed to fit more peoples use cases.

How should I think about the cost of each additional websocket connection a.) in Django Channels v2 and b.) in the browser?

Yes since unless your tunnelling ws over HTTP2 (not something channels support but nginx could help you year if you are sure all your clients are http2) the users browser will likely limit the number of concurrent HTTP connections you can have open. Many browsers will limit you to about 7 open connections but this could change at any time, and mobile browser might be more strict (they tend to be)

Upvotes: 3

Related Questions