Hello! Faced such a problem. I have a Tornado web server and it works using the WebSocket protocol and regular http. As a result, I launched testing, where 2500 users were connected via websocket. Well, they sent a bunch of messages to the server, which pulled the database. Judging by the monitoring of resources, the database was not heavily loaded. But for some reason, when I go to the page hosted on this web server, it was loaded for a very long time, although there are no requests even to the database. What factors can make a web server hang? I note that another web server is also raised on the server and it does not hang.

    1 answer 1

    Hello,

    Tornado is really fast when processing requests for non-blocking subscribers (Websockets connections), but do not forget that it works in single-threaded mode, and blocking requests will get bogged down.

    1. Separate the WebSocket instance handlers and block request handler instances. If necessary, you can add more instances to handle regular requests, but it is not advisable to run more instances than the number of processor cores. For balancing use Nginx.

    2. For heavy queries (development of algorithms requiring time delays) it is necessary to use decorators from the tornado.gen native module.

    3. To work with the database, it is better to use multi-threaded pools of connections:

      • For Postgres, Momoko works well;
      • For MySQL TorMySQL or Tornado-MySQL;

      • It is not recommended to use multi-threaded decorators to work with Redis. More often, Redis requests are processed faster than tornado creates additional threads. Therefore, for fast Redis requests it is better to use a single-threaded handler.

    4. For very heavy handlers, use the Zeromq, RabbitMQ, Gearman or Redis queue managers. Immediately after sending the job to the queue, the tornado handler is ready to take the next request.

    Tornado is very fast in single-threaded mode, design your application using the balance between the need for additional threads and a well-thought-out single-threaded handler algorithm. Multi-threaded handlers give a good result, but until a certain point. With the general corral of everything and everything in separate streams, the speed of a tornado drops noticeably.