This article is useful for those who use the connection pool in the Nodes.js + mysql module environment. This is the second part of introducing the mysql-pool-booster module, which allows you to use the connection pool with better performance and functionality than original module.
Have you ever heard about PoolCluster? This feature was added to mysql module in 2013, but many people who have used mysql module for a long time will not know that. (It was my first contribution for open source project.)
PoolCluster provides the functionality to easily handle multiple connection pools. Please see this official document if you want to know about basic usage and options more.
Suddenly, I thought I had to improve this feature. Just like my fate.
# The beginning of improvement
For example, you have the following server groups.
Server group
Description
core
Core information is stored (Master, Slave)
data-*
Sharded data is stored
Execute an INSERT query from a master server in the core group.
Get the information from a slave server in the core group.
Execute a SELECT query from one of servers in the data group.
The implementation using PoolCluster is as follows.
I thought there were a lot of things to improve.
# The direction of improvement
Three functions have been added.
1. Create with options
I usually define the configuration as JSON, so adding a node using the add function is inconvenient. That’s because I need to make additional codes (logic to read and initialize the configuration). Now, you can create it with options(nodes > clusterId) instead of using add function.
2. Writer & Reader
In many cases, many people will use the connection pool consisting of a master and a slave server. In this case, I think it’s easier to use by accessing the concept of Writer and Reader rather than the ID base defined. You can set it up with options(nodes > clusterType) or functions(addWriter, addReader, add).
3. Sharding
If you want to use the application-level sharding, all you need to do is set it up with options(shardings) or addSharding function. In complex cases, I think you’d better use another professional sharding platform.
You can use the getSharding or getShardingConnection(getShardingReaderConnection, getShardingWriterConnection) function with argument to get a connection.
# The result of improvement
Here’s an example of what you’ve seen in the previous version. Do you think it’s a little simpler?
# Using the improved version
Install the mysql-pool-booster module with the mysql module installed,
If you convert an existing mysql object, you can use the improved features.
# If you have any problems
Please leave an issue in github or send me an e-mail. I’ll do my best to solve your problem as soon as possible. I have tried to make it work without problems, but since it’s a new project that has just started, please apply it to your service after enough verification steps.