Strategy for Asynchronous database access
-
I need to create a server in Qt C++ with QTcpServer which can handle so many requests at the same time. nearly more than 1000 connections and all these connection will constantly need to use database which is MariaDB.
Before it can be deployed on main servers, It needs be able to handle 1000 connections with each connection Querying data as fast it can on 4 core 1 Ghz CPU with 2GB RAM Ubuntu virtual machine running on cloud. MySQL database is hosted on some other server which more powerful
So how can I implement this ? after googling around, I've come up with following options
-
Create A new QThread for each SQL Query
-
Use QThreadPool for new SQL Query
For the fist one, it might will create so many Threads and it might slow down system cause of so many context switches.
For second one,after pool becomes full, Other connections have to wait while MariaDB is doing its work on other server. So what is the best strategy ?
-
-
None of the above. Pool your TCP connections on top of a few threads, and independently pool your few database connections on top of another set of threads. Make the actual queries asynchronous by queuing them through the tread pool.
The solution's rather complex, though, and 1000 connections ain't that much, so you may be able to get away with a single thread for the TCP stack and a single thread for the SQL stack.