Connection Pooling
Production environments are often composed of multiple resources: web servers, caching servers, and database servers. The database is typically hosted on a separate machine from the web server, allowing horizontal growth of the public-facing website without the need for setting up and configuring complex database clusters. Application developers must therefore be aware of the performance implications in accessing resources and how those access costs affect their site’s performance.
Connection pooling is an important concept in web development because the performance cost of establishing a database connection is relatively high; creating one or more new connections for every request creates an unnecessary burden on a heavily trafficked site and will contribute to weaker performance. The solution is to maintain database connections inside a cache pool after they are no longer needed, so they can be used immediately by the next incoming request.
Many database drivers provide pooling functionality, but that
pattern goes against Node’s “one module, one purpose” philosophy. Instead,
Node developers should use the generic-pool module
in front of their data layer to serve new database connections (see Example 6-34). generic-pool
will reuse connections where possible to prevent the overhead of creating
new database connections, and the module can be used with any data library.
Example 6-34. Using the connection pool with node-db
var mysql = require( 'db-mysql' ); var poolModule ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access