Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Tcp server for high volume traffic
Qt 6.12 LTS is out! See what's new in the release blog

Tcp server for high volume traffic

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 7 Posters 2.8k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marek
    wrote on last edited by
    #1

    Hi
    I have TCP server basically created as from the example of Threaded Fortune Server, I use this scheme for many of my projects, works well and stable.
    However someone wanted me to run the server for 10.000 users simultaneosly. So I quickly created server, and client application to test it out, client when it starts it creates 2500 threads, connects to the server and sends/receive JSON payload every 5 sec, there were 4 clients running on different Linux machines located on the Internet
    With 5000 connections I have about 17 mln. files open on my Linux VPS (8 cores 16GB RAM) and I fill like this is the limit for stable work. When I try 10.000 simultaneous connections, server drops some of the connections, or I can't connect.
    I can't use REST API approach for that because the goal is that server sends some data to all users on specific time intervals.

    I have been shown that some other company made server with 30.000 simultaneous users connected and sending data to all of them took server 3 seconds. My test was a bit different becuse each testing thread was sending JSON query every 5 sec. but I got the feeling that the problem was with the number of simultaneos connections.
    I have made a repo with server example and screens from top command running on Linux
    https://github.com/frankipl/load-server.git

    Details of my test:
    10.000 user records in PostgresSQL database, server on startup reads them all, so for reading queries I serve data from server memory, only for update or write I update postgres data
    Each thread has 88 queries of read data, 10 queries to update data, 1 query add data, 1 query sends small PDF file (315 kb), so there is 100 queries, each testing thread draws a query until it sends them all and repeats again.

    Question is: How to make it work better ? And especially for a server with 8 cores

    Best Regards
    Marek

    raven-worxR D 2 Replies Last reply
    0
    • M Marek

      Hi
      I have TCP server basically created as from the example of Threaded Fortune Server, I use this scheme for many of my projects, works well and stable.
      However someone wanted me to run the server for 10.000 users simultaneosly. So I quickly created server, and client application to test it out, client when it starts it creates 2500 threads, connects to the server and sends/receive JSON payload every 5 sec, there were 4 clients running on different Linux machines located on the Internet
      With 5000 connections I have about 17 mln. files open on my Linux VPS (8 cores 16GB RAM) and I fill like this is the limit for stable work. When I try 10.000 simultaneous connections, server drops some of the connections, or I can't connect.
      I can't use REST API approach for that because the goal is that server sends some data to all users on specific time intervals.

      I have been shown that some other company made server with 30.000 simultaneous users connected and sending data to all of them took server 3 seconds. My test was a bit different becuse each testing thread was sending JSON query every 5 sec. but I got the feeling that the problem was with the number of simultaneos connections.
      I have made a repo with server example and screens from top command running on Linux
      https://github.com/frankipl/load-server.git

      Details of my test:
      10.000 user records in PostgresSQL database, server on startup reads them all, so for reading queries I serve data from server memory, only for update or write I update postgres data
      Each thread has 88 queries of read data, 10 queries to update data, 1 query add data, 1 query sends small PDF file (315 kb), so there is 100 queries, each testing thread draws a query until it sends them all and repeats again.

      Question is: How to make it work better ? And especially for a server with 8 cores

      Best Regards
      Marek

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Marek
      10.000 threads are honestly horrible ;)
      especially if your are aiming for more perfomance. Thread scheduling,/handling is not free and also consumes resoruces!

      the keyword here is: load balancing

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      M 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @Marek
        10.000 threads are honestly horrible ;)
        especially if your are aiming for more perfomance. Thread scheduling,/handling is not free and also consumes resoruces!

        the keyword here is: load balancing

        M Offline
        M Offline
        Marek
        wrote on last edited by
        #3

        @raven-worx
        This was also my idea. But they showed me server running on single VPS with 30.000 users connected, so... maybe I shouldn't create thread for each connection ? Like with select on Linux, just to monitor descriptors ?
        I mean surely someone has some experience with that, 10.000 users is not such a big number, probably 10.000 threads is ;)

        Best,
        Marek

        1 Reply Last reply
        0
        • M Marek

          Hi
          I have TCP server basically created as from the example of Threaded Fortune Server, I use this scheme for many of my projects, works well and stable.
          However someone wanted me to run the server for 10.000 users simultaneosly. So I quickly created server, and client application to test it out, client when it starts it creates 2500 threads, connects to the server and sends/receive JSON payload every 5 sec, there were 4 clients running on different Linux machines located on the Internet
          With 5000 connections I have about 17 mln. files open on my Linux VPS (8 cores 16GB RAM) and I fill like this is the limit for stable work. When I try 10.000 simultaneous connections, server drops some of the connections, or I can't connect.
          I can't use REST API approach for that because the goal is that server sends some data to all users on specific time intervals.

          I have been shown that some other company made server with 30.000 simultaneous users connected and sending data to all of them took server 3 seconds. My test was a bit different becuse each testing thread was sending JSON query every 5 sec. but I got the feeling that the problem was with the number of simultaneos connections.
          I have made a repo with server example and screens from top command running on Linux
          https://github.com/frankipl/load-server.git

          Details of my test:
          10.000 user records in PostgresSQL database, server on startup reads them all, so for reading queries I serve data from server memory, only for update or write I update postgres data
          Each thread has 88 queries of read data, 10 queries to update data, 1 query add data, 1 query sends small PDF file (315 kb), so there is 100 queries, each testing thread draws a query until it sends them all and repeats again.

          Question is: How to make it work better ? And especially for a server with 8 cores

          Best Regards
          Marek

          D Offline
          D Offline
          DerReisende
          wrote on last edited by
          #4

          @Marek 10k threads are bad in most OS due to overhead. Usage of a thread pool should be the best way for it.
          Maybe this tutorial is helpful for a sample implementation.

          M 1 Reply Last reply
          1
          • D DerReisende

            @Marek 10k threads are bad in most OS due to overhead. Usage of a thread pool should be the best way for it.
            Maybe this tutorial is helpful for a sample implementation.

            M Offline
            M Offline
            Marek
            wrote on last edited by
            #5

            @DerReisende Thanks
            Lots of tutorials on this website.
            After checking a few of them, I'm not sure I doing my Threaded Server the right way
            I don't use QThread::run
            I just create new AppNetThread which inherits from QThread and run it keeping pointers to this object in QMap
            Can someone wise take a look at the code? :)

            Best,
            Marek

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              You might want to check the Cutelyst project which may have some of your needs covered.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              1
              • SGaistS SGaist

                Hi,

                You might want to check the Cutelyst project which may have some of your needs covered.

                M Offline
                M Offline
                Marek
                wrote on last edited by
                #7

                @SGaist Thanks for that link
                It does not solve tcp server issue, but I always had problem with web applications in my projects, usually I use qhttpengine to make REST API and then React for frontend. If this Cutelyst can solve issue of proper REST API or I can compile into the server and make simple web panel for administration then it is a huge improvement ;)

                Best,
                Marek

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on last edited by
                  #8

                  My understanding is that typical web servers handle multiple connections in a single thread. Just as the others said, 10000 threads is a lot for the operating system to handle. The threaded fortune server example is nice for few connections, but does not scale well. It is okay to have multiple threads, but rather like 8 threads for 8 cores. Each thread can then handle a multitude of connections.

                  I am not sure if Qt is then the best solution. You might want to have a look at other frameworks for networking, like asio (https://think-async.com/Asio). asio also is part of Boost if you are using this already.

                  M 1 Reply Last reply
                  0
                  • S SimonSchroeder

                    My understanding is that typical web servers handle multiple connections in a single thread. Just as the others said, 10000 threads is a lot for the operating system to handle. The threaded fortune server example is nice for few connections, but does not scale well. It is okay to have multiple threads, but rather like 8 threads for 8 cores. Each thread can then handle a multitude of connections.

                    I am not sure if Qt is then the best solution. You might want to have a look at other frameworks for networking, like asio (https://think-async.com/Asio). asio also is part of Boost if you are using this already.

                    M Offline
                    M Offline
                    Marek
                    wrote on last edited by
                    #9

                    @SimonSchroeder
                    Hi,
                    This is also my idea now, create my own pool of threads related to number of processor cores and handle multiple connections inside one thread, this will utilize cores better. I just don't have time now to rewrite test server, but once I will I do that and report performance. Also my colleague suggested using Boost for that, which we actually mix now with Qt code in mobile apps.
                    Best Regards
                    Marek

                    jsulmJ 1 Reply Last reply
                    0
                    • M Marek

                      @SimonSchroeder
                      Hi,
                      This is also my idea now, create my own pool of threads related to number of processor cores and handle multiple connections inside one thread, this will utilize cores better. I just don't have time now to rewrite test server, but once I will I do that and report performance. Also my colleague suggested using Boost for that, which we actually mix now with Qt code in mobile apps.
                      Best Regards
                      Marek

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Marek said in Tcp server for high volume traffic:

                      create my own pool of threads

                      There is https://doc.qt.io/qt-5/qthreadpool.html

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      J.HilkJ 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @Marek said in Tcp server for high volume traffic:

                        create my own pool of threads

                        There is https://doc.qt.io/qt-5/qthreadpool.html

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @jsulm @Marek

                        or Qt Concurrent that does the thread pool management for you.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        M 1 Reply Last reply
                        1
                        • J.HilkJ J.Hilk

                          @jsulm @Marek

                          or Qt Concurrent that does the thread pool management for you.

                          M Offline
                          M Offline
                          Marek
                          wrote on last edited by
                          #12

                          @J-Hilk
                          Hi
                          From what I read about it ThreadPool will not be good idea for me.
                          in QThreadPool you need to provide function to run or Object that will inherit from QRunnable with overwritten run method. If I would like to manage signal/slot for socket then QRunnable should also inherit from QObject, too much complications. It looks like QThreadPool is designed for heavy workload which start and ends after some time then start again with same/different work to do, while in my scenario threads will run constantly, just socket connections will come and go. But thanks for looking into it, I will report what I found out about performance
                          Best Regards
                          Marek

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt
                          • Unsolved