CDN, Cookieless Requests and Subdomains

In this text I will go in to the topic of using a separate domain for serving your static files to avoid the client sending unnecessary cookies in the headers and why it may be or may not be a solution to speed up your website.

The optimal way of speeding up static files - CDN's

While this write up will not be about CDN's in general (rather the CDN module) I will start out by pointing out the simple fact that if you have the economy to use a good CDN like CacheFly or Akamai it will be faster on a global scale, then using whatever techniques I will write about in this piece. The simple reason for this is that those CDN's does global distributed computing and can serve the client from a close geographical proximity. If you wonder what all that bullshit I wrote means in practice, it's like this - If Carlo is using his mobile phone in Buenos Aires, Argentine to surf your website which is located on your server in London, England then the biggest contributer to the time it will take for him to see an image from your server is most likely the transfer speed. So even if you apply all the means to your server to serve that file in 1 ms instead of 3 ms and compress it to the tops, the transfer time might anyway be 1000 ms.

A CDN that has a global outreach will cache this image for you globally and distribute the nearest geographical one to the client. So in our case with Carlo - if your website use a CDN, he will be served an image from a local server in Buenos Aires instead.

So unless you use a global cloud server network, you can't optimize this anywhere near to what a good CDN can do.

Please note though that there exists a lot of really crappy CDN's as well and that you get what you pay for. A CDN is not an universal solution to serve faster static files, a good CDN is. 

What you should think of before subdomains

First of all no, matter what you do or what web server you use, you have to learn how that web server software best serves the static files fast. While I have my favourites in Nginx and Varnish, even Apache and Lighttpd can serve static files fast if you just optimize them. I will not go into optimization on the web server as such in this post, and many people know more about this then me, but I can give you hints at least:

Nginx - read more about sendfile, tcp_nodelay and tcp_nopush.
Apache - read more about KeepAlive, KeepAliveTimeout, mod_mem_cache (or change to Apache Traffic Server)
Varnish - read more about Malloc (Varnish is really easy since it's made for static files)
Lighttpd - read more about file descriptors, KeepAlive, event-handler, stat-cache-engine and hope it does not leak memory

Also for any of these, read up on caching, compression and tmpfs since that can speed up a lot.

Cookies, cookie request headers and Drupal

Drupal works with cookies for logged in sessions, but other modules as ShareThis, ctools or Google Analytics might add cookies to monitor the client. This means that most likely any user might end up with a couple of cookies no matter how they use your site.

And here lies the performance probleem - for the server to keep track of what to do with the cookies they need to know the cookie information on each request to the server. For this to happen the cookie needs to be appended to the request as a header.

While most cookies are very small they still add up and make every request some bytes bigger.

And since the client does not know when the cookie is needed or not it will append the cookie to the header for each request no matter if it is an image where it's not needed or a html page where it is needed.

Subdomains for static files

If you own the domain an example of a subdomain would be for instance. Why are subdomains connected to the cookie problem? Basically cookies are appended to a domain and not to it's subdomain, meaning that if you logged in on you would get a session cookie that would not append to

So if we were to put all images on this would mean that any request to get something from there would not need the overhead of the cookie request headers.

All good? It's not so simple unfortunatly. First off you have to think carefully what files you put in the static files subdomain. For instance if you have private photos that only a special logged in user should be able to see, you will need to have the session cookie so you can confirm that person. If you just put every images on the static file subdomain, this means that also private images would be watchable for anyone that know the url.

Secondly you have the problem with DNS lookups. While DNS lookup tends to be fast and are almost always cached on the client there are specific cases where they might not be - mobile phones with limited connections for instance might have to wait a long time for dns lookups. Many mobile apps even solve this by pointing towards and IP number rather then a domain name, but on the open browsable Internet that might look bad.

Thirdly you will have to take in account that keepalive transactions are per subdomain, so while this can actually speed up the download if you have many files and split them up on many subdomains because some browser will only try to make X concurrent number of requests per subdomain, it can also make your web server run out of workers. And since it's actually using keepalive it might even improve without the subdomains if you have less files since it won't have the TCP/IP overhead of multiple connections to the same host.

So you have to take that into account depending on what your website is and who it is for, before deciding if you want to setup a subdomain for files. Also for all you people using SSL all over the board, if you have not implemented SPDY the SSL handshake will also be per domain/subdomain.

Another issue that might be a non-issue is how it will affect SEO. Some says it will, but most likely it won't since this is a recommended technique by the page-speed plugin from Google and since Google are doing this themselves.

How can this be done in Drupal?

CDN module is what you need. Follow these steps to make's images point to

After you have installed the module go to admin/config/development/cdn/details and set it up something like this

After that click the GENERAL tab and enabled the CDN.

The cookie photo above was taken by Brian Richardson and is used under the CC 2.0 licence.