How to Master/Slave switch, load balancing in CakePHP
Asked Answered
S

3

10

I need to implement master/slave/load balancing into an existing site.

Does anyone use these (or other) implementations for master/slave switching?

The resources I found on how to implement master/slave into Cake:.

I'm getting number 1) to work most of the times but it has trouble with some of the joins.

I welcome new sources, hacks or mods for master/slave implementation as for now I can't get my head around it.

(Cake version I am using atm is 1.2) (I'm cross posting this on CakePHP's google groups http://groups.google.co.uk/group/cake-php/browse_thread/thread/4b77af429759e08f)

Sham answered 26/8, 2010 at 12:3 Comment(1)
I would like to find a good tutorial on how this works, some graphs as well, but not for Cake, for general self built applications.Statolith
S
0

Take a look at this tutorial in regards to Master/Slave over several nodes.

http://www.howtoforge.com/setting-up-master-master-replication-on-four-nodes-with-mysql-5-on-debian-etch

This may help you understand better.

Statolith answered 26/8, 2010 at 12:45 Comment(1)
Hi Robert, thank you for the link. What I am looking for is specifically how to either implement the master/slave based on the 3 sources I gave or a total new implementation into CakePHP. As it seems to have some difficulty deeper within Cake.Sham
H
0

As far as I can tell this happens if your model has relationships with models that do not use the same behaviour. Please correct me if this assumption is wrong.

All models have meta-data, which CakePHP accumulates using a DESCRIBE query on the database, if this data is not present your joins will be broken. This meta-data is database config specific.

CakePHP uses this meta-data to populate the $this->_schema property. SQL joins are built with data from the $this->_schema property and I guess this is where your issue lies, the database introduced by this MasterSlave switch behaviour do not have any model meta-data for tables associated with the model.

A solution would be to update your behaviour so that it only switches selectively on read and writes. Add this behaviour to all related models. i.e Any model that is related using hasOne, hasMany etc should also use the same behaviour.

In essence all models that are related should write to the same database and read from the same database.

The bonus of this solution is you will share the same database connections.

Homeroom answered 16/3, 2011 at 9:20 Comment(0)
T
0

Your web app seems to be multi tier, you need to scale each tier individually:

  • The web layer, i.e. tha CakePHP app can be spread across multiple web servers. This is easy to do, as the code itself is idempotent. You should look into how to load balance apache servers, it is not a big deal. Webservers have quite high throughput though, so if you have a bottleneck here, you might improve your code/caching strategy instead. (Use memcache instead of file caches for example.) If you depend on the file system (uploads for example) this becomes a bit more complex, as it must become distributed or separated.

  • The data layer. There are various tutorials how to scale/load balance MySQL already linked by others.

Albeit first I would suggest to make benchmarks. (Premature optimization is the root of all evil.) You must know first where the bottlenecks are, where the throughput should scale. Often you can optimize queries, caching, or make thing cacheable in the first place. You must also be clear in your goals: scalability? fault tolerance?

Teetotal answered 24/11, 2011 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.