42

I have a habit of storing everything locally and making sure my site is able to run even on local server without internet connection. This has been my principle ever since so that my site can run stand alone offline, and most specially for developing phase where my page refreshes are super quick and I can continue to develop my sites even without internet.

So in all my web applications and sites, I always download the packages and libraries I need and locally link to them for includes and what not.

I know more and more people are linking / importing libraries via public CDNs and stuff so I was wondering if there's any real SIGNIFICANT benefits to doing that?

I have identified the obvious (feel free to correct me):

PRO LOCAL - faster development experience - zero dependency on other servers that may break - faster user experience (?) since the site pages executes rapidly working with files/libraries on the same server already (localized)

PRO CDN - saves web space (but in today's world, this seems insignificant.. in a typical site you're able to save maybe 10-20mb worth of space only and what is that in the grand scheme of things nowadays and cheap servers and all)

  • lighter on server (?) = I'm actually wondering about this. Will loading from a CDN be lighter on the server's processor ram and i/o disk, or will it be worse since the executing document (eg. index.php) takes bit longer to finish there by hogging threads/mem/cpu cycles?

Thanks all.

Chad
  • 1,531
  • 3
  • 20
  • 46
BrownChiLD
  • 3,545
  • 9
  • 43
  • 61
  • *"faster user experience (?) since the site pages executes rapidly working with files/libraries on the same server already (localized)"* - If it's a publicly available website, why would having the files all on the same server be faster? I'm not saying it would be *slower*, though it might be, but they still all have to be downloaded to the end user's browser before they run, they don't run on the web server. – nnnnnn Mar 04 '17 at 03:43
  • oh true, i dunno what i was thinking hehe. good to be reminded thanks a bunch! – BrownChiLD Mar 04 '17 at 05:09
  • See [this article](https://httptoolkit.tech/blog/public-cdn-risks/). – x-yuri Nov 02 '21 at 08:59

2 Answers2

85

The benefit of a CDN is not (and should not be) for you. It is for your customers / users. Using CDN files - especially for widely used libraries like jQuery (or any other big library) - means they will receive a cached copy of the file from a physical location near them. In most cases, using a popular CDN for big libraries means the user has also downloaded that resource before - from someone else's site - so the file will be cached locally in their browser.

This helps reduce the time your user spends downloading files. It is not about your server load or your development ability in all but the smallest margin of edge cases. Think about your users first!

Using a CDN for most static files is strictly better for your users.


Update 2022-01-25:

It's worth noting that while this was true for a long time, it's really not the case any more. There are still real benefits from using CDNs (for example: files are served from a location in close physical proximity to the consuming end-user - on large, globally distributed CDNs), but the cross-domain resource caching is no longer a benefit.

@x-yuri noted this near the end of 2021, and all the major browsers implemented cache partitioning no later than 2020.

It is still beneficial to use a CDN, and it is still for your users' benefit, but the benefit is in reduced latency with geo-located servers, not in general performance due to a hot cache hit.

rockerest
  • 10,412
  • 3
  • 37
  • 67
  • I wonder how the order at which libraries are loaded is affected by using CDNs. Is it the same as serving them locally? @rockerest – Zilong Li Feb 10 '18 at 07:53
  • @Gnoliz According to this [Chrome DevTools Network Performance](https://developers.google.com/web/tools/chrome-devtools/network-performance/) resource, assuming ` – Chad Jan 28 '19 at 15:53
  • 4
    One more thing I don't like about CDN and similar stuff - they're changing! My website is not fancy landing page which will die after a year, it was running without any technical maintenance for 4 years before I joined the project three years ago. In 7 years every library will get updated or will die, so if I have it saved locally - I'm good and sure that my website lives. But when I use CDN I need to check "is it still alive?", "is there a new version?", "if there is, will it break any compatibility?", "why the server is not responding" and so on and so on... – The Godfather May 24 '19 at 14:16
  • 4
    Just wanted to leave this here for any folks coming to this later: "One more thing I don't like about CDN and similar stuff - they're changing!" This is incorrect use of a CDN. You should be identifying a version you want and using that on a CDN. There's nothing stopping you from pinning a version. – rockerest Jul 07 '20 at 18:11
  • Brilliant concise, clear, decisive summary! – Darren Murphy Sep 10 '20 at 18:30
  • In order to take advantage of a public CDN a library must be really popular, considering they have different versions. And these days owing to [cache](https://developer.mozilla.org/en-US/docs/Web/Privacy/State_Partitioning) [partitioning](https://developers.google.com/web/updates/2020/10/http-cache-partitioning) there's arguably [no point](https://httptoolkit.tech/blog/public-cdn-risks/) in using public CDNs. – x-yuri Nov 02 '21 at 08:48
20

Every HTTP/HTTPS request is an effort for the web server. Requests made for JS, CSS or any other static file would be redirected to a CDN which usually has a distributed, fast and stable architecture.

If you are managing a web app or site with pages that perform many requests and you are receiving a lot of users, the number of HTTP requests per second may increase dramatically; if your web server starts to respond slowly it would be a good idea to split the requests, CDNs are really good at managing static contents.

Basically performing tuning, the server may be scaled less and save money.

If you are not facing a lack of performance maybe you don't need to use a CDN.

Michele
  • 1,468
  • 3
  • 24
  • 54