We had a scenario here, which we needed to distribute load between few servers, buying a load balancer was not an option (or at least we wanted to experiment with DNS Load balancing) and it worked great.
Afterall, A load balancer is just another point of break in case your load balancer crash!
Please note that this load balancing won’t check status of servers and randomly return IP addresses (but your visitor won’t see a crashed server!)
Another side effect is that DNS load balancer won’t keep same visitor on same server on each page refresh or link change, so you need a shared session for all servers.
PHP’s default session uses file system and isn’t made for sharing between servers, so you’ll need a shared session manager. memcached is a great option here. sync all your server PHP sessions by connecting them to memcached of a server.
Next step, you need to make sure your database is replicated as master-master and file system is synced (you can use lsyncd for file system sync)
If you have more than 2 servers, you might need to use MySQL 5.7 above, because they offer multi-source master replication, so your slave server can connect to 2 masters at same time.
Before MySQL 5.7, multi-master replication was done using round robin replication which causes big problem in case one of servers go offline.
Now, lets see what will happen when you add multiple A records to your DNS.
so, lets say we have this record:
A www.hitsteps.com 126.96.36.199
We change it to:
A www.hitsteps.com 188.8.131.52 A www.hitsteps.com 184.108.40.206
Where 220.127.116.11 is your first server’s IP address and 18.104.22.168 is your second server IP address.
And it is done!
So here is what happens in our experience. Browser randomly choose one of these IP addresses. since file system and session and database is cached, visitor will always see correct page regardless of which server he is connected to.
It is really random and we cannot
Now, if one of your servers crash and IP address become unavailable, browser will choose another available IP address, therefor your visitors won’t see any downtime. Once crashed server come back online, browser will start serving pages using this server again.