Round robin connects to mysql

skyline_GTR

New Member
I want to load balance queries to 2 mysql servers, in my application.What would be the best way to do that, so that each query goes to a different db server?I am thinking of having a global counter which is incremented each time a connection is made, mod the counter with my db servers in order to get the host I should connect. The above process however needs to be atomic, so that no 2 requests see the same counter.How should I go about it? Use a semaphore lock, mysql's get_lock()?pseudo code\[code\]counter = 0hosts = array('192.168.1.1:3306', '192.168.1.2:3306')//the below code needs to be atomicGET A GLOBAL LOCKcounter = counter+1RELEASE THE LOCKhost = hosts[counter % len(hosts)]\[/code\]Thanks
 
Back
Top