Heroku Queuing Time, Part2: Solution

Preface

In previous articles we discussed application with slow resources and heroku queuing time problems.Today we find out how to setup HAProxy as “slow-fast” request balancer for heroku application using Amazon Elastic Compute Cloud.You need:
  • non optimized application
  • Heroku account
  • Amazon EC2 account

Prepare AWS tools

Although you can manage AWS via WEB interface we prefer command line tools. Now switch to console and setup your credentials for tools.Check access by invoking (for example):Tools are ready.

Create balancer ssh access key

In order to access balancer we need ssh private key.Choose name for your key (e.g. haproxy-balancer) and create it by:Save output to some file e.g haproxy-balancer.pem. Don’t forget to set right permissions:

Configure Firewall

Open to ports in order to access our instances via SSH and HTTP:Note. From security point of view we recommend to create special access group for balancer instead of using default group.

Setup balancer on AWS

AMI

We are going to use standard latest Ubuntu AMI. On alestic page choose desired ubuntu version.Be sure to choose AMI from us-east-1 region because heroku instances are located exactly in this region, otherwise you will have network latency.On moment of writing this article latest AMI is:Run instance using haproxy-balancer credentials into us-east-1 region on t1.micro instance:In order to find out status and IP of instance wait a while and run:

HAProxy

Connect to balancer instance:Install HAProxy package:(Optionally) Install git package:Clone our HAProxy request balancer configuration. You are free to use own HAProxy configuration just follow ideas in our configuration. Anyway we strongly recommend you to keep your configuration under control version system (git. subversion, etc). And then deliver changes to server from SCM repository instead of editing it directly on the server.For simplity we clone sample configuration and change it on the server:Tune slow urls in:Enable HAProxy by setting ENABLED=1 into /etc/default/haproxy:Start HAProxy:Check HAProxy:

Switch to HAProxy balancer

Now we can try visit slow/fast urls of our application using balancer IP address or perform some load/stress testing. When everything is ok we are ready to change application DNS host settings to balancer IP.We recommend to allocate and assign elastic IP for this purporse. It allows you easy switch to another balancer if something goes wrong.Allocate new IP addresss:Assign address to your Balancer IP address:Check again by visiting your application via assigned elastic IP.Finally change your DNS “A” record of to balancer IP according your DNS provider manual.Also Heroku Custom Domains article can be useful.

Rollback

If something went wrong switch back DNS settings to use heroku application directly.

Conclusion

As we already mentioned this solution is “temporary”. It gives your application opportunity to survive under high traffic. You may use “slow-fast” balancer until you fix your slow resources by moving them to background or changing your architecture. Then you may switch back to direct application usage.We are hope you find this article helpful.

References