Your MVC may be Slowing Down Your Site

by Subbu Allamaraju on July 2, 2012 · 39 comments

One of most common design patterns used on the Web is the model-view-controller. Though some variations of this pattern exist, the basic idea is simple – a controller parses and processes the incoming HTTP request, thinks for a while, and then writes a view as HTTP response.

The point of this post is not to blame MVC, but to point out that implementations in web frameworks may be leading to higher time to first byte times.

But did you know that, this style, as commonly implemented, may be slowing down your site? See the waterfall charts below for some of the Alexa's top 500 sites in US, captured using webpagetest over the DSL network profile with Firefox.

When you look from the network perspective, all these charts share a common pattern – deafening silence, followed by a log jam.

The network is silent when the server is thinking (the green area). As soon as response starts to trickle down (the blue area), the browser starts new network activity and immediately gets into a log jam mode. In this mode, the browser is busy making DNS look ups, TCP handshakes, followed by several HTTP requests over a limited number of connections.

From a user experience point of view, this is bad as it leads to a rapid backlog of HTTP requests. As the number of connections is limited, it takes a while for this log jam to clear up which increases the overall page latency. A protocol like SPDY can deal with the log jam better, but reducing the jam is better for the user.

The problem here is not with MVC per se, but how it is commonly implemented. Most often, the thinking part consists of talking to back-end systems and/or some computation completely blocking response generation. Even in implementations that do some of this thinking asynchronously, view rendering ofter starts after the thinking part is done. This contributes to higher "time to first byte" times and the subsequent log jam.

Below is a waterfall chart of a site that minimizes this log jam.

In this case, the server is starting HTML delivery as soon possible so that the browser can start to download some of those resources sooner. The HTML delivery continues even after some of the subsequent resources complete. This is possible by interleaving HTML delivery with backend work and stream chunks of response as they become ready.

Though such sooner delivery does not completely avoid the log jam, it does help ease it.

If you do MVC, don’t forget to think from the network point of view.

Update: See the Hacker News thread for more comments: http://news.ycombinator.com/item?id=4194500

{ 39 comments… read them below or add one }

Fábio A Falavinha (@fabiofalavinha) July 2, 2012 at 1:23 pm

Hello! We have a bug…
http://t.co/jVm74s1N

Reply

pblakez Ξ bokeh (@pblakez) July 2, 2012 at 2:45 pm

MVC ( model-view-controller) may be Slowing Down Your Site http://t.co/l1mjd0Bg by @fabiofalavinha

Reply

Ricardo Cosme (@ricjcosme) July 2, 2012 at 6:48 pm

MVC may be Slowing Down Your Site http://t.co/P3tzB8wo

Reply

saturngod (@saturngod) July 2, 2012 at 8:03 pm

MVC may be Slowing Down Your Site http://t.co/xs3xkm43 via @zite #fb #in

Reply

giovanni allasia (@giovanniallasia) July 2, 2012 at 9:43 pm

MVC may be Slowing Down Your Site http://t.co/MVgveST5 via @zite

Reply

Phil_Factor (@Phil_Factor) July 2, 2012 at 11:21 pm

MVC may be Slowing Down Your Site http://t.co/J0bInhmn

Reply

Alex July 3, 2012 at 1:59 am

It is not MVC which slows down your site. It is a large number of resources you may have in your page, like js, css or images. The solution for this problem is to minimize number of requests using various techniques like: merge js, merge css, use sprites or cssDataUri for encoding images into css files.

Reply

Subbu Allamaraju July 3, 2012 at 5:26 am

These are orthogonal issues. Longer TTFB is a consequence of how the server is implemented. Most sites already do the things you mention but still suffer from longer TTFB and the consequential log jam.

Reply

Alex July 3, 2012 at 9:54 am

The same here, the TTFB is not related to MVC. If your problem is on the server-side, you can split the business logic in smaller chunks and run it in parallel. The title is at least misleading ….

Reply

Talker (@q8talker) July 3, 2012 at 9:01 am

MVC may be Slowing Down Your Site http://t.co/xBPGakyK #news

Reply

James July 3, 2012 at 9:26 am

This is because most web applications require heavy I/O processing. Most websites TTFB time is proportional to how fast your disk is (not your CPU/RAM). Faster I/O (faster hard-drive such as RAID-10 on SSD) will reduce your TTFB dramatically.

Reply

Subbu Allamaraju July 3, 2012 at 9:28 am

Yes. The other thing to check is how network IO is done from front-end to back-end.

Reply

Florian Petri (@florianpetri) July 3, 2012 at 9:36 am

MVC ist auch nicht pauschal immer gut: “MVC may be Slowing Down Your Site http://t.co/tt65q3S2

Reply

TH July 3, 2012 at 9:42 am

This is simply a criticism of how server-side applications operate, and it has nothing whatsoever to do with MVC.

What you’re suggesting is that people stream more content in a staggered fashion so it reaches the client faster and not all at once. Interesting idea, but say what you mean, not some drivel about a concept as generic as MVC being at fault. Nonsense.

Reply

Subbu Allamaraju July 3, 2012 at 9:47 am

Right, to quote from the post above

