FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Speed Tips: Use Multiple SubDomains

by 21 comments

To make your site even faster, serve certain content from different subdomains. For instance, on this site I serve (html, favicon, etc.) from www.askapache.com, (css, js, images, uploads, etc.) from static.askapache.com, and (flash, flv, swf, movies, mp3, etc.) from f.askapache.com. The reason this works is amazingly cool!

Easy Apache Speed Tips Articles

A subdomain is the part of a domain that is appended to the beginning of the domain name, like a prefix. For example, my domain is "askapache.com," and 3 subdomains I am using are "www.askapache.com," "static.askapache.com," and "f.askapache.com".


Speed Benefits of Multiple Subdomains

Basically when a visitor comes to http://www.askapache.com/htaccess/htaccess.html, they make a DNS request for each of my 3 subdomains [www/z/f].askapache.com to translate them into IP addresses. These hostname to IP address translations are then cached for that visitor, so no more DNS requests are needed. Because the first request was for http://www.askapache.com/htaccess/htaccess.html, the browser requests the .html document from the www server first. In the .html document the browser is told to also retrieve .css and .js files from my z server. In the .js file I load some flash which tells the browser to fetch the flash from my f server. So the users are able to see the content (.html) first, but thats not the only benefit to using these subdomains.

Subdomain Persistant Connections Benefits

The other benefit to using these subdomains is brilliant. Instead of a browser having to make 30 tcp connections to my www server to download everything, a browser is instead instructed to download 3 items from my www server, 22 items from my z server, and 5 items from my f server. So instead of waiting for 30 items from 1 source, it waits for 30 items from 3 different sources, drastically speeding up the transfer. By using multiple subdomains browsers are further able to maintain persistant connections with all 3 unique servers, making the site load incredibly faster.

Please read about Persistant Connections for more info.

Start using Subdomains

If you are an incredibly savvy and experienced web developer, no doubt you already have a DreamHost account, and so you already have the ability to add as many subdomains (for free) that you want. I think the limit per domain is 2500.

If you don't have DreamHost, it's never to late to start experiencing what the elite webmaster already has. For user and editorial reviews of DreamHost and other web hosts, check out Web Hosting Search

Tags

September 17th, 2007

