H
H
Hamlet Muradyan2016-01-31 15:32:02
Python
Hamlet Muradyan, 2016-01-31 15:32:02

How to balance a WebSocket server?

Good afternoon.
I'm developing a high-load project that uses the WebSocket protocol for two-way communication between browsers and a server. Every second, the server receives thousands of data from some clients, processes, and sends similar data to other clients. In this case, the continuous operation of the server is very important, since the clients that receive data, if they stop receiving them for at least a few seconds, then their client part will not work properly.
At the moment, everything works fine for me when I raise only one instance of the server. But I also want to set up a balancer for all this.
But if you raise two instances, then each instance can only receive data from clients that are connected to it. The same goes for sending data.
How can I write this part of the server correctly so that all data and connections to the server are synchronized between processes?
PS The server is written in Python using the TornadoWeb framework. The balancer I plan to use Nginx, but if it's not suitable, then I can use another one.
UPD: Instances must be raised on different physical servers.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lega, 2016-01-31
@lega

You can do it "in a simple way" so that the nodes transfer data to each other on the server side, but if the spread of connections is large and a lot of traffic goes between the nodes, then there is no sense in this.
1) Therefore, it is better to group all users into groups and these groups communicated only within the node.
2) If the sender knows to whom he sends data (for example, a chat), then in the user object you can keep the node id on which he is currently hosted, then the sender will send data directly to the server where the recipient is hosted.
3) the same as 2. but it may be easier - to make groups/cabinets like in chats, and these groups are hosted on certain nodes through them and users communicate.
You can also unload your tornado server, throw out everything that is not related to data transfer, if there are any calculations, etc. - to an external handler.

A
Andrey K, 2016-01-31
@mututunus

Use some Pub/Sub server like Redis.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question