Hi all,
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
counter = 0
hosts = array('192.168.1.1:3306', '192.168.1.2:3306')
//the below code needs to be atomic
GET A GLOBAL LOCK
counter = counter+1
RELEASE THE LOCK
host = hosts[counter % len(hosts)]
Thanks