Load Balancing for PHP and MySQL

download Load Balancing for PHP and MySQL

If you can't read please download the document

Transcript of Load Balancing for PHP and MySQL

PowerPoint-Prsentation

Ulf Wendel, Oracle

PECL/mysqlnd_ms 1.2, 2011

Load Balancing for
PHP and MySQL

The speaker says...

PECL/mysqlnd_ms is a free and open source plugin for the mysqlnd library. It makes using any kind of MySQL cluster easier. Among others, if offers read-write splitting, load balancing, fail over and service levels.
The mysqlnd library ships with PHP as of PHP 5.3. It is a drop-in replacement for the MySQL Client Library (libmysql). Mysqlnd is the php.net default on all platforms since PHP 5.4. All PHP MySQL extensions/APIs can be compiled to use mysqlnd.

PECL/mysqlnd_ms is a mostly transparent mysqlnd plugin.

Database load balancing

Single DBMS is a single point of failure

Single DBMS is hard to scale

The load balancing challenge

HTTP ServerHTTP ServerApp Server (PHP)MySQL Server

InternetApp Server (PHP)App Server (PHP)HTTP Server

The speaker says...

A single DBMS has no load balancing challenge. But, using a single DBMS is a second best solution.

A DBMS failure interrupts service. Scaling vertically by growing hardware capacity has its limits. Use of commodity hardware is not possible. Elastic-fantastic is nothing but a dream for peak loads.

Use a cluster, face the load balancing challenge, let MySQL help you with the additional task.

Cluster for scalability and availability

MySQL Replication: read scale-out

MySQL Cluster: read and write scale-out, 99.999%

MySQL 3rd party: leveraging MySQL technology

Use a MySQL Cluster

HTTP ServerHTTP ServerApp Server (PHP)MySQL NodeApp Server (PHP)App Server (PHP)HTTP ServerMySQL NodeMySQL NodeDBMS Connection Load Balancing

The speaker says...

MySQL users are offered a wide range of scale-out solutions to choose from. Including 3rd party vendors which have discovered the potential of MySQL technology.

MySQL Replication drives the web. It stands for asynchronous read scale-out. Node failover recently became easier with client-side global transaction emulation in PECL mysqlnd_ms 1.2 and upcoming server support in MySQL 5.6.4. MySQL Cluster gives 99.999% availability to anybody in need for read and write scale-out. On commodity hardware, of course. Synchronous, of course. Of course, you'll try, won't you?

Simple 1:1 mapping!

Requires synchronous cluster

No fault tolerance: node failure = stack failure

Inefficient resource usage: no balancing

Whole stack cluster

HTTP ServerHTTP ServerApp Server (PHP)MySQL NodeApp Server (PHP)App Server (PHP)HTTP ServerMySQL NodeMySQL Node

The speaker says...

In a synchronous cluster, for example, if using MySQL Cluster, all nodes have all the data. Thus, every application server could be assigned to one DBMS node. Easy, fast and good for MySQL Cluster but with limits. No good for asynchronous MySQL Replication.

Limit: DBMS node failure includes application node failure. Client should have additional fail over logic.

Limit: Over- and undercapacity of a DBMS node cannot be compensated. Clients cannot switch to more powerful nodes. Overcapacity of a MySQL node cannot be used by other clients. Different hardware size is ignored.

Free and open source MySQL Proxy

No application changes, free 3rd party proxy scripts

MySQL node failure does not stop application server

Good resource usage, dynamic adoption possible

Proxy approach - Pro

HTTP ServerHTTP ServerApp Server (PHP)MySQL NodeApp Server (PHP)App Server (PHP)HTTP ServerMySQL NodeMySQL NodeMySQL Proxy

The speaker says...

A transparent MySQL Proxy based solution requires no application changes. Clients connect to the Proxy using MySQL Client Server Protocol, as if the MySQL Proxy was a MySQL Server.

Proxy can compensate for DBMS failures. It can react to temporary and permant outages.

Proxy can adapt load balancing dynamically. Taking into account node hardware size, current node load, latency, location, fantasy sets the limits here for self-written or 3rd party Proxy scripts.

Additional component

Complex architecture, can be single point of failure

C and Lua are no natural match for PHP lovers

Adds latency: from client to proxy to node

Proxy approach - Con

