3

A Rails application has the following feature:

Some of its models access data from an external MySQL database. The following code is being used to set the external database to the model:

class Foo < ActiveRecord::Base

  establish_connection ENV['EXTERNAL_DATABASE_URL']
  self.table_name = 'foos'

  ...

end

Running the app locally on my notebook, I have the following request execution times:

Completed 200 OK in 374ms (Views: 118.1ms | ActiveRecord: 198.6ms)

When I upload it to Heroku and use the app there, the same request has the following execution times

Completed 200 OK in 8000ms (Views: 901.6ms | ActiveRecord: 6609.2ms)

Both ActiveRecord and Views times are insanelly large when I run it on Heroku.

  • The external db may be the problem?
  • How can I check this?
  • If it's not, what could be the problem?

EDIT

NewRelic report

NewRelic shows that the bottleneck is database. However, it's clearly not the time DB spends on the queries, because when I run locally, it doesn't take that much. So I must assume that it's the latency that's causing it?

9
  • The external db might be a problem. Where is the database hosted? How big is the latency between heroku in the US and the db? How many request to you do to your db in each request? Commented May 23, 2014 at 13:52
  • The database is hosted by Amazon RDS, but in São Paulo region, not in US. In this example there are 18 requests. I'm not sure how to measure the latency between Heroku and db. How can I do that? Thanks! Commented May 23, 2014 at 13:58
  • "Running the app locally on my notebook" please never compare cloud platform performance to your local laptop. It doesn't make any sense at all. For profiling, checkout something like New Relic or skylight.io Commented May 23, 2014 at 14:02
  • A local db query might take 3-5ms. I bet the latency between US and San Paulo is at least 100ms. That means at each request takes least 200ms longer. I am not surprised. Move the db into Amazons Datacenter amazon-web-services::us-east-1 that is where heroku is hosted. Commented May 23, 2014 at 14:19
  • @spickermann wow! I had no idea that db latency could be that large. I believe the NewRelic chart I added just reinforce this believe, right? Commented May 23, 2014 at 14:21

1 Answer 1

2

How long a database request takes depends on several factors:

  1. How long does it take the database management system to find the data? This depends on the complexity of the query, existence of indexes and table size.
  2. How long does it Rails take to build ActiveRecord object from the result? This depends on the number of results on each page.
  3. And how long does it take to send the request from the web server to the database server plus how long does it take to send the result from the database server back to the web server? This latency depends on the distance between the servers.

When your web server is hosted by Heroku in the US (us-east-1), but your database is on a Amazon RDS in São Paulo, then each request to the database adds an other 150ms latency to the overall response time of the webpage. This problems gets bigger the more requests to the database are done within one web request.

I suggest to move the database as close as possible to the web server. If possible within the same datacenter in the US (on the same server would be the fastest). Or consider hosting the webserver yourself in São Paulo (perhaps with Amazon OpsWorks).

Sign up to request clarification or add additional context in comments.

1 Comment

I migrated the db to the same region of Heroku and the request times are now actually smaller than running locally. Many thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.