> The problem here is not with MVC per se, but how it is commonly implemented.

Reply

Paolo July 3, 2012 at 10:11 am

Then why the title? FUD?

Reply

TH July 3, 2012 at 10:12 am

Right, to quote from the title, opening sentence, and various other paragraphs:

> MVC may be Slowing Down Your Site

> One of most common design patterns used on the Web is the model-view-controller. … But did you know that, this style, as commonly implemented, may be slowing down your site?

> If you do MVC, don’t forget to think from the network point of view.

Overwhelmingly misleading, in a similar fashion to news stories on scientific articles. Eg: “Eating chicken may be killing you,” followed by an article that talks about eating *fried* chicken in mass quantities. Not the same issue.

Reply

Subbu Allamaraju July 3, 2012 at 10:17 am

Changed the title to start with “Your …”. Disagree with other your comments. I think you’re missing “as commonly implemented” and the “network” part.

Reply

Stephan July 3, 2012 at 9:49 am

I feel that the argument that MVC is slowing your website isn’t accurate. It’s more a problem of a controller or any code that has data it needs to fetch before it can render output to the client. While your proclaiming that MVC may be slowing down your site your negating this argument in the latter part of your post, which makes me a bit confused.
The log jam issue you mention can be alleviated by minimizing the amount of request for external resources, use of CDN’s and so forth. Also this is not a MVC related issue either.

Reply

Subbu Allamaraju July 3, 2012 at 9:52 am

> “The log jam issue you mention can be alleviated by minimizing the amount of request for external resources, use of CDN’s and so forth. Also this is not a MVC related issue either.”

Interesting point, but in reality, due to limited no connections, requests do get backed up in the browser itself. In the charts above (you can try more to confirm) most of the subsequent resources are coming from the CDN.

Reply

(@tek_news) (@tek_news) July 3, 2012 at 9:55 am

HNews: MVC may be Slowing Down Your Site http://t.co/Pw5hcJ5o

Reply

Kevin July 3, 2012 at 10:26 am

Awesome. I have not tested, but I kind of expected this. I think MVC was originally invented for good maintenance, but not for good performance. :)

Reply

Subbu Allamaraju July 3, 2012 at 10:29 am

+1. It was proposed by GoF as a means of separation of concerns.

Reply

Rodrigo Delduca (@skhaz) July 3, 2012 at 11:36 am

Your MVC may be slowing down your site http://t.co/yR5aMqzd

Reply

Luca Molteni (@volothamp) July 3, 2012 at 12:09 pm

MVC may be Slowing Down Your Site http://t.co/CvTIYKW7

Reply

R Sriram July 3, 2012 at 12:16 pm

IMHO from a user experience point of view what matters is when can a user start interacting with the UI. So won’t the final loading time be the same

Reply

Subbu Allamaraju July 3, 2012 at 12:20 pm

Good point, but TTFB is only one of the metrics to look at for any given page. Others include above-the-fold, time to action etc.

Reply

Stackful.io VPS (@stackful_io) July 3, 2012 at 3:00 pm

MVC may be Slowing Down Your Site http://t.co/DpM9iLrb #webdev #devops #webapp ** VPS -> http://t.co/96LQHaQz

Reply

Remus Rusanu July 4, 2012 at 3:37 am

The main issue I see is that you cannot start rendering back a response until you know the HTTP status (200, 403, 404) and in order to figure that one out you need to burn pretty much the most of your controller ‘thinking’ time.

Reply

Marcus Sá (@sa_vini) July 4, 2012 at 5:03 am

MVC vem sendo surrado últimamente. Your MVC may be Slowing Down Your Site http://t.co/mFEnUohF

Reply

Hasan Tayyar BEŞİK (@htayyar) July 4, 2012 at 6:28 am

Your MVC may be Slowing Down Your Site http://t.co/2DDkC6DC

Reply

Randy Burgess (@wrburgess) July 4, 2012 at 8:10 am

MVC may be Slowing Down Your Site http://t.co/JpoDqT2a

Reply

(@lsdr) (@lsdr) July 4, 2012 at 9:42 am

http://t.co/P2NEDl1N – Your MVC may be Slowing Down Your Site

Reply

Sung-Taek, Kim (@stkim1) July 4, 2012 at 9:41 pm

MVC may be Slowing Down Your Site http://t.co/ZJ6alXsw

Reply

falutx (@falutx) July 5, 2012 at 2:30 pm

Así es como #MVC ralentiza la web http://t.co/UqVqdCCH

Reply

Héctor Menéndez (@et0r) July 18, 2012 at 11:39 pm

Your MVC may be Slowing Down Your Site http://t.co/4uideJoa

Reply

(@ikeike443) (@ikeike443) July 22, 2012 at 12:13 am

“MVC may be Slowing Down Your Site” http://t.co/lykXcXty

Reply

coders_vzla (@coders_vzla) September 10, 2012 at 4:41 am

Que opinan? Your MVC may be Slowing Down Your Site http://t.co/bU5wkVEn

Reply

abraxas November 20, 2012 at 12:51 pm

is there an alternative pattern to mvc? thanks!

Reply

Leave a Comment

Previous post:

Next post: