The mysqlnd replication and load balancing plugin is implemented as a PHP extension. It is written in C and operates under the hood of PHP. During the startup of the PHP interpreter, in the module init phase of the PHP engine, it gets registered as a mysqlnd plugin to replace selected mysqlnd C methods.
At PHP run time it inspects queries send from mysqlnd (PHP) to the MySQL server. If a query is recognized as read-only it will be sent to one of the configured slave servers. Statements are considered read-only if they either start with SELECT, the SQL hint /*ms=slave*/ or a slave had been choose for running the previous query and the query starts with the SQL hint /*ms=last_used*/. In all other cases the query will be sent to the MySQL replication master server.
The plugin takes care internally of opening and closing the database connections to the master server and the slave servers. From an application point of view there continues to be only one connection handle. However, internally, this one public connection handle represents a pool of internal connections managed by the plugin. The plugin proxies queries to the master server and the slave ones using multiple connections.
Database connections have a state consisting, for example, transaction status, transaction settings, character set settings, temporary tables. The plugin will try to maintain the same state among all internal connections, whenever this can be done in an automatic and transparent way. In cases where it is not easily possible to maintain state among all connections, such as when using BEGIN TRANSACTION, the plugin leaves it to the user to handle. Please, find further details below.