Impact of MySQL multihost client connections(master-slave)

Background:

When there is MySQL replication, with slaves replicating the master asynchronously, the application can be configured to connect to both master and slave. The MySQL Replication Driver can be used to load balance between different slaves, or choose slave over master for specific queries.
The following spring datasource will do the work,

To send queries to slaves, we need to set the JDBC Connection to readOnly. This can be achieved by setting the readyOnly attribute of @Transactional to be True. Actually, it depends on the configured TransactionManager, it works for DataSourceTransactionManager, but may not work for other transaction managers. If it's not working, you can get the current connection in the application code using DataSourceUtils.getConnection(DataSource) and set readOnly using Connection.setReadOnly(True). We used this way type of connections to scale out our reads and to increase the utilisation of rather idle slaves.


Impact:

The effect was now each abstract connection object holds as many hosts in the connection URL. Let's assume that the max connection limit setting is enabled and the limit is reached for one of the slave host. Now, the application won't be able to create any connection at all, since it can't open a connection to that specific slave, which had hit its limit. Even though all the other mysql servers can accept incoming connections,  the application won't be able to create new connection. For it to create a new connection, at the time of creation, master and all the slaves must be able to accept connections or the slave must be down. If the slave is totally down, we are able to created a new connection successfully, since this scenario is handled by allowSlaveDownConnections flag.

After this issue, we learnt that it's better to separate the master and slave as two different datasources at the application level and use it accordingly.

Comments

Popular posts from this blog

Spring data mongodb secondary node reads

Spring mongodb connection pool limit

Spark streaming with kafka and HDFS