user10146018
user10146018

Reputation:

Strategy for Asynchronous database access with Qt5 SQL

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

1. Create A new QThread for each SQL Query

2. 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. So what is the best strategy ?

Upvotes: 1

Views: 462

Answers (2)

Wilson Hauck
Wilson Hauck

Reputation: 2343

Consider for my.cnf [mysqld] section

thread_handling=pool-of-threads

Good luck.

Upvotes: 0

Deep
Deep

Reputation: 2512

Sorry for bad english.

1) Exclude.

2) Exclude.

3) Here first always doing work qt. Yes, connections (tasks for connections) have to wait for available threads, but you easy can add 10000 tasks to qt threadpool. If you want, configure max number of threads in pool, timeouts for tasks and other. Ofcourse your must sync shared data of different threads with semaphore/futex/mutex and/or atomics.

Mysql (maria) it's server, and this server can accept many connections same time. This behaviour equally what you want for your qt application. And mysql it's just backend with data for your application.

So your application it's server. For simple, you must listen socket for new connections and save this clients connections to vector/array and work with each client connection. Always when you need something (get data from mysql backend for client (yeah, with new, separated for each client, onced lazy connection to mysql), read/write data from/to client, close connection, etc.) - you create new task and add this task to threadpool.

This is very simple explanation but hope i'm helped you.

Upvotes: 1

Related Questions