0

I have a Rails app on a free tier on Heroku and it recently started getting some users. One of the events in my app involves querying another API and can take up to 10 seconds to finish. How do I make sure other users who visit a simple page at the same time (as another user's API event) don't need to wait 10 seconds for their page to load?

Do I need to pay for more Dynos? Is this something that can be solved with the delayed_job gem? Would another host (like AppFog or OpenShift) be able to handle simultaneous requests faster?

Update: This question suggest manually handling threads instead of using delayed_job.

Community
  • 1
  • 1
Ari
  • 1,974
  • 19
  • 30

3 Answers3

1

That sounds like a Delayed Job situation. If the first request is just waiting, the most efficient thing to do is assign a process to wait for it to complete and cut the Rails process loose to handle another request.

bgates
  • 2,333
  • 1
  • 18
  • 12
  • Should I should use a Worker to run delayed jobs? – Ari Aug 06 '13 at 18:19
  • That looks like [what they recommend](https://devcenter.heroku.com/articles/delayed-job). – bgates Aug 06 '13 at 18:24
  • thanks. tho i ran into some issues setting it up: http://stackoverflow.com/questions/18090760/how-to-make-the-controller-wait-for-a-delayed-job-while-the-rest-of-the-app-cont – Ari Aug 06 '13 at 21:24
1

Yes you need more dynos, speccialy worker dynos those are the ones that work on the background you can check this railscast on delayed jobs that can help also:

http://railscasts.com/episodes/366-sidekiq

also here is a quick tutorial on adding unicorn and multiple threads to your free heroku instance:

https://devcenter.heroku.com/articles/rails-unicorn

you divide your dyno into two or more instances then each one can handle a different request

Rodrigo Zurek
  • 4,555
  • 7
  • 33
  • 45
  • no its not complicated at all, check this app i did, im using the same technique: https://github.com/kazpsp/amelia, check the Procfile at the root of the app, and also the config>unicorn.rb its as simple as that – Rodrigo Zurek Aug 06 '13 at 16:08
  • also dont forget to add unicorn to your gemfile like this: gem 'unicorn' – Rodrigo Zurek Aug 06 '13 at 16:08
  • thanks. when i tried it with basic setup, it didn't immediately prevent delays. how does it decide when to put processes into separate threads? – Ari Aug 06 '13 at 16:15
  • well the way that unicorn works in that scenario is that is has three threads, so first come first served, if one thread is busy it will use one thats not busy and so on. – Rodrigo Zurek Aug 06 '13 at 16:38
0

What kind of app server are you using? If you are using passenger or unicorn, you can have multiple worker processes that can handle simultaneous requests

http://www.modrails.com/documentation/Users%20guide%20Apache.html#_passengermaxinstancesperapp_lt_integer_gt

usha
  • 28,973
  • 5
  • 72
  • 93