118

I created two very simple Heroku apps to test out the service, but it's often taking several seconds to load the page when I first visit them:

All I did was create a simple Sinatra app and deploy it. I haven't done anything to mess with or test the Heroku servers. What can I do to improve response time? It's very slow right now and I'm not sure where to start. The code for the projects are on github if that helps.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Lance
  • 75,200
  • 93
  • 289
  • 503

5 Answers5

203
  • If your application is unused for a while it gets unloaded (from the server memory).
  • On the first hit it gets loaded and stays loaded until some time passes without anyone accessing it.

This is done to save server resources. If no one uses your app why keep resources busy and not let someone who really needs use them ?
If your app has a lot of continous traffic it will never be unloaded.

There is an official note about this.

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
clyfe
  • 23,695
  • 8
  • 85
  • 109
  • 2
    It's done so that the server can have some free memory if other applications require it. Secondly, it's quite easy to overload a server with applications if they're always running. There's numerous reasons. Clyfe nails the simple version of it. – Ryan Bigg Apr 09 '10 at 09:24
  • is that in the docs somewhere? how long after no use does that happen? thanks for the answer, that makes things a lot clearer :). – Lance Apr 09 '10 at 09:25
  • I think it's a passenger behaviour – shingara Apr 09 '10 at 09:38
  • 55
    The easiest way to fix this is to increase your dynos to 2. – Chap Apr 09 '10 at 12:37
  • @clyfe could you please give an example of how to make a send request to a blank page using for example whenever gem ? (or any other gem would be fine).: every 30.minutes do what???? Thanks in advance – diegopau Jul 23 '11 at 08:04
  • @diegopau this must be done from an external application, not from your heroku app, acting essentially like a user hitting your app – clyfe Nov 22 '11 at 19:02
  • @SteenhouwerD + Leventix is this allowed by heroku? – Stephan Schielke Dec 07 '12 at 10:38
  • 1
    @chap True, and a good idea. Should mention that each additional dyno runs about $35/month. – TKH Dec 28 '12 at 16:37
  • 79
    Pinging your server to keep it from idling is crapping in your own nest. These services you are talking about are _free_. They need to conserve resources. If everyone pings their server, none get swapped out, and the provider has to scale up. That costs money.... goodbye free service. I think the poster of this answer should delete the suggestion to ping the server. – GreenAsJade Dec 30 '12 at 03:59
  • 43
    I use uptimerobot.com to ping my Heroku app every 5 minutes for free - it tells me I have a 200OK (and more importantly when I don't) and it keeps the app responsive. I make no apologies for this; I have 10 heroku apps, most of which are dev or demo apps, but this one in particular is live/production and despite low traffic it needs to respond quickly when requested. If this was a threat to Heroku's business model then they'd stop us doing it. When I've got thousands of global users I'll spin up another dyno and start paying for what is a great service. So quit with the guilt-trip thing! :) – ED-209 May 20 '13 at 13:34
  • 5
    I'm with Tokn. How are new apps supposed to get users if the site loads so slow they leave before it's up? – Deborah Jan 31 '14 at 07:32
  • 4
    Looking back on this now, the nest crapping was indeed achieved. Luckily Heroku handled it nicely. You can no longer have a free dyno perpetually up as the free tier is limited by uptime now. – Jenny Shoars Feb 14 '16 at 17:29
  • 1
    Adding the second dyno costs $7/month. I think it's low enough to guilt-trip yourself over spoiling the free service for someone from a first-world country. – Evgenia Karunus Apr 17 '17 at 11:39
16

You might also want to investigate the caching options you have on Heroku w/ Varnish and Memcached. These are persisted independent of the dynos.

For example, if you have an unchanging homepage, you can cache that for extended periods in Varnish by adding Cache-Control headers to the response. Then your users won't experience the load hit until they want to "do something" rather than when they arrive.

sevennineteen
  • 1,182
  • 8
  • 14
6

You should check out Tom Robinson's answer to "Scalability: How Does Heroku Work?" on Quora: http://www.quora.com/Scalability/How-does-Heroku-work

Heroku divides up server resources among many different customers/applications. Your app is allotted blocks of computing power. Heroku partitions based on resource demand. When you have a popular application that demands more power, you can pay for more 'dynos' (application containers) and then get a larger chunk of the pie in return.

In your case though, you are running a free app that few people--if any outside of you--are visiting/using. Therefore, Heroku cuts down on the resources you're getting by unloading your app--putting it in hibernation essentially--until there is a request made to your address. When that happens, and your app has been idling for a long time, it takes time to reload.

Add 1 extra dyno to keep your app from falling asleep, if that reload time is important.

RKelley
  • 576
  • 6
  • 16
3

I am having the same problem. I deployed a Rails 3 (1.9.2) app last night and it's slow. I know that 1.9.2/Rails 3 is in BETA on Heroku but the support ticket said it should be fine using some instructions they sent me.

I understand that the first request after a long time takes the longest. Makes sense. But simply loading pages that don't even connect to a DB taking 10 seconds sometimes is pretty bad.

Anyway, you might want to try what I'm going to do. That is profile my app and see how long it takes locally. If it's taking 400ms then something is wrong. But if I get 50ms locally and it still takes 10 seconds on Heroku then something is definitely wrong.

Obviously, caching helps but you only get 5MB for free and once again, with ONE person using the site, it shouldn't be that slow.

cbmeeks
  • 11,248
  • 22
  • 85
  • 136
0

I had the same problem with every app I have put on via heroku's free account. Now there are options of adding dynos so that your app does not get offloaded while it is not being used for a while, you can also try using redis or memcached for caching. But I used a hacky solution for my small scale project, I basically built web scraper put it inside an infinite loop with sleep and tada the website actually had much better response times(I guess it was not getting offloaded because of the script).

adit negi
  • 39
  • 2