Is Your CDN Slowing You Down?
July 27, 2015 | Aleksandar MaticWhen choosing a CDN to deliver your web content, various factors need to be taken into consideration: the reliability of the network, the audience the CDN serves, and the ease of integrating the CDN into your web service. But to many, one of the most important factors is speed, or how fast the CDN delivers content to users. When configured properly, there’s no doubt a CDN makes your web service faster. But proper configuration doesn’t always happen. Whether because you decide to forgo an integration service (like the complimentary one offered by MaxCDN) or because you recently made changes to your website without making changes to your CDN, your CDN can actually have the opposite of its intended effect. Often, the main reasons for CDN slowdown are cache misses, or requests that are redirected from the CDN to the origin. As you can see in the image below, a redirection causes four actions instead of two, meaning added latency. This is why, whenever possible, you want to aim for cache hits. This ensures that users get assets directly from the CDN. Image 1.1: CDN cache hit and cache miss scheme In this post we’ll explore popular misconfigurations that lead to your CDN getting in the way - rather than helping out - with fast loading times. We will then help you configure it properly. You’ll see that once you know what the problem is, the fix is pretty simple.
Reason 1: Redirection of HTTPS Requests to OriginNot long ago Google announced HTTPS as a ranking signal. This, among other recent developments in web security and performance like HTTP/2, is causing more businesses to make the switch to HTTPS. When uploading the SSL certificate to a website, many people tend to force a redirect from HTTP to HTTPS. This way all requests come through encrypted links. But this redirect presents an issue if you have http://www.domain.com set as the origin URL on your CDN. If left as is, the CDN would have to redirect requests back to the origin, resulting in added latency in asset loading time. To have secure assets delivered by the CDN, and thereby dramatically decrease latency, the fix is simple: Set the origin URL on your CDN to https://www.domain.com. This connects your CDN pull zone directly to port 443 (HTTPS) and removes the need for redirection. Image 2.1: Zones -> Pull Zones -> Summary -> Zone Configuration in MaxCDN control panel. Another way to cut down on latency caused by HTTPS connections is to use HTTP Strict Transport Security (HSTS). By adding an HSTS header, redirections are performed by the browser itself, completely removing the initial HTTP connection and redirect to HTTPS. Related Reading: How to Use HSTS with MaxCDN
Reason 2: Redirection of WWW and Non-WWW RequestsSimilar to the HTTPS redirection case above, there are a number of websites where requests from domain.com are forced-redirected to www.domain.com, and vise versa. In this case, the origin URL on your CDN should be set to the domain that the redirects are set to. As with many of the misconfigurations presented in this post, this is an easy fix once you identify the problem.
Reason 3: Headers that Don’t Allow for CachingOther causes of the CDN interfering with optimal performance are misconfigured caching headers. Below we’ll point out the three biggest culprits for cache misses. 1)
Cache-Control: privateThis caching header indicates that all parts of the request are intended for the end user only and that it must not be cached by other sides such as the browser or reverse proxy (i.e. CDN). In order for files to be cacheable, this needs to be set to
Cache-Control: no-cacheThe no-cache caching header is interpreted the same way "private" is. If your files contain this header, it should be removed in order for your files to become cacheable. 3)
Cache-Control: max-age: 0The max-age header determines the amount of time the file is cached on the CDN. When the number is set to 0, it means the CDN must request the file from the origin and then pass it on to the end user. Ultimately, no caching can take place with this header enabled. Your CDN can override these headers though. Image 2.2: Zones -> Pull Zones -> Settings-> Cache Settings in MaxCDN control panel. As you can see in Image 2.2, MaxCDN lets you ignore some cache-related headers set by your origin. You can also easily control how long your content is cached on the CDN (1 day, 7 days, 30 days, 1 year). Related Reading: Using Cache-Control Headers
Reason 4: Unnecessary Query StringsMore often than not, your website will need a plugin or module that provides you with some additional functionality. Some of these plugins, however, may add unneeded query strings to your files. Query strings are good for serving different versions of a file through the same url, by just changing the query string at the end of the file (?v2.0, ?v2.1 for example). In a use case where you don't serve different file versions using query strings, they act as unneeded “cache busters.” Each request containing a new query string represents a new request and the CDN interprets it as a new file that needs to be cached again, even though the content is the same. Ultimately, this results in unneeded requests to the origin server. You can surpass the query issue by setting the query strings option in your pull zone to disabled. This can be done in the same section of the MaxCDN control panel shown in Image 2.2. We elaborate on this, and the next section on cookies, in the PDF titled Improving Your CDN’s Cache Hit Ratio. Related Reading: How to Ignore Query Strings Selectively
To have your CDN ignore the cookie header and thereby make the file cacheable, you need to strip all cookies. This can be done on the same page of the MaxCDN control panel shown in Image 2.2. Related Reading Use CDN With Set-Cookie Sent From Origin How to Use CDN with CloudFlare
$ curl -I domain.com HTTP/1.1 406 Not Acceptable Server: cloudflare-nginx Date: Tue, 14 Aug 2012 14:31:59 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding,Cookie,User-Agent Expires: Wed, 15 Aug 2012 14:31:59 GMT Set-Cookie: __cfduid=de8b7126ad3841cd0da33c69651231c901344954719; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.domain.com
Experiencing CDN Slowdown? Find Out. Fix It.Because many misconfigurations are revealed through cache misses and headers that prevent caching, here are a few things you should do to see if your web service is experiencing CDN slowdown.
- Check the cache hit ratio provided by your CDN. A cache hit ratio of 99% for static files is optimal while 80% or lower often signals a problem.
- Use tools like WebPagetest or cURL to inspect headers. Look for the headers we mentioned above in reason #3 attached to the responses for your static files. Were these headers set intentionally?