HTTP ServerHTTP ServerApp Server (PHP)MySQL NodeApp Server (PHP)App Server (PHP)HTTP ServerMySQL NodeMySQL NodeMySQL Proxy

The speaker says...

MySQL Proxy is a great performer!

But, MySQL Proxy adds complexity to the stack. MySQL Proxy needs to be managed. MySQL Proxy is build around C and Lua. C and PHP would be a better match for PHP users. Wrongly used, MySQL Proxy becomes a single point of failure. It is single threaded. This bares the risk of tasks (Proxy scripts) blocking each other.

But, MySQL Proxy adds latency. Although, it can be minimized significantly running MySQL Proxy on the App Server to avoid use of TCP/IP between PHP and Proxy.

Client application handles load balancing

No single point of failure

No additional latency

Highly configurable and adaptive

Client-side load balancer - Pro

HTTP ServerHTTP ServerApp Server (PHP)MySQL NodeApp Server (PHP)App Server (PHP)HTTP ServerMySQL NodeMySQL Node

The speaker says...

A client-side approach to the problem is promising, if client applications can be changed. It has most Pro's of the previous approaches.

The load balancer is part of the client application. It scales by client and it fails by client. Scalability is given and there is no single point of failure. No additional latency occurs.

Load balancing can be adaptive for good resource usage. DBMS node failures do not block clients, fail over is possible.

Client application handles load balancing

Application must be designed for cluster user

No drop-in solution for existing applications

PHP is stateless: adaptive load balancing is difficult

Client-side load balancer - Con

HTTP ServerHTTP ServerApp Server (PHP)MySQL NodeApp Server (PHP)App Server (PHP)HTTP ServerMySQL NodeMySQL Node

The speaker says...

The major downside of a client-side application based solution is the need to change the application. Application changes must be done even for basic cluster use cases. Modifications to source code may not be possible. They may complicate upgrades of standard software. They may cause lots of work thus become expensive.

Load balancing is part of PHP thus stateless. This is both a Pro and a Con. It is difficult to hint other clients about node failures. On the other hand, shared nothing is not a bad idea either.

Client-side and driver based: PECL/mysqlnd_ms

Pro: all previous

Synchronous cluster: no application changes

Asynchronous cluster: no change for basic cases

Is driver based the solution?

HTTP ServerHTTP ServerApp Server (PHP)MySQL NodeApp Server (PHP)App Server (PHP)HTTP ServerMySQL NodeMySQL Node

PECL/mysqlnd_msPECL/mysqlnd_msPECL/mysqlnd_ms

The speaker says...

A driver based client-side solution has all Pro's!

Considering load balancing aspect only, no application changes are required. If using MySQL Cluster, a synchronous cluster, no application changes are required. If using MySQL Replication, an asynchronous cluster that needs read-write splitting, the need for application changes is significantly reduced!

The PECL mysqlnd_ms installation can be part of the next PHP deployment, done anyway for security considerations.
No new procedure. If any issues, simply disable extension.

Client may need to hint Load Balancer

Transaction boundaries: only API monitored

Read-write splitting edge-cases: may need hints

PECL/mysqlnd_ms - Con

HTTP ServerHTTP ServerApp Server (PHP)MySQL NodeApp Server (PHP)App Server (PHP)HTTP ServerMySQL NodeMySQL Node

PECL/mysqlnd_msPECL/mysqlnd_msPECL/mysqlnd_ms

The speaker says...

PECL/mysqlnd_ms cannot solve all MySQL Replication and MySQL Protocol shortcomings.

Load balancing must be disabled during transactions. The start and end of a transaction are detected monitoring API calls. SQL is not monitored. The MySQL Protocol is no help, applications must be careful, eventually hint PECL/mysqlnd_ms.

Connections have a state. Read-write splitting can trigger node switches, applications must hint the plugin, if stateful connections are used.

Continiously evolving...

Works with all PHP MySQL extensions

Mostly transparent drop-in solution

Load Balancing and Fail over

Read-write splitting

Service (consistency) levels

User can control all automatic actions

70+ pages documentation: hands on and reference

Stable, more test code than C code in the plugin

Dear Java users, MySQL Connnector/J can ...

Summary - PECL/mysqlnd_ms

THE END

Credits: Andrey Hristov - Contact: [email protected]