Comments Welcome

  • Ajay

    I'm a little confused about this. Wouldn't the load on your server be the same?

  • http://www.h4x3d.com jez

    using this technique ever since I used to get more then 10,000 hits a day. dreamhost chucked me off for killing their servers btw. I am now on a dedicated/vps at zone.net

  • http://www.askapache.com/ AskApache

    @ jez

    I easily break 10,000/day so I got worried when I read that and contacted the friendly DreamHost Support Team asking them if they ban people.

    They love a site like mine that uses all these optimizations for speed and such. They assured me my site would never be shut down like that, and they do offer upgrades and even dedicated servers now.

  • http://www.h4x3d.com jez

    hey apache,
    thanks for your reply, although a month late. I reckon you have been busy?
    We wanted to work on a mutual partnership some time ago, not sure at what end the communication got stuck or if my mail server ate some mails. I am still up for it and my site is flourishing very well, averaging 12,000 unique and 17,000 hits a day earning me 50$/day on adsense.

    Well, the situation I described at Dreamhost took place last year when they did not have dedicated and/or virtual private servers. They had shared hosting and big butt hosting (for 395$/month). Dreamhost has always been very polite and honest with me, so the tech people I talked to recommended me to move out. The problem with my site(s) was the php-module within apache to cause too high-loads. I have been using the wp-cache plugin, so I did not understand why so. Lateron I found out this might have been the case because I was compressing my js and css files using the php-handler.
    this speeds up the site for visitors, but at a cost of the server´s cpu going crazy,

    If you are cool with Dreamhost than you can be very happy! I have been very happy, too, still I honor private support (personal) a lot because you always have a feeling of people taking care instead of some support service center with changing employees who tend to not know your custom setup.

    Yours, looking forward to hear from you,
    jez

  • rugs

    hmmm... now the hype about dreamhost starts making sense! I know that they had, at one stage, some significant downtime and a whole lot of issues. I think it had to do with large migrations etc. 2500 subdomains? That's brilliant. They've definitely got a great business model there.
    I like how you explained that subdomains can make things considerably faster. This is true. I'm definitely keen to make more subdomains for my various sites.

  • julien

    well...well...well that's what i call a useful website.

    thanks for sharing all these tips and tricks. Really cool.

    have you done some benchmarks to really see the exact difference in page load time? i'd be interested to see these numbers.
    cheers

  • Sukses

    Thanks for this articles. It is really informative.

    Yet, I have some difficulties in implementing this tips on my site. Could you please tell me how can I correct the js, css, and image files links to subdomain? Should I hardcode it and how can I do this?

    I'm a newbie in scripting and this is a really huge obstacles to me, however, I'm really interested in it.

    Thank you in advance.

    Sukses

  • http://www.duografikmedia.com julien

    Sukses:
    i haven't tried it yet, but to my knowledge you should be able to make it work using absolute URL in your .html and .css files. so to point your js file in the header of your html file do as follow:

    and if you have a url in your css file:

    body { background: #fff url(http://YourSubdomain.domain.com/path/to/your/image.png) no-repeat}

    it is obviously an example and you need to replace the color, repeat etc.. to your liking.

    that should make it work.

    cheers

  • http://www.myspace.com/kevin Kevin D. Freund

    There's a fine balance between forking hostnames and handling as few connections as possible.

    If you use too many different hostnames to download assets in parallel, you run the risk of thrashing the browser's HTTP keep-alive cache, meaning that you can no longer pipeline requests on the same connection because you are killing the browser's ability to hold open connections.

    For example, if you partition your assets into 10 hostnames, not only does the client have to suffer through 10 DNS lookups and 10 HTTP connections (with all the latency involved in constructing those connections), but you run the risk of the browser closing an HTTP connection if it hits the maximum amount of HTTP connections allowed and then later having to reopen a connection to the same host later on in the page load if the browser encounters another object from the previously closed connection. There's a finite number of connections many browsers will hold open simultaneously.

    Another consideration is that many users have asynchronous broadband connections (satellite, cable modems, and ADSL), meaning their upstream bandwidth is not as wide as their downstream bandwidth. The more connections you force the client to open, the more data the browser must send upstream in the HTTP request headers. This is especially true if you have a gigantic site cookie collection on the domain you're serving assets from, then this exacerbates the problem because with each connection, a client must upload the whole cookie every time it makes a request. Do you really want a browser to send a 4K cookie upstream at 128kbps in order to download a 53 byte clear.gif at 768kbps? It is always best if possible to serve assets (js, css, images) from a separate hostname/subdomain so that your main site's cookies are not transmitted upstream with each and every request (static content rarely, if ever, needs to access a cookie).

    So while leveraging parallel HTTP pipes to download content is tantalizing, recognize that there is a sweet spot. Sure, 4 HTTP hostnames to the same server may allow you to maximize the client's downstream bandwidth, but depending on your site's setup, you will eventually hit a point of diminishing returns with hostname forking.

    Cheers!

  • juylien

    Thanks Kevin for pointing this. Good post. It's always good to know the limitation of each technique.

    Pardon my question, cause i am just a novice who just love to learn more about all this; but you are more talking about a large site with high traffic, right?

  • http://richardxthripp.thripp.com/technology/ Richard X. Thripp

    This is not a good idea. Each subdomain requires a separate DNS request, which makes the site slower. This is exactly the opposite of what you should do for a faster website.

    Real speed benefits come from things like cached gzipping, merging CSS and JavaScript files, CSS sprites, moving JavaScript to the bottom of the page, and far-future HTTP expires headers, not this.

  • tandys

    I have been trying to do this with my Joomla site, but alas, Joomla doesn't allows such trickery. I am now getting quotes from component developers to try to bring such features to Joomla.

  • julien

    Well...well...

    What is the conclusion then? we have people saying it's good and others bad. And from the same host in one case (dreamhost).

    Maybe benchmark would be needed, no? or maybe explain that this technique might be good for DV or VPS but not for shared servers.

    Or maybe it's good for low traffic site and not heavy traffic or vice versa

    Are there any guru out there that can share their knowledge and not just their experience as it's seem to vary from one site to another.

    i dunno, i am just trying to figure out what's best.

    cheers

  • Mirang

    Do the subdomains need to have unique ip addresses?
    On a hosted account,you would normally have a shared ip,and the subdomain would also have the same ip as the domain.would the benfits be the same in that case too?

  • http://site-perf.com zuborg

    More domains mean more additional delays to resolve these domains. Anyway 3-6 such additional domains for images can strongly speedup your site loading speed if there is a lot of images to load.
    You can also do tests using tools like http://Site-Perf.com/ or YSlow to see what exactly cause your site to load so long time.

  • http://qfox.ru alex

    Thatx a lot for article!

    Some words about comments:
    don`t forget about *.domain.com A fields in name servers which describes a lot of subdomains.
    that can be very impressive and fury if you want to create them with just 1!(or 2 if you not use www.) dns query(ies).
    also you can use special subdomain to describe a group of subdomains...
    4example:
    subdomains z.s.askapache.com, f.s.askapache.com
    - and just one A record *.s.askapache.com
    ... two dns queries. and a lot of subdomains available now. )

    amazing)...

  • Tom

    Afaik the concurrent connection limit for IE / Mozilla is 2, which alone makes the benefit of this technique moot.

    But even the browser could connect to 10 hosts at once, what's to stop it connecting to the same host 10 times? It will do this automatically if it decides it'll help performance.

    Also remember that Apache and other servers will do Keep-alive on those connections, which eliminates the TCP/IP overhead of additional requests to the same host. You lose this benefit if you spread the requests. This cost gets higher the worse latency gets.

  • Marcin

    @Ajay:

    Yes, it would be, but thanks to this solution, browser can download files asynchronously, what will result in faster page load time.

  • non

    I'd like to use godaddy subdomain host on dreamhost, too.

  • Feeling Not Too Smart

    Hello,

    Recently I started using WordPress. Love the easy add-ons and upgrades, but hate the lousy page load time. Really can't complain too loud 'cause it's free software! (Most commercial, prioprority software include a TOS provision stating their software is not guaranteed to function and the consumer is not allowed to fix it either.)

    I've read several of your Speed Tip articles, as recommended by others. Thank you for your clear, easy to understand explanations. My prior attempts at some of the things outlined, although guided by Apache documentation, didn't go well. Now I think I understand why. These tips are useful for just about any site, regardless of the software, if any, driving the site.

    When using Multiple SubDomains with WordPress, how does one get WordPress to cooperate? WordPress spits out: http : // www site com/wp-content/themes/theme-name/library/images/favicon.ico. but we want http : // www site com/favicon.ico to show. For images or javascript use http : // cdn site com/file.ext for the URL. Also, what is the best method to reverse the process? (My limited rewrite experience is with using the same domain.)

    Any help you can provide is appreciated.

    Thank you,
    Doug

  • Pingback: Editando wp-config.php do wordpress

Popular Articles
My Online Tools

Related Articles
Newest Posts
Twitter



Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman






[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

| Google+ | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain