{"id":22898024,"url":"https://github.com/andyj/mariadb-connection-pool","last_synced_at":"2025-04-01T00:46:39.565Z","repository":{"id":263509845,"uuid":"890629809","full_name":"andyj/mariadb-connection-pool","owner":"andyj","description":"Optimising MariaDB performance in Node.js applications","archived":false,"fork":false,"pushed_at":"2024-11-19T09:35:19.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-07T02:37:43.962Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andyj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-18T22:51:23.000Z","updated_at":"2024-11-19T09:35:23.000Z","dependencies_parsed_at":"2024-11-19T00:19:06.562Z","dependency_job_id":"a85b73d5-037b-4f08-94e9-0d68e4abe472","html_url":"https://github.com/andyj/mariadb-connection-pool","commit_stats":null,"previous_names":["andyj/mariadb-connection-pool"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyj%2Fmariadb-connection-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyj%2Fmariadb-connection-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyj%2Fmariadb-connection-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyj%2Fmariadb-connection-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andyj","download_url":"https://codeload.github.com/andyj/mariadb-connection-pool/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246563381,"owners_count":20797446,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-14T00:20:37.924Z","updated_at":"2025-04-01T00:46:39.550Z","avatar_url":"https://github.com/andyj.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Mariadb Connection Pools In Node\n## Optimizing MariaDB Performance in Node.js Applications\n\nRead the full post here:\n[Optimizing MariaDB Performance in Node.js Applications](https://www.andyjarrett.com/posts/2024/connection-pools-in-node-with-mariadb/)\n\n### Why Use a Connection Pool?\n\nDatabase connection pooling is critical for your web applications. Without a pool, every database query spins up a new connection, which can quickly exhaust your resources! A connection pool maintains a stash of open connections ready to use, reducing overhead and boosting performance.\n\nThis project also provided an opportunity to revisit and improve some of my older code by incorporating [console methods for debugging](https://www.andyjarrett.com/posts/2024/utility-console-methods-for-debugging-workflow/) that I’ve covered [recently](https://www.andyjarrett.com/posts/2024/advanced-javascript-console-methods/).\n\n### Benefits of Connection Pooling\n\n- **Reduced Latency**: Reusing connections eliminates the time spent establishing new ones.\n- **Better Resource Management**: Limits the number of simultaneous connections, keeping your database stable.\n- **Improved Scalability**: Handle more users without compromising performance.\n\n### Installation\n\nTo get started, install the necessary dependencies:\n\n```bash\nnpm install mariadb dotenv\n```\n\n### Setting Up the Connection Pool\n\nCreate a `db.js` file to configure the connection pool:\n\n```javascript\nrequire('dotenv').config(); // Load environment variables\n\nconst mariadb = require('mariadb');\n\nconst pool = mariadb.createPool({\n  host: process.env.DB_HOST,\n  user: process.env.DB_USER,\n  password: process.env.DB_PASSWORD,\n  database: process.env.DB_NAME,\n  port: process.env.DB_PORT,\n  connectionLimit: 10 // Adjust based on your application's needs\n});\n\nmodule.exports = pool;\n```\n\n### Using the Pool in Your Application\n\nHere’s an example of how to use the connection pool in an Express route:\n\n```javascript\nconst express = require('express');\nconst pool = require('./db'); // Import the pool\nconst app = express();\n\napp.get('/data', async (req, res) =\u003e {\n  console.group('Database Query');\n  console.time('Query Time');\n\n  let conn;\n  try {\n    conn = await pool.getConnection();\n    console.assert(conn, 'Failed to obtain a database connection');\n\n    const rows = await conn.query('SELECT current_timestamp()');\n    console.table(rows);\n\n    res.json(rows);\n  } catch (err) {\n    console.error('Error executing query');\n    console.trace(err);\n    res.status(500).send('Database query error');\n  } finally {\n    if (conn) conn.release(); // Release the connection back to the pool\n    console.timeEnd('Query Time');\n    console.groupEnd();\n  }\n});\n\napp.listen(3000, () =\u003e {\n  console.log('Server is running on port 3000');\n});\n```\n\n### Best Practices\n\n- **Monitor Pool Usage**: Adjust `connectionLimit` based on your application’s traffic and database capacity.\n- **Use Timeouts Wisely**: Set reasonable timeouts for idle connections and query execution to optimize resource usage.\n- **Leverage Cloud Tools**: Use AWS CloudWatch (or equivalent) to monitor performance and tweak your configurations.\n\n### Example Configuration for AWS RDS\n\nFor those deploying on AWS RDS, here are example settings for two different instance types:\n\n#### **db.t3.micro** (Entry-Level Instance)\n\n```javascript\nconst pool = mariadb.createPool({\n  host: process.env.DB_HOST,\n  user: process.env.DB_USER,\n  password: process.env.DB_PASSWORD,\n  database: process.env.DB_NAME,\n  port: process.env.DB_PORT,\n  connectionLimit: 5,\n  acquireTimeout: 10000,\n  idleTimeout: 30000,\n  connectTimeout: 10000,\n});\n```\n\n#### **db.m6g.large** (Production-Level Instance)\n\n```javascript\nconst pool = mariadb.createPool({\n  host: process.env.DB_HOST,\n  user: process.env.DB_USER,\n  password: process.env.DB_PASSWORD,\n  database: process.env.DB_NAME,\n  port: process.env.DB_PORT,\n  connectionLimit: 20,\n  acquireTimeout: 20000,\n  idleTimeout: 60000,\n  queueLimit: 30,\n  connectTimeout: 15000,\n});\n```\n\n### License\n\nThis project is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandyj%2Fmariadb-connection-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandyj%2Fmariadb-connection-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandyj%2Fmariadb-connection-pool/lists"}