{"id":219,"date":"2016-02-01T00:02:44","date_gmt":"2016-02-01T00:02:44","guid":{"rendered":"http:\/\/www.hitsteps.com\/blog\/?p=219"},"modified":"2016-02-01T13:19:26","modified_gmt":"2016-02-01T13:19:26","slug":"dns-load-balancing-and-auto-ip-failover","status":"publish","type":"post","link":"http:\/\/www.hitsteps.com\/blog\/dns-load-balancing-and-auto-ip-failover\/","title":{"rendered":"DNS Load Balancing and Auto IP Failover"},"content":{"rendered":"<p>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.<\/p>\n<p>Afterall, A load balancer is just another point of break in case your load balancer crash!<\/p>\n<p>Please note that this load balancing won&#8217;t check status of servers and randomly return IP addresses (but your visitor won&#8217;t see a crashed server!)<\/p>\n<p>Another side effect is that\u00a0DNS load balancer won&#8217;t keep same visitor on same server on each page refresh or link change, so you need a shared session for all servers.<\/p>\n<p>PHP&#8217;s default\u00a0session uses\u00a0file system and isn&#8217;t made for sharing between servers, so you&#8217;ll need a shared session manager. memcached is a great option here. sync\u00a0all your server PHP sessions by connecting them to memcached of a server.<\/p>\n<p>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)<br \/>\nIf 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.<br \/>\nBefore MySQL 5.7, multi-master replication was done using round robin replication which causes big problem in case one of servers go offline.<\/p>\n<p>Now, lets see what will happen when you add multiple A records to your DNS.<br \/>\nso, lets say we have this record:<\/p>\n<pre>A \u00a0 www.hitsteps.com \u00a0 123.123.123.123<\/pre>\n<p>We change it to:<\/p>\n<pre>A \u00a0 www.hitsteps.com \u00a0 123.123.123.123\r\nA \u00a0 www.hitsteps.com \u00a0 123.123.123.124<\/pre>\n<p>Where 123.123.123.123 is your first server&#8217;s IP address and 123.123.123.124 is your second server IP address.<\/p>\n<h3>And it is done!<\/h3>\n<p>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.<br \/>\nIt is really random and we cannot<\/p>\n<p>Now, if one of your servers crash and IP address become unavailable, browser will choose another available IP address, therefor your visitors won&#8217;t see any downtime. Once crashed server come back online, browser will start serving pages using this server again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"http:\/\/www.hitsteps.com\/blog\/dns-load-balancing-and-auto-ip-failover\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">DNS Load Balancing and Auto IP Failover<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":228,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers"],"_links":{"self":[{"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":1,"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":222,"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions\/222"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/media\/228"}],"wp:attachment":[{"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hitsteps.com\/blog\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}