{"id":19423,"date":"2015-05-06T10:51:50","date_gmt":"2015-05-06T05:21:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=19423"},"modified":"2016-08-30T10:47:08","modified_gmt":"2016-08-30T05:17:08","slug":"load-blancing-of-amazon-rds-read-replica","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/load-blancing-of-amazon-rds-read-replica\/","title":{"rendered":"Load Blancing of Amazon RDS Read Replica"},"content":{"rendered":"<p>AWS provide some intensive services and these are easily manageable and as being admin there is less overhead to scale up your application infrastructure. In <a title=\"aws automation\" href=\"http:\/\/www.tothenew.com\/devops-aws\">automated deployments<\/a> if we are planing to horizontally scale your database, with help of a single click we can populate read replica of our running database.<\/p>\n<p>But to handle large volume of traffic we have to scale database with two or more read instance. To trading off availability and consistency it&#8217;s necessary to put all read instance under single load balancer. So, the traffic would be equally distributed and manageable. We will configure haproxy as load balancer on Ec2 instance to serve read requests.<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">Here is common architecture which we are using:-<\/h3>\n<p>1. Web application:- Deployed on Web Server.<br \/>\n2. Load balancer:- Ec2 instance with ubuntu 14.<br \/>\n3. RDS:- Two Read Replica of master RDS.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/Untitled-Diagram-2.jpg\" alt=\"\" \/><\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">Configuration Steps:-<\/h3>\n<p>1. RDS Configuration.<br \/>\n2. HA PROXY Configuration.<br \/>\n3. Load Balance testing.<\/p>\n<p><strong>1. RDS Configuration.<\/strong><br \/>\nWe need two users on database, the first user (ha_check) for haproxy active check and second user (ha_read) for integration within application. We will create these users on master database as changes will replicate to slave servers by itself. First off all create &#8220;ha_check&#8221; user which will be accessible from load balancer (10.0.0.204)<\/p>\n<p>[js]mysql -uroot -pPASSWORD -hmaster.xxxxxxx.ap-southeast-1.rds.amazonaws.com<br \/>\n&gt; create user &#8216;ha_check&#8217;@&#8217;10.0.0.204&#8217;;<br \/>\n&gt; flush privileges;[\/js]<\/p>\n<p>Now create second user with read only privileges.<\/p>\n<p>[js]<br \/>\n&gt;grant select, show view on test.* to &#8216;ha_read&#8217;@&#8217;10.0.0.204&#8217; identified by &#8216;password&#8217;;<br \/>\n&gt; flush privileges;<br \/>\n[\/js]<\/p>\n<p><strong>2. HA PROXY Configuration.:-<\/strong><br \/>\nInstall haproxy package using apt-get.<\/p>\n<p>[js]<br \/>\napt-get install haproxy<br \/>\n[\/js]<\/p>\n<p>By Default the proxy mode is disabled and to enable it change \/etc\/default\/haproxy file.<\/p>\n<p>[js]<br \/>\nENABLED=1<br \/>\n[\/js]<\/p>\n<p>The main config file is \/etc\/haproxy\/haproxy.cfg and after editing the final file looks like:-<\/p>\n<p>[js]<br \/>\nglobal<br \/>\n    log 127.0.0.1 local0 notice<br \/>\n    user haproxy<br \/>\n    group haproxy<br \/>\n        daemon   #Makes the process fork into background.<\/p>\n<p>defaults<br \/>\n    log global<br \/>\n    retries 2<br \/>\n    timeout connect 3000<br \/>\n    timeout server 5000<br \/>\n    timeout client 5000<br \/>\n# Both salve server defined under this section<br \/>\n#listen &lt;ANY NAME&gt;<br \/>\n#   bind &lt;Load balancer Ip Address&gt;:3306<br \/>\n#   mode tcp<br \/>\n#   option mysql-check user &lt;haproxy check user name&gt;   # ha proxiy active check user<br \/>\n#   balance &lt;load distribution algoritham&gt;<br \/>\n#   server &lt;any_name&gt; &lt;Read replica end point&gt;:3306 check weight &lt;no of request to be sent&gt;  fall &lt;connect retry to declare dead&gt; fastinter &lt;interval between checks in ms&gt;<\/p>\n<p>listen rds-cluster<br \/>\n    bind 10.0.0.204:3306<br \/>\n    mode tcp<br \/>\n    option mysql-check user ha_check<br \/>\n    balance roundrobin<br \/>\n    server mysql-1  ha1.xxxxxxx.ap-southeast-1.rds.amazonaws.com:3306 check weight 1 fall 2 fastinter 1000<br \/>\n    server mysql-2 ha2.xxxxxxx.ap-southeast-1.rds.amazonaws.com:3306 check weight 1 fall 2 fastinter 1000<\/p>\n<p># To chck load balancer status<br \/>\n#listen &lt;ANY NAME&gt;<br \/>\n#    bind &lt;Server IP&gt;:8080<br \/>\n#    mode http<br \/>\n#    stats enable<br \/>\n#    stats uri \/<br \/>\n#    stats realm Strictly\\ Private<br \/>\n#    stats &lt;USERNAME&gt;:&lt;PASSWORD&gt; <\/p>\n<p>listen cluster-check<br \/>\n    bind 10.0.0.204:8080<br \/>\n    mode http<br \/>\n    stats enable<br \/>\n    stats uri \/<br \/>\n    stats realm Strictly\\ Private<br \/>\n    stats auth admin:password<br \/>\n[\/js]<\/p>\n<p>Restart haproxy service to apply changes.<\/p>\n<p>[js]<br \/>\nservice haproxy restart<br \/>\n[\/js]<\/p>\n<p><strong>3. Load balance testing:-<\/strong><\/p>\n<p>To check haproxy setup, login into your web server and run this command.<\/p>\n<p>[js]<br \/>\nmysql -h10.0.0.204 -uha_read -ppassword -e &quot;show variables like &#8216;server_id&#8217;&quot;<br \/>\n&#8216;<br \/>\n[\/js]<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/Screenshot-from-2015-05-05-143052.png\" alt=\"\" \/><\/p>\n<p>Each time we are requesting server_id the response value is different. It stats haproxy equally distributing traffic between two nodes. You can test the results while changing weight parameter in \/etc\/haproxy\/haproxy.cfg.<\/p>\n<p>It is also important to keep your eyes on load balancer health. Login into your browser to see real time load balancer status.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/05\/Screenshot-from-2015-05-05-143400.png\" alt=\"\" \/><\/p>\n<p>Here we tested the scenario with two slave servers. You can add as much server as you want, but creating more read replica bring more work head on master server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AWS provide some intensive services and these are easily manageable and as being admin there is less overhead to scale up your application infrastructure. In automated deployments if we are planing to horizontally scale your database, with help of a single click we can populate read replica of our running database. But to handle large [&hellip;]<\/p>\n","protected":false},"author":165,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":13},"categories":[1174,2348],"tags":[1257,2366,1749],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19423"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/165"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=19423"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19423\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=19423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=19423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=19423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}