SlideShare a Scribd company logo
Ulf Wendel, Oracle Web scale made easy (just kidding...) The mysqlnd replication and load balancing plugin
MySQL scale-out solutions Part 1 - Proven and designed for web-scale
The speaker says... MySQL offers a variety of scale-out solutions. Which are they, when to use and how to use from an application developers point of view?
Developer dreams Day 1 (Launch) – web site visitors The first customer Systemload: 0.1 100%
The speaker says... Let's take a PHP developer centric look at a high-growth web site. The web site is build on top of the industrial leading LAMP platform: Linux, MySQL, Apache and PHP. The day before the launch, the developer dreams the dream of his options or shares rising in value: 2x, 5x, 42x ...
Developer dreams Day 2 (Growth) – web site visitors The first customer Twitter follower Facebook friend A slashdot reader A TV moderator Systemload: 0.5
The speaker says... ... a wonderful dream. The enterprise becomes popular, IPO is on the horizon. There are many reasons for growth. As a developer you should know about the MySQL scale-out solutions to be prepared. According to a  MySQL Online Quickpoll from 2009  a majority of the MySQL users is using MySQL Replication.
Developer dreams Day 3 (Success) – scale out needed 408 Request Timeout 503 Service Unavailable 1040 SQLSTATE: 08004 (ER_CON_COUNT_ERROR) -  Too many connections Systemload: 42
The speaker says... Dear developer, help is on the way. The PHP mysqlnd replication and load balancing plugin ( PECL/mysqlnd_ms ) is a drop-in solution for your PHP MySQL web application adding MySQL scale-out support. For basic scale-out setups no code changes are required. As of PHP 5.3 all three PHP MySQL APIs ( mysql ,  mysqli ,  PDO_MySQL ) can be compiled to use the MySQL native driver for PHP ( mysqlnd ) library. Mysqlnd is shipped with PHP as of version 5.3. It is a default as of PHP 5.4.
Scale out means clustering Scale-out horizontally TCO: Commodity hardware
TCO: Proven Open Source solutions MySQL Server 1 MySQL Server 2 MySQL Server n
The speaker says... Scale out, not up. Use a cluster of commodity servers. Scale horizontally building upon proven Open Source solutions.  Try MySQL Replication, MySQL Cluster or a custom MySQL scale-out solution.
Asynchronous read scale-out Master: writes, Slaves: reads
Various topologies supported
Other use cases: backup, HA, OLTP/warehousing MySQL Replication Writes Reads Master Slave Slave Slave
The speaker says... MySQL Replication is used for read scale-out.  Writes are send to a MySQL master, reads are send to a MySQL slave. The master sends updates to the slaves in an  asynchronous  way. Slaves may not have the latest changes. MySQL Replication can also be used for: Backup – perfom blocking backup on slave
High Availablility – for example, remote slaves
Warehousing – OLTP reporting on the slaves
Real-time transactional write scale-out 99,999% availability
Auto sharding, shared nothing architecture
Web use cases: session server, tele communication MySQL Cluster SQL Node SQL Node Cluster Data Node Cluster Data Node Cluster Data Node
The speaker says... MySQL Cluster is used for real-time transactional read and write scale-out.  Data is auto-partitioned on cluster data nodes resulting in 99.999% availability. The cluster is accessed through MySQL Application Cluster Nodes. The most basic MySQL Application Cluster Node is a SQL Node – a MySQL server (mysqld). If MySQL Cluster is new to you, think of MySQL Cluster as a storage engine for the network. Like InnoDB and MyISAM are storage engines for disks.
For example, Partitioning Horizontal: rows in different tables
Vertical: columns in additional tables
... sharding Custom Cluster Shard 1 ID % 2 = 0 Node 3 Cats Node 4 Dogs Shard 2 ID % 2 = 1
The speaker says... Special requirements may make it necessary to build a custom cluster.  For example, your application may require a sharding solution.
On using any database cluster Part 2 - Required application changes
Tasks: Choose servers to execute statement
Load balance within candidates
Maintain connection pool
Automatic fail over for High Availability Application using any DB cluster MySQL Server 1 MySQL Server 2 MySQL Server n
The speaker says... All PHP applications talking to a cluster of MySQL database servers are facing the same tasks. Replacing a single database with a database cluster means changing the 1:1 relation between the application and the database into a  1:n relation . 1:n puts an additional task on the application:  find the right n, find the right server to talk to.
The plugin for all of you MySQL Server 1 MySQL Server 2 MySQL Server n
The speaker says... PECL/mysqlnd_ms is a plugin for the MySQL native driver for PHP (mysqlnd) library . The mysqlnd library is part of PHP as of version 5.3. As of 5.4 mysqlnd is a default. All three PHP MySQL APIs (mysql, mysqli, PDO_MySQL) can be compiled to use the mysqlnd library, thus  all existing PHP MySQL application are supported by the plugin. From an applications point of view a mysqlnd plugin can act as a transparent proxy.  Depending on plugin an use case, no code changes are needed.  A plugin is a drop-in solution.
Plugin focus: MySQL Replication Part 3 - Tasks, solutions and limitations
Using MySQL Replication Statement redirection Read/write split (optional: master on write)
Replication filter (table based parititioning)
Custom: callback Load Balancing Round robin, random, random once
Custom: callback Connection Pooling Lazy connections
Failover
The speaker says... The plugin is not limited to but optimized for MySQL Replication.  The common clustering support feature set focusses on MySQL Replication.  For example, statement redirection covers read/write splitting and MySQL Replication filter support. All read accesses are executed on the slave servers, all writes are performed on the master. MySQL Replication supports table based partitioning, replicating selected tables to selected slaves only. The plugin can be made aware of this.
Speaking code /* Connection user handle represents a connection pool */ $mysqli = new mysqli( "hostname" , $user, $pw, $db, $port); /* Read/Write split, Load Balancing */ $res = $mysqli->query("SELECT 'Slave speaking' FROM DUAL"); var_dump($res->fetch_all(MYSQLI_ASSOC)); { "hostname" :{ "master":{ "host":"localhost" }, "slave":{ "slave_0":{"host":"192.168.2.27"} }, } }
The speaker says... No code changes for basic use cases. Install the plugin following standard PHP PECL procedures. Create a configuration file. Enable the plugin and set its configuration file in your PHP configuration using mysqlnd_ms.enable=1 and mysqlnd_ms.ini_file=config.ini .  In the plugin configuration create a section with the name of the host your application connects to, for example „hostname“. List master and slave servers. Any connection to „hostname“ is now handled by the plugin. Using any API.
Some code changes /* Connection user handle represents a connection pool */ $mysqli = new mysqli("hostname", $user, $pw, $db, $port); / * R/W split: no SELECT, run on master */ $mysqli->query("SET @myrole='Master'); /* R/W split: overruled by SQL hint */ $sql = sprintf("/*%s*/%s", MYSQLND_MS_MASTER_SWITCH, "SELECT @myrole AS _role"); $mysqli->query($sql); var_dump($res->fetch_all(MYSQLI_ASSOC));
The speaker says... Non-trivial use cases require support by the application. In other words: existing applications may need updates, new applications need to be designed appropriately. In the example a SQL hint is used to force executio of a SELECT statement on a MySQL Replication master server. There are SQL hints for running a statement on the master, on the slave and on the last used server. Alternative: new APIs call, no win compared to SQL hint.
Why and when code changes $mysqli = new mysqli("hostname", $user, $pw, $db, $port);  $mysqli->query("SET @myrole='Master'); $mysqli->query("SELECT myrole AS _role"); Without plugin With plugin
The speaker says... An application not designed for use with a cluster assumes a connection handle always points to the same server (1:1), having the same state.  When using the plugin a connection handle represents a pool of connections (1:n), potentially having different states.  Connection switches may occur whenever a statement is executed. After the switch, the connection handle may point to a connection with a different state. Applications need to take care and hint the plugin.
A trouble maker in detail Connection State Connection settings, e.g. charsets
Transaction status
Temporary tables

More Related Content

ODP
PoC: Using a Group Communication System to improve MySQL Replication HA
ODP
Vote NO for MySQL
ODP
MySQL 5.6 Global Transaction Identifier - Use case: Failover
ODP
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
ODP
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
ODP
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency
ODP
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
ODP
MySQL 5.7 Fabric: Introduction to High Availability and Sharding
PoC: Using a Group Communication System to improve MySQL Replication HA
Vote NO for MySQL
MySQL 5.6 Global Transaction Identifier - Use case: Failover
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
MySQL 5.7 Fabric: Introduction to High Availability and Sharding

What's hot (20)

PDF
DIY: A distributed database cluster, or: MySQL Cluster
ODP
Built-in query caching for all PHP MySQL extensions/APIs
KEY
Intro to PECL/mysqlnd_ms (4/7/2011)
ODP
PHP mysqlnd connection multiplexing plugin
ODP
MySQL Group Replication
ODP
NoSQL in MySQL
ODP
MySQL 5.7 clustering: The developer perspective
PDF
HTTP Plugin for MySQL!
ODP
Award-winning technology: Oxid loves the query cache
PDF
Highly Available MySQL/PHP Applications with mysqlnd
PDF
MySQL Group Replication
PPT
Mysql high availability and scalability
PDF
Introduction to Galera
PPTX
MySQL Multi Master Replication
PDF
Methods of Sharding MySQL
PDF
High-Availability using MySQL Fabric
PDF
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
PDF
Introduction to Galera Cluster
PDF
MySQL High Availability: Managing Farms of Distributed Servers (MySQL Fabric)
PDF
Scaling with sync_replication using Galera and EC2
DIY: A distributed database cluster, or: MySQL Cluster
Built-in query caching for all PHP MySQL extensions/APIs
Intro to PECL/mysqlnd_ms (4/7/2011)
PHP mysqlnd connection multiplexing plugin
MySQL Group Replication
NoSQL in MySQL
MySQL 5.7 clustering: The developer perspective
HTTP Plugin for MySQL!
Award-winning technology: Oxid loves the query cache
Highly Available MySQL/PHP Applications with mysqlnd
MySQL Group Replication
Mysql high availability and scalability
Introduction to Galera
MySQL Multi Master Replication
Methods of Sharding MySQL
High-Availability using MySQL Fabric
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Introduction to Galera Cluster
MySQL High Availability: Managing Farms of Distributed Servers (MySQL Fabric)
Scaling with sync_replication using Galera and EC2
Ad

Viewers also liked (7)

PDF
Maintaining Low Latency While Maximizing Throughput on a Single Cluster
PPTX
Enabling Exploratory Analytics of Data in Shared-service Hadoop Clusters
PDF
My sql cluster case study apr16
DOCX
Buckle promotional campaign
PPTX
Netflix: A Case Study
PPTX
Cluster analysis
PPTX
MBA case study presentation template
Maintaining Low Latency While Maximizing Throughput on a Single Cluster
Enabling Exploratory Analytics of Data in Shared-service Hadoop Clusters
My sql cluster case study apr16
Buckle promotional campaign
Netflix: A Case Study
Cluster analysis
MBA case study presentation template
Ad

Similar to The mysqlnd replication and load balancing plugin (20)

PDF
MySQL Day Paris 2016 - MySQL HA: InnoDB Cluster and NDB Cluster
PDF
Scaling MySQL -- Swanseacon.co.uk
PDF
MySQL InnoDB Cluster and Group Replication in a nutshell hands-on tutorial
PDF
MySQL InnoDB Cluster: High Availability Made Easy!
PDF
Oracle Open World 2018 / Code One : MySQL 8.0 High Availability with MySQL I...
ODP
MySQL HA Alternatives 2010
PDF
200 million qps on commodity hardware : Getting started with MySQL Cluster 7.4
PPTX
What's new in MySQL Cluster 7.4 webinar charts
PDF
MySQL High Availability -- InnoDB Clusters
PDF
MySQL: From Single Instance to Big Data
PDF
Santo Leto - MySQL Connect 2012 - Getting Started with Mysql Cluster
PDF
OpenStack Days East -- MySQL Options in OpenStack
PDF
MySQL InnoDB Cluster in a Nutshell - Hands-on Lab
ODP
MySQL HA
PPT
MySQL Cluster Basics
PDF
MySQL 8.0 InnoDB Cluster - Easiest Tutorial
PPT
2010 12 mysql_clusteroverview
PDF
MySQL InnoDB Cluster - Meetup Oracle MySQL / AFUP Paris
ODP
MySQL HA with PaceMaker
PDF
MySQL InnoDB Cluster and NDB Cluster
MySQL Day Paris 2016 - MySQL HA: InnoDB Cluster and NDB Cluster
Scaling MySQL -- Swanseacon.co.uk
MySQL InnoDB Cluster and Group Replication in a nutshell hands-on tutorial
MySQL InnoDB Cluster: High Availability Made Easy!
Oracle Open World 2018 / Code One : MySQL 8.0 High Availability with MySQL I...
MySQL HA Alternatives 2010
200 million qps on commodity hardware : Getting started with MySQL Cluster 7.4
What's new in MySQL Cluster 7.4 webinar charts
MySQL High Availability -- InnoDB Clusters
MySQL: From Single Instance to Big Data
Santo Leto - MySQL Connect 2012 - Getting Started with Mysql Cluster
OpenStack Days East -- MySQL Options in OpenStack
MySQL InnoDB Cluster in a Nutshell - Hands-on Lab
MySQL HA
MySQL Cluster Basics
MySQL 8.0 InnoDB Cluster - Easiest Tutorial
2010 12 mysql_clusteroverview
MySQL InnoDB Cluster - Meetup Oracle MySQL / AFUP Paris
MySQL HA with PaceMaker
MySQL InnoDB Cluster and NDB Cluster

More from Ulf Wendel (8)

ODP
Data massage: How databases have been scaled from one to one million nodes
ODP
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
ODP
PHPopstar der PHP Unconference 2011
ODP
The power of mysqlnd plugins
ODP
Mysqlnd query cache plugin benchmark report
ODP
mysqlnd query cache plugin: user-defined storage handler
ODP
Mysqlnd query cache plugin statistics and tuning
ODP
Mysqlnd Async Ipc2008
Data massage: How databases have been scaled from one to one million nodes
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
PHPopstar der PHP Unconference 2011
The power of mysqlnd plugins
Mysqlnd query cache plugin benchmark report
mysqlnd query cache plugin: user-defined storage handler
Mysqlnd query cache plugin statistics and tuning
Mysqlnd Async Ipc2008

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPT
Teaching material agriculture food technology
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Digital-Transformation-Roadmap-for-Companies.pptx
Electronic commerce courselecture one. Pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
“AI and Expert System Decision Support & Business Intelligence Systems”
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Teaching material agriculture food technology
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Advanced methodologies resolving dimensionality complications for autism neur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Building Integrated photovoltaic BIPV_UPV.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks

The mysqlnd replication and load balancing plugin

  • 1. Ulf Wendel, Oracle Web scale made easy (just kidding...) The mysqlnd replication and load balancing plugin
  • 2. MySQL scale-out solutions Part 1 - Proven and designed for web-scale
  • 3. The speaker says... MySQL offers a variety of scale-out solutions. Which are they, when to use and how to use from an application developers point of view?
  • 4. Developer dreams Day 1 (Launch) – web site visitors The first customer Systemload: 0.1 100%
  • 5. The speaker says... Let's take a PHP developer centric look at a high-growth web site. The web site is build on top of the industrial leading LAMP platform: Linux, MySQL, Apache and PHP. The day before the launch, the developer dreams the dream of his options or shares rising in value: 2x, 5x, 42x ...
  • 6. Developer dreams Day 2 (Growth) – web site visitors The first customer Twitter follower Facebook friend A slashdot reader A TV moderator Systemload: 0.5
  • 7. The speaker says... ... a wonderful dream. The enterprise becomes popular, IPO is on the horizon. There are many reasons for growth. As a developer you should know about the MySQL scale-out solutions to be prepared. According to a MySQL Online Quickpoll from 2009 a majority of the MySQL users is using MySQL Replication.
  • 8. Developer dreams Day 3 (Success) – scale out needed 408 Request Timeout 503 Service Unavailable 1040 SQLSTATE: 08004 (ER_CON_COUNT_ERROR) - Too many connections Systemload: 42
  • 9. The speaker says... Dear developer, help is on the way. The PHP mysqlnd replication and load balancing plugin ( PECL/mysqlnd_ms ) is a drop-in solution for your PHP MySQL web application adding MySQL scale-out support. For basic scale-out setups no code changes are required. As of PHP 5.3 all three PHP MySQL APIs ( mysql , mysqli , PDO_MySQL ) can be compiled to use the MySQL native driver for PHP ( mysqlnd ) library. Mysqlnd is shipped with PHP as of version 5.3. It is a default as of PHP 5.4.
  • 10. Scale out means clustering Scale-out horizontally TCO: Commodity hardware
  • 11. TCO: Proven Open Source solutions MySQL Server 1 MySQL Server 2 MySQL Server n
  • 12. The speaker says... Scale out, not up. Use a cluster of commodity servers. Scale horizontally building upon proven Open Source solutions. Try MySQL Replication, MySQL Cluster or a custom MySQL scale-out solution.
  • 13. Asynchronous read scale-out Master: writes, Slaves: reads
  • 15. Other use cases: backup, HA, OLTP/warehousing MySQL Replication Writes Reads Master Slave Slave Slave
  • 16. The speaker says... MySQL Replication is used for read scale-out. Writes are send to a MySQL master, reads are send to a MySQL slave. The master sends updates to the slaves in an asynchronous way. Slaves may not have the latest changes. MySQL Replication can also be used for: Backup – perfom blocking backup on slave
  • 17. High Availablility – for example, remote slaves
  • 18. Warehousing – OLTP reporting on the slaves
  • 19. Real-time transactional write scale-out 99,999% availability
  • 20. Auto sharding, shared nothing architecture
  • 21. Web use cases: session server, tele communication MySQL Cluster SQL Node SQL Node Cluster Data Node Cluster Data Node Cluster Data Node
  • 22. The speaker says... MySQL Cluster is used for real-time transactional read and write scale-out. Data is auto-partitioned on cluster data nodes resulting in 99.999% availability. The cluster is accessed through MySQL Application Cluster Nodes. The most basic MySQL Application Cluster Node is a SQL Node – a MySQL server (mysqld). If MySQL Cluster is new to you, think of MySQL Cluster as a storage engine for the network. Like InnoDB and MyISAM are storage engines for disks.
  • 23. For example, Partitioning Horizontal: rows in different tables
  • 24. Vertical: columns in additional tables
  • 25. ... sharding Custom Cluster Shard 1 ID % 2 = 0 Node 3 Cats Node 4 Dogs Shard 2 ID % 2 = 1
  • 26. The speaker says... Special requirements may make it necessary to build a custom cluster. For example, your application may require a sharding solution.
  • 27. On using any database cluster Part 2 - Required application changes
  • 28. Tasks: Choose servers to execute statement
  • 29. Load balance within candidates
  • 31. Automatic fail over for High Availability Application using any DB cluster MySQL Server 1 MySQL Server 2 MySQL Server n
  • 32. The speaker says... All PHP applications talking to a cluster of MySQL database servers are facing the same tasks. Replacing a single database with a database cluster means changing the 1:1 relation between the application and the database into a 1:n relation . 1:n puts an additional task on the application: find the right n, find the right server to talk to.
  • 33. The plugin for all of you MySQL Server 1 MySQL Server 2 MySQL Server n
  • 34. The speaker says... PECL/mysqlnd_ms is a plugin for the MySQL native driver for PHP (mysqlnd) library . The mysqlnd library is part of PHP as of version 5.3. As of 5.4 mysqlnd is a default. All three PHP MySQL APIs (mysql, mysqli, PDO_MySQL) can be compiled to use the mysqlnd library, thus all existing PHP MySQL application are supported by the plugin. From an applications point of view a mysqlnd plugin can act as a transparent proxy. Depending on plugin an use case, no code changes are needed. A plugin is a drop-in solution.
  • 35. Plugin focus: MySQL Replication Part 3 - Tasks, solutions and limitations
  • 36. Using MySQL Replication Statement redirection Read/write split (optional: master on write)
  • 37. Replication filter (table based parititioning)
  • 38. Custom: callback Load Balancing Round robin, random, random once
  • 39. Custom: callback Connection Pooling Lazy connections
  • 41. The speaker says... The plugin is not limited to but optimized for MySQL Replication. The common clustering support feature set focusses on MySQL Replication. For example, statement redirection covers read/write splitting and MySQL Replication filter support. All read accesses are executed on the slave servers, all writes are performed on the master. MySQL Replication supports table based partitioning, replicating selected tables to selected slaves only. The plugin can be made aware of this.
  • 42. Speaking code /* Connection user handle represents a connection pool */ $mysqli = new mysqli( "hostname" , $user, $pw, $db, $port); /* Read/Write split, Load Balancing */ $res = $mysqli->query("SELECT 'Slave speaking' FROM DUAL"); var_dump($res->fetch_all(MYSQLI_ASSOC)); { "hostname" :{ "master":{ "host":"localhost" }, "slave":{ "slave_0":{"host":"192.168.2.27"} }, } }
  • 43. The speaker says... No code changes for basic use cases. Install the plugin following standard PHP PECL procedures. Create a configuration file. Enable the plugin and set its configuration file in your PHP configuration using mysqlnd_ms.enable=1 and mysqlnd_ms.ini_file=config.ini . In the plugin configuration create a section with the name of the host your application connects to, for example „hostname“. List master and slave servers. Any connection to „hostname“ is now handled by the plugin. Using any API.
  • 44. Some code changes /* Connection user handle represents a connection pool */ $mysqli = new mysqli("hostname", $user, $pw, $db, $port); / * R/W split: no SELECT, run on master */ $mysqli->query("SET @myrole='Master'); /* R/W split: overruled by SQL hint */ $sql = sprintf("/*%s*/%s", MYSQLND_MS_MASTER_SWITCH, "SELECT @myrole AS _role"); $mysqli->query($sql); var_dump($res->fetch_all(MYSQLI_ASSOC));
  • 45. The speaker says... Non-trivial use cases require support by the application. In other words: existing applications may need updates, new applications need to be designed appropriately. In the example a SQL hint is used to force executio of a SELECT statement on a MySQL Replication master server. There are SQL hints for running a statement on the master, on the slave and on the last used server. Alternative: new APIs call, no win compared to SQL hint.
  • 46. Why and when code changes $mysqli = new mysqli("hostname", $user, $pw, $db, $port); $mysqli->query("SET @myrole='Master'); $mysqli->query("SELECT myrole AS _role"); Without plugin With plugin
  • 47. The speaker says... An application not designed for use with a cluster assumes a connection handle always points to the same server (1:1), having the same state. When using the plugin a connection handle represents a pool of connections (1:n), potentially having different states. Connection switches may occur whenever a statement is executed. After the switch, the connection handle may point to a connection with a different state. Applications need to take care and hint the plugin.
  • 48. A trouble maker in detail Connection State Connection settings, e.g. charsets
  • 52. Session system variables and user variables
  • 53. Prepared statements (API and SQL PREPARE)
  • 56. Units of work from an application point of view
  • 57. The speaker says... No client library, no client proxy can monitor all state changing actions. Applications in need for a certain connection state need to control the plugins connection switches using : SQL hints MYSQLND_MS_SLAVE_SWITCH
  • 59. MYSQLND_MS_LAST_USED_SWITCH Callback to replace built-in server selection logic
  • 60. Trouble maker transaction Unit of work Part of the connection state
  • 61. Application to prevent connection switches Workaround PHP 5.3: SQL hints must be used
  • 62. PHP 5.4: API calls monitored, switch auto-disabled Replication cluster tip Always run on master, only master has latest data!
  • 64. The speaker says... A transaction is part of the connection state. For the duration of a transaction, server switches must be disabled. Transactions can be controlled by SQL statements and API calls. The plugin cannot monitor all of them with reasonable effort. Thus, you must hint the plugin. Disable load balancing by adding SQL hints (MYSQLND_MS_LAST_USED_SWITCH), you install your own load balancer logic (callback). As of PHP 5.4, the plugin monitors API calls to auto-disable switching.
  • 65. Slave request read-only: statement begins with SELECT
  • 66. read-only: statement begins with slave SQL hint
  • 67. custom: callback picks slave for statement Redirection: Read/Write split Writes Reads (SELECT, SQL hint) Master Slave Slave Slave
  • 68. The speaker says... MySQL Replication is for read scale-out. Read-only statements shall go to slaves, writes to the master. The plugin automatically redirects a statement to a slave, if the statement begins with SELECT or the MYSQLND_MS_SLAVE_SWITCH SQL hint. All other statements are considered as write operations and executed on the master. By default SHOW or CALL will be run on the master. It is a design principle of the plugin that all automatic decisions can be overruled. A callback can be installed to pick the server for running a statement on, if needed.
  • 69. Optimization: use master only after write MySQL Replication is asynchronous
  • 70. MySQL slaves can lag behind
  • 71. Most applications need current data after write Redirection: Master on write Writes, Reads after write Reads before write Master Slave Slave
  • 72. The speaker says... The master_on_write configuration option is an optimization of the R/W split. MySQL Replication slaves can lag behind the master because MySQL Replication is asynchronous. When using master_on_write all reads made after a write are executed on the master for the duration of the web request. Applications that perform a write often need to rely on fresh reads afterwards. Think of a web shop creating an order, writing to the master. If reading again from the order table, within the same web request, the application should access the master to make sure the previously written is available.
  • 73. MySQL Replication filter support Slaves replicate different databases/tables Redirection: Partitioning Writes Reads (Filter) DB_Report DB_Shop DB_Report DB_Internal DB_Shop DB_Shop Master Slave Slave Slave
  • 74. The speaker says... MySQL Replication supports partitioning: slaves can be configured to replicate only selected databases and tables using MySQL Replication filter. Filter help to reduce the amount of internal replication messages. Less frequently accessed data can be replicated to only few slaves. In future versions, plugin can be made aware of the filtering rules for appropriate statement redirection. Every statement is analyzed to find a slave for reading. If no match, the plugin redirects the statement to the master.
  • 75. Built-in or user-defined through callback Round robin: iterate over slaves for each read
  • 76. Random: pick random slave for each read
  • 77. Random once: pick random slave for all reads Load Balancing Writes Load Balancing of Reads Master Slave Slave Slave
  • 78. The speaker says... MySQL Replication is about read scale-out. The plugin helps distributing the load over the read-only slaves. The best and default load balancing stategy is the one with the lowest impact on the connection state: random once. Minimize switches for the life-span of your PHP script. Pick a random slave during startup and use it for all reads. The life-span of PHP is the of a web request. It is short. Two web requests will end up on two random slaves. Load balancing adaptive to server health can be achieved through a user-defined callback.
  • 79. Caution: connection state change Default: failover=disabled, raise exception
  • 80. Optional: failover=master, automatic Pooling: connection failover Writes Reads Master Slave Slave Slave
  • 81. The speaker says... Automatic and transparent failover is impossible with stateful connections. The plugin does not and cannot know the full state of a connection. Parts of the state are only known to the server. By default the plugin leaves it to the application to handle a connection failure. Like without the plugin, it is up to the application to decide what to do. Applications using stateless connections can enable automatic failover to the master.
  • 82. Do not open connection before needed Delay open until statement execution
  • 83. Reduce number of connections, reduce load Pooling: lazy connections Writes Reads Master Slave Slave Slave
  • 84. The speaker says... By default, to reduce the connection pool size of every PHP web request, connections are not opened before executing a statement. Imagine you have 50 concurrent web requests, you configured four slaves and, you are using random once. If not using lazy connections, the plugin opens 50 x 4 = 200 slave connections but uses at max 50 of them (random once). Every connection occupies resources – ever seen a PHP application opening connections before using? There are many badly written ones...
  • 86. Documentation and resources php.net/mysqlnd_ms Installation, Quickstart, Concepts, Reference php.net/mysqlnd Introduction, Changes, C plugin API
  • 87. THE END Credits: Andrey Hristov, Johannes Schlüter - Contact: ulf.wendel@oracle.com