PMP
PMP

Reputation: 231

Heroku Slow Loading Throughout

My Heroku Rails is always loading extremely slowly and not only at first start.

It doesnt load this slowly in production mode.

I have New-Relic Installed, I have two Dynos running and I have PostgreSQL Crane Plan. But when I start my app it will normally take about 30 seconds to load and 60% of the time it'll go straight to Application Error, if I check my logs I get Memory Quota Exceeded. Even if I go to a static page with no Ruby in its source, it'll still load slowly. Sometimes I manage to connect to my app and it will normally take around 10 seconds to load when navigating from page to page. I've been looking online for ages and all the most common answer I find is to just add an extra dyno, but that doesnt help.

Im also running on Unicorn

These are the logs from a movies#show page which loaded at first and then crashed

Started GET "/movies/61708" for 81.34.154.155 at 2013-08-29 23:08:23 +0000
2013-08-29T23:08:30.093034+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/?page=7 host=www.websitename.com fwd="81.34.154.155" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0
2013-08-29T23:08:31Z app[postgres.8916]: [BLUE] duration: 6945.946 ms  statement: SELECT "movies".* FROM "movies" 
2013-08-29T23:08:41+00:00 app[heroku-postgres]: source=HEROKU_POSTGRESQL_BLUE measure.current_transaction=2844 measure.db_size=60732536bytes measure.tables=23 measure.active-connections=10 measure.waiting-connections=0 measure.index-cache-hit-rate=1 measure.table-cache-hit-rate=1
2013-08-29T23:08:45.401673+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/movies/63724 host=www.websitename.com fwd="81.34.154.155" dyno=web.1 connect=2ms service=30001ms status=503 bytes=0
2013-08-29T23:08:45.445116+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/movies/63704 host=www.websitename.com fwd="81.34.154.155" dyno=web.2 connect=3ms service=30000ms status=503 bytes=0
2013-08-29T23:08:52.684401+00:00 heroku[web.2]: Process running mem=512M(100.0%)
2013-08-29T23:08:52.684613+00:00 heroku[web.2]: Error R14 (Memory quota exceeded)
2013-08-29T23:08:53.647059+00:00 app[web.1]: E, [2013-08-29T23:08:53.384868 #2] ERROR -- : worker=0 PID:5 timeout (61s > 60s), killing
2013-08-29T23:08:54.375154+00:00 app[web.1]: E, [2013-08-29T23:08:54.374971 #2] ERROR -- : reaped #<Process::Status: pid 5 SIGKILL (signal 9)> worker=0

Does anyone know a solution for this?

Upvotes: 1

Views: 918

Answers (1)

deefour
deefour

Reputation: 35350

You mentioned in the comments you have 60,000 rows in your movies table. From your logs it appears you're doing something like the following:

Movie.all

This will generate an SQL query like what we see in your logs.

SELECT "movies".* FROM "movies"

The memory/storage capacity of Postgres doesn't matter with respect to your issue. Your database could be on a dedicated server with 96G RAM. You'll still end up with the same problem on Heroku's end.

When you do something like Movie.all, you're creating in-memory ruby class instances of Movie for all 60,000 records. This takes time and space.

You're probably also rendering too many (dare I say all?!) of these records to a single webpage at once. This is simply way too much data to render during a single request, whether you're on Heroku or not.

Heroku has a hard 30s time limit for it's request's running time. You're hitting this limit. Try a much smaller subset of data, even just as a test, and see if it solves your issues. I'm betting it will.

Movie.limit(10)

Upvotes: 2

Related Questions