Is it worth having static resources in a separate domain/server?
I am currently in the process of improving my grails website performance and following many best practices I found on the internet, I currently need to make a decision between two solutions before refacto开发者_JS百科ring my code
Solution 1 : Export all of my static resources (js, css, images) to a separate domain and server (as already done by SO team - see here).
Solution 2 : Just keep my resources into my WAR file and configure apache to act as a reverse/caching proxy so that incoming requests to /images, /css, /js etc are all cached by apache.
What do you recommend and what are the pros and cons?
PS: Concerning solution 1, is there any web hosting providers specialized for static content out there?
Thank you.
You should only do this if you've profiled and determined that latency caused by the browser queuing up the downloads is a major factor in the performance of your site. If the static resources load in a few miliseconds, but the HTML itself takes 3 seconds to download after the server has to hit the database ... then improving those few ms won't do much.
Solution one is the better solution. If you haven't already, you should consult the YSlow performance guide, as it will talk about this in greater depth.
Generally a browser will load page resources in a parallel fashion. However, the browser will limit the number of requests to a specific domain, so if all your resources are on that domain, some of them will block until a previous parallel request finishes. Splitting your resources to different domains facilitates this particular bottleneck, as you can have multiple requests going to images.yourdomain.com
and flash.yourdomain.com
at the same time. Building your application to utilize this style of resource separation from the start can be a benefit later, even if you serve them all from a single server initially.
You can implement all of Solution 2 as part of Solution 1, ie. have caching on your multiple domains. Generally it's cheaper to start with #2, as you can continue to serve from your existing servers, but have them configured for caching.
Once the big traffic starts you hit and you really want to optimize performance, splitting out your domains can make a difference.
If you're not already using it, I'd take a look at the ui-performance grails plugin. It implements many best practices around caching, minifing, and compressing things right out of the box.
As other posters have noted, I'd make sure that you know where your bottleneck is before doing anything too drastic.
It depends on how many resources your page/pages require. Remember browsers can only make a limited number of connections from a single server at a time. For IE 8 this is 2 at a time. If you have 40 resources you can only retrieve them 2 at a time. Spreading your resources out amoung multiple subdomains can greatly increase the speed of page loads on your site.
You can setup a number of CNAME records in your DNS all pointing back to the same physical server and greatly increase the speed of resource loading.
Also check out Google PageSpeed for additional ideas on speeding up your page loads.
精彩评论