If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Switch from a single MYSQL connection to a connection pool

Started by driscolllamvert, Sep 19, 2022, 01:00 AM

Previous topic - Next topic

driscolllamvertTopic starter

Hi there, I apologize for my lack of expertise in this matter. So, I am currently developing a small website that utilizes MYSQL DB. Unfortunately, the connection tends to hang constantly, even when only one person is accessing it. While the database itself is relatively small, with the largest table containing only a few thousand records, it's still crucial to be prepared to expand the user base from 20-50 people to 500-1000.
On the other hand, I understand that not all 1000 users will access the database simultaneously. However, it's essential to anticipate peak loads, especially since according to theory, there could potentially be 1000 simultaneous accesses to the DB.

My questions revolve around whether or not there are any resources explaining how and when to switch from one database connection to a pool. Also, if processing one request from one user takes 0.2 seconds, does that mean it would take 4 seconds for 20 users? Finally, is it possible to make do with just one connection while the database is still relatively small?
I welcome any advice or suggestions you may have on this matter.
  •  

cyborgdigital

The DB connection scheme, the type of database client, and the user's actions (such as read or insert/update) are all factors that determine the performance of the database. Additionally, the amount of data retrieved by select statements is crucial to overall efficiency.

It's worth noting that establishing a separate connection takes time and shouldn't be overlooked. For instance, if the database is poorly structured or contains unoptimized requests that produce excessive unnecessary data, even a small number of users can overload the connection. Finally, it's essential to account for potential issues with transactions, specifically if they require manual management or may need attention later on.
  •  

BluellFrono

It sounds like you're dealing with some common challenges when it comes to scaling a website with a MySQL database. Let's address your questions one by one.

1. Switching from single database connection to a pool:
  When the number of users accessing the database increases, it's often beneficial to switch from a single database connection to a connection pool. A connection pool allows you to manage multiple connections to the database, reusing them instead of creating new connections for each request. This can help reduce the overhead of establishing new connections and improve overall performance. You can find resources explaining how and when to switch to a connection pool in the documentation of your chosen programming language or framework, as well as in MySQL-specific resources.

2. Processing time for multiple users:
  When processing one request takes 0.2 seconds, it does not necessarily mean it would take 4 seconds for 20 users. If the requests are independent of each other and there are enough system resources, the processing time for multiple users can be significantly less than the sum of individual request times. However, it's important to consider potential bottlenecks such as database locks, CPU usage, and disk I/O when scaling to a larger number of users.

3. Using a single connection with a small database:
  While it is possible to use a single connection with a small database, as the user base grows, it may become a limiting factor. Using a connection pool even with a small database can still be beneficial, as it allows for better resource management and scalability.

In summary, as your user base grows, transitioning to a connection pool and optimizing database queries and server resources will be crucial for ensuring the performance and scalability of your website. Keep an eye on potential bottlenecks and consider implementing caching mechanisms to reduce the load on the database.


Switching from a single MySQL connection to a connection pool can significantly improve the performance and scalability of your application. Here's how you can do it using Node.js and the `mysql` package.

First, you'll need to install the `mysql` package if you haven't already:

npm install mysql


Then, you can create a connection pool like this:

const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'mydb'
});


In this example, we're creating a pool with a connection limit of 10. You can adjust the `connectionLimit` based on your application's needs.

Now, instead of creating a single connection, you can get a connection from the pool and use it:

pool.getConnection((err, connection) => {
  if (err) throw err; // handle error

  // Run your queries using the connection
  connection.query('SELECT * FROM mytable', (error, results, fields) => {
    // Handle query results
    // Don't forget to release the connection when done
    connection.release();
  });
});


By using a connection pool, you can efficiently manage multiple connections to the MySQL database, handle connection errors, and minimize the overhead of creating new connections for each request. This approach can significantly improve the performance and scalability of your Node.js application with MySQL.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...