News Archive
PhpRiot Newsletter
Your Email Address:

More information

Replication plugin A filter A conquer = 1.1.0 coming

Note: This article was originally published at Planet PHP on 28 September 2011.
Planet PHP

The soon to be announced version 1.1.0-beta of the mysqlnd replication and load balancing plugin (PECL/mysqlnd_ms) for PHP introduces a new concept of filters to the plugin. Filters take a list of servers to pick one or more of it. Filters can be chained, similar command line tools. Imagine a future with a filter chain like: user_multi | roundrobin, table_partitioning | random_once, table_partitioning | adaptive_loadbalancer, a For example, user_multi | roundrobin will first invoke a callback and then apply static round robin load balancing to the servers returned by the callback set with user_multi . Or, table_partitioning | adaptive_loadbalancer would first apply replication partitioning rules (db.a - server_a, db.b - server_ba) and then a candicate is choosen for statement executions using some sort of adaptive load balancing implemented by the adaptive_loadbalancera. future music, though!

The filters of 1.1.0

The filter concept is not fully developed in 1.1.0-beta. Foundations had to be laid first. Backward compatibility of the plugins configuration file format was broken between alpha and beta. As of 1.1.0, JavaScript Object Notation (JSON) format is used instead of PHP (php.ini) configuration file format. JSON allows defining hierarchical data structures and is easy to process, thanks to PHPs build in JSON support. Please, follow the links to the mysqlnd_ms manual for details.

{ "myapp": { "master": { "master_0": { "host": "localhost" } }, "slave": { "slave_0": { "host": "", "port": "3306" }, "slave_1": { "host": "", "port": "3306" } }, "filters": { "roundrobin": [ ] } } }

The following filters are included in 1.1.0-beta.

How filters work

Whatever kind of MySQL replication cluster your PHP application may use, it will be faced with two tasks whenever a SQL statement is to be executed:

  1. Identify the servers capable of executing a statement
    • Read or write request?
    • Accessing a database or table not replicated to all nodes?
  2. Choose one of the candidates to execute the statement
    • Random choice?
    • Round robin load balancing?
    • Load balancing adaptive to system load?

By answering those questions an application filters the list of configured replication nodes until one node is left. And this is, what filters are about. Filters stepwise reduce the list of candidates for statement execution. In pseudo-code this looks like:

foreach (filters as filter) { servers = filter-reduce(statement, servers); if (1 == count(servers)) break; }

Filtering is an iterative process. There can be more than one filter. In a way you can compare chaining filters to chaining small, easy to maintain small command line tools on a shell using the shell's pipe operator. Instead of having one complex, hard to use super-duper tool you combine small tools to get the job done.

grep -R "filter" en/reference/mysqlnd_ms/ | sort | head -n 1

Like a command l

Truncated by Planet PHP, read more at the original (another 5565 bytes)