# Delay Loading Rails Views

**URL:** <https://www.ruby-forum.com/t/delay-loading-rails-views/202603>\
**Category:** Rails\
**Created:** [February 2, 2011, 10:08am UTC](https://www.ruby-forum.com/t/delay-loading-rails-views/202603 "2011-02-02T10:08:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 2, 2011, 10:08am UTC](https://www.ruby-forum.com/t/delay-loading-rails-views/202603/1 "2011-02-02T10:08:30Z")

</div>

Hi All,

I’m having trouble with a Rails app and I can’t figure out what /  
where the problem is.

From the logging and testing I’ve done, it looks like there is a  
consistent 3 second delay between the time that Rails finishes  
rendering the view and the time that the browser actually receives the  
data. That delay doesn’t show up on my laptop, running in Production  
mode, but it does show up on my server, running in Production mode.  
Both machines are running Ruby 1.8.7 and both are running Rails 3.0.3.

I ran the test as pure as I could. I SSHed into the server and loaded  
the page through curl and timed the load. I also loaded directly from  
Rails itself, not going through the Apache proxy. I think that  
eliminates both the network and Apache as sources of the delay. I  
think the delay is somewhere in Rails itself.

Here’s my logs. First, the commands and date/time stamps from the  
prompt.

[litho:~/apps/timetrack/current/log] jmartin$ date ; curl -b  
“cookie:val” [http://127.0.0.1:10100/activity/list](http://127.0.0.1:10100/activity/list) \>/dev/null ;  
date ;curl -b “cookie:val” [http://127.0.0.1:10100/activity/list](http://127.0.0.1:10100/activity/list) \>/dev/  
null ; date

Tuesday, February 1, 2011 2:28:38 PM GMT  
% Total % Received % Xferd Average Speed Time Time  
Time Current  
Dload Upload Total Spent  
Left Speed  
100 4709 0 4709 0 0 1328 0 --:–:-- 0:00:03  
–:–:-- 1328  
Tuesday, February 1, 2011 2:28:42 PM GMT

Tuesday, February 1, 2011 2:28:42 PM GMT  
% Total % Received % Xferd Average Speed Time Time  
Time Current  
Dload Upload Total Spent  
Left Speed  
100 4709 0 4709 0 0 1352 0 --:–:-- 0:00:03  
–:–:-- 1352  
Tuesday, February 1, 2011 2:28:45 PM GMT

Next, here are the entries from the Rails production.log file.

Started GET “/activity/list” for 127.0.0.1 at Tue Feb 01 14:28:38  
+0000 2011  
Processing by ActivityController#list as _/_  
before filter  
Exist fragment? views/127.0.0.1:10100/activity/list (0.1ms)  
Write fragment views/127.0.0.1:10100/activity/list (0.1ms)  
Rendered activity/\_recent.html.erb (7.4ms)  
Rendered activity/\_switch.html.erb (2.4ms)  
Rendered activity/list.html.erb within layouts/application (14.1ms)  
Completed 200 OK in 19ms (Views: 14.5ms | ActiveRecord: 108.9ms)

Started GET “/activity/list” for 127.0.0.1 at Tue Feb 01 14:28:42  
+0000 2011  
Processing by ActivityController#list as _/_  
before filter  
Exist fragment? views/127.0.0.1:10100/activity/list (0.1ms)  
Read fragment views/127.0.0.1:10100/activity/list (0.1ms)  
Rendered activity/\_recent.html.erb (1.3ms)  
Rendered activity/\_switch.html.erb (2.4ms)  
Rendered activity/list.html.erb within layouts/application (7.7ms)  
Completed 200 OK in 12ms (Views: 8.3ms | ActiveRecord: 106.9ms)

It looks like the Rails app receives the request as soon as curl sends  
it. It process it in 12 or 19 milliseconds and then curl doesn’t get  
the response for another 3 full seconds.

What could possibly be causing the delay between the time that the  
view is Completed and logged and the time that curl receives the HTML  
data?

---

<div class="post-metadata">

**Author:** ![joemanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@joemanfoo](https://www.ruby-forum.com/u/joemanfoo)\
**Post date:** [February 2, 2011, 3:32pm UTC](https://www.ruby-forum.com/t/delay-loading-rails-views/202603/2 "2011-02-02T15:32:18Z")

</div>

On Feb 1, 2011, at 5:57 PM, Joe M. wrote:

> It looks like the Rails app receives the request as soon as curl sends  
> it. It process it in 12 or 19 milliseconds and then curl doesn’t get  
> the response for another 3 full seconds.
> 
> What could possibly be causing the delay between the time that the  
> view is Completed and logged and the time that curl receives the HTML  
> data?

This kind of consistent delay sounds a lot like a DNS issue to me.  
Some machine in the transaction (maybe the server, maybe the machine  
that cURL is running on) is trying to look up its own name and  
failing, and having to wait through the DNS timeout. That’s the  
direction I would start looking personally.

Walter

---

<div class="post-metadata">

**Author:** ![joemanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@joemanfoo](https://www.ruby-forum.com/u/joemanfoo)\
**Post date:** [February 2, 2011, 9:22pm UTC](https://www.ruby-forum.com/t/delay-loading-rails-views/202603/3 "2011-02-02T21:22:11Z")

</div>

3 seconds is also very close to the time a server will wait for a  
second request, when keep-alive is on. I don’t know why it would be  
triggered here as surely curl did not set that option on the  
connection.

Firewall config could also affect this.

–Michael

---

<div class="post-metadata">

**Author:** ![joemanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@joemanfoo](https://www.ruby-forum.com/u/joemanfoo)\
**Post date:** [February 2, 2011, 5:32pm UTC](https://www.ruby-forum.com/t/delay-loading-rails-views/202603/4 "2011-02-02T17:32:07Z")

</div>

I thought of that, but I don’t see where it could be getting into the  
mix. I’m running cURL directly on the application server and I’m  
connecting to the application via 127.0.0.1 no DNS there. The  
database.yml is pointing to the database on 127.0.0.1 no DNS there  
either. I’m not using memcached or anything else fancy that might need  
a DNS lookup.

I’ve checked all of the files in config/ and I don’t see any host name  
entries. Is there anywhere else I should look for host name  
configuration?

Is there any way I can trace exactly what’s happening after the  
“Completed 200 OK in 12ms” log entry, to see what’s hanging?

~Joe

---

<div class="post-metadata">

**Author:** ![joemanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@joemanfoo](https://www.ruby-forum.com/u/joemanfoo)\
**Post date:** [February 3, 2011, 4:40am UTC](https://www.ruby-forum.com/t/delay-loading-rails-views/202603/5 "2011-02-03T04:40:05Z")

</div>

I found the problem, although I’m still baffled as to why it was  
occurring at all.

My session (using active\_record\_store) was storing the User object of  
the logged in user. Any page that access that object experienced the 3  
second delay. Any page that didn’t access that object loaded almost  
instantly. I re-jiggered the application to just store the user id and  
User.find the object whenever it was needed. Voila no more delays.

Problem solved but I don’t know why it was a problem.
