Updating README.md adding 'Configuring the Database Connection Pool'. Updating main.go configurating the database connection pool with maxOpenConns, maxIdleConns and maxIdleTime values.

This commit is contained in:
Maxime Delporte
2025-10-30 12:13:55 +01:00
parent 8cdf3c7ada
commit f72ad2aa2f
2 changed files with 29 additions and 2 deletions

View File

@@ -200,4 +200,13 @@ To read more about PostgreSQL optimization :
https://www.enterprisedb.com/postgres-tutorials/how-tune-postgresql-memory
To generate suggested values based on your available system hardware, you can use :
https://pgtune.leopard.in.ua
https://pgtune.leopard.in.ua
#### Configuring the Database Connection Pool
1. You should explicitly set a **MaxOpenConns** value. This should be comfortably below any hard limits on the number of connections imposed by your database and infrastructure, and you may also want to consider keeping it fairly low to act as a rudimentary throttle.
For this project we'll set a **MaxOpenConns** limit of 25 connections. This is a reasonable starting point for small-to-medium web applications and APIs, but ideally you should tweak this value for your hardware depending on the results of benchmarking and load-testing.
2. In general, higher **MaxOpenConns** and **MaxIdleConns** values will lead to better performance. But the returns are diminishing, and you should be aware that having a too-large idle connection pool (with connections that are not frequently re-used) can actually lead to reduced performance and unnecessary resource consumption.
Because **MaxIdleConns** should always than or equal to **MaxOpensConns**, we'll also limit **MaxIdleConns** to 25 connections for this project.
3. To mitigate the risk from point 2 above, you should generally set a ConnMaxIdleTime value to remove idle connections that haven't been used for a long time. In this project we'll set a ConnMaxIdleTime duration of 15 minutes.
4. It's probably OK to leave **ConnMaxLifetime** as unlimited, unless your database imposes a hard limit on connection lifetime, or you need it specifically to facilitate something like gracefully swapping databases. Neither of those things apply in this project, so we'll leave this as the default unlimited setting.