Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use CDN for js and similar resources vs local? [closed]

Tags:

html

php

cdn

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.

like image 816
BrownChiLD Avatar asked Mar 04 '17 02:03

BrownChiLD


People also ask

Is it better to use CDN or local?

CDNs are best suited for today's modern websites which involve different types of media files and a combination of static and dynamic content. With a local server hosting your website, the scope is also quite narrow and focused, and you don't have to worry about the nature and configuration of a network of servers.

Should I use CDN for JavaScript?

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.

Is a CDN faster than a local copy?

CDNs deliver faster loading speeds for readers. Because this content is readily available, it is pushed to users faster than would be the case in a local website server.

Should I use CDN for production?

To your question on Best Practices, there are a lot of very good reasons to use a CDN in a production environment: It increases the parallelism available. It increases the chance that there will be a cache-hit. It ensures that the payload will be as small as possible.


1 Answers

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.

like image 137
rockerest Avatar answered Oct 12 '22 04:10

rockerest