Simple and easy to install HAProxy. Just the following commands:
brew update brew install haproxy
Configuring haproxy config file inside the rails app
Add the following file inside
global maxconn 4096 pidfile ~/tmp/haproxy-queue.pid defaults log global log 127.0.0.1 local0 log 127.0.0.1 local1 notice mode http timeout connect 300000 timeout client 300000 timeout server 300000 maxconn 2000 option redispatch retries 3 option httpclose option httplog option forwardfor option httpchk HEAD / HTTP/1.0 frontend http-farm bind *:9000 default_backend app1latest acl url_tag02 path_beg /tag02/ use_backend tagged-02 if url_tag02 backend app1latest balance roundrobin server localhost_9001 localhost:9001 backend tagged-02 balance roundrobin server localhost_9002 localhost:9002 listen haproxyapp_admin:9100 127.0.0.1:9100 mode http stats uri /
Now to test this architecture, fire-up 4 terminals. Run the following commands within the rails app directory
haproxy -f config/haproxy.cfg
This will run the haproxy and you can view its metrics at
bundle exec rails s -p 9001
bundle exec rails s -p 9002
The above 2 commands fire up two separate backend servers which will get the request from haproxy
And lastly, the following command with send the request at
[http://127.0.0.1:9000](http://127.0.0.1:9000). Do hit couple of times.
You’ll see how haproxy balance the requests and send it over the 2 backend
Now you can play around with haproxy and your rails app with different configurations. The configurations are endless as per your needs.
Note: The application I’m working on has a different requirement to run the application separately on different servers once the new code changes is pushed to the main application for certain time. Its like running the different versions of the same application until the pending jobs are done. So, the strategy I took is to use load-balancer (haproxy) to determine to which server farm to send based off of the url substring. I will post the detailed explanation of the architecture in future. So, if you want to stay tuned, you can follow @nepalonrails