Designing Your CDN Failover Strategy Table of Contents Overview 2 • CDN Problems Happen 2 • Uptime (and Speed) 2 Using Multiple CDNs 2 • Load Balancing Approach 3 • Aggregator Solution 4 Using Fallback Resource URLs • JavaScript and CSS 5 5 Using P2P CDNs 6 Integration 8 • Multi-CDN Tutorial 8 • P2P CDN Tutorial 12 Conclusion 13 Overview A CDN failover strategy ensures uptime and speed when your original CDN goes down. While full outages are rare for many CDNs, they can and do happen. The failover tactics we’ll discuss today can be used singularly - or combined with others when designing your CDN failover strategy. They will increase redundancy, guard you against outages, and even provide everyday “non-failover” speed benefits. CDN Problems Happen In 2014, Amazon’s CloudFront service experienced DNS problems that resulted in nearly two hours of intermittent issues for users worldwide. This goes to show that while CDNs improve the speed and reliability of your platform, they’re not foolproof. Not even those that some of the largest providers offer. Because rare instances of CDN issues will continue for the foreseeable future, having a CDN failover strategy is vital for businesses that can’t afford a moment of downtime. Platforms like LinkedIn, WordPress, and Tumblr learned this when their CDN (EdgeCast) experienced intermittent issues similar to CloudFront’s. Uptime (and Speed) In designing your CDN failover strategy, you’ll notice that many failover tactics can immediately benefit your “non-failover” content delivery strategy. Rather than solely helping you when unfortunate events occur, some can help you deliver content better as a whole. This is because when integrated correctly, you’re able to leverage other networks in addition to that provided by your original CDN. One of the most common ways people benefit as a whole in designing their failover strategy is with a multi-CDN. Using Multiple CDNs This practice known as multi-CDN does more than let you easily fall back on another CDN if the original one fails. It also allows you to increase your global presence. As mentioned in a MaxCDN blog post, there are two approaches to multi-CDN. 2 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 Custom Load Balancing Approach Many companies with a growing audience prefer this approach. Unlike the aggregator approach (see below), it allows you to keep your current CDN and strategically choose another CDN to stand alongside it. While one can integrate a second CDN strictly as a failover solution, it can also be used for dayto-day content delivery. Use Case You need a failover strategy, but also have a growing user base in China. To serve your failover needs and this new market, you search for a second CDN that: • has a network that closely mimics your original CDN’s in active areas (U.S. and Europe perhaps) • has a network with a larger presence in China than your original CDN Load balancing this second CDN with a service by NSONE or Cedexis would give you the ability to choose when - and how - your original CDN and second CDN is used. You could command a more robust content delivery network, or simply implement an as-needed failover solution. In the multi-CDN tutorial below, we show you how to do the latter. Pros and Cons Pros Cons Total control over what CDNs are used in Higher cost to maintain, depending on what your multi-CDN strategy and how many CDNs are used Unlimited opportunities for optimizations Not ideal for companies without an in-house and developer configurations web performance team Bottom line: More control Bottom line: Resource-intensive 3 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 Aggregator Approach This approach uses just one service provider that combines, or aggregates, individual CDNs for you so you can manage them through one control panel. There are obvious benefits to this approach, the clearest being administration is cut down. However, this approach is not often ideal for a failover strategy, unless you’re rethinking your original CDN. Because aggregators are already using multiple CDNs, this approach acts as a complete content delivery solution AND failover strategy from day one. Use Case You need a failover strategy, but aren’t pleased with your current CDN. You also have a global audience that, for the most part, is distributed evenly. Your current CDN performs well, but its network isn’t as evenly distributed as your audience. While you’d like to leverage multiple CDNs, you’re not comfortable with load balancing, configuring, and updating CDNs of your choosing. You’re fine with an umbrella provider choosing them for you and foregoing technical tweaks. Pros and Cons Pros Cons Single control panel allows for easier CDN Can’t manage control panels of individual management CDNs Good for companies without in-house web Limited control for teams with developers performance person or team who want to customize CDN Bottom line: Easy to use Bottom line: Hard to customize 4 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 Using Fallback Resource URLs A more code-centric approach involves modifying your web pages to load a resource from a separate URL in case the primary URL fails. If your website relies on a third-party CDN such as jsDelivr or OSS CDN, a fallback URL will allow your users to experience your website even if one of those CDNs is inaccessible. JavaScript and CSS A basic JavaScript fallback checks the state of a variable loaded by a script. If the variable isn’t present, then the web page links to a local copy. A cleaner approach would involve using a JavaScript library such as RequireJS, but the idea can be implemented in plain JavaScript: <script src=”https://code.jquery.com/jquery-2.1.3.min.js”></script> <script> if (typeof jQuery == ‘undefined’) { document.write(unescape(“%3Cscript src=’/js/jquery-2.1.3.min.js’ type=’text/javascript’%3E%3C/script%3E”)); } </script> In this example, the browser attempts to load jQuery from the jQuery CDN, then checks for the existence of a jQuery object. If the variable can’t be found, then it’s assumed jQuery wasn’t properly loaded and the browser loads a fallback URL. In this case, the fallback refers to a resource stored on the local server. In addition to JavaScript, the Fallback JS library supports fallback URLs for CSS and images. Similar to the above example, Fallback JS checks for the existence of a variable or DOM element before attempting one or more URLs. 5 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 <script src=”js/fallback.min.js” type=”text/javascript”></script> … <script type=”text/javascript> var config = { ‘libs’: { ‘css$main’: { ‘exports’: ‘.logo’, ‘urls’: ‘http://workingurl.com/main.css’ }, ‘img$logo’: [ ‘http://brokenurl.com/logo’, ‘http://workingurl.com/logo’ ], } }; ... </script> In this example, the script checks the main stylesheet to see if the logo class exists. If not, it loads the stylesheet from the URL provided. Below that, it checks for an image with the logo tag. If one doesn’t exist, it loads the image from the URL provided. Using fallback URLs adds complexity to your code, but they provide greater flexibility when using third-party CDNs. Rather than putting all of your eggs in one basket, you can spread your external resources across multiple CDNs and improve your overall reliability. Using Peer-to-Peer CDNs Building on the same idea that made file sharing services so ubiquitous, peer-to-peer (P2P) CDNs deliver content through users rather than a central infrastructure. When a user visits your website, a P2P CDN uses his or her computer to redistribute those resources to other visitors, reducing your bandwidth costs by up to 90%. With a P2P CDN, each user’s computer becomes a point of presence. 6 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 Any service that operates outside of a host’s control can be unpredictable, and P2P CDNs are no exception. They rely entirely on the resources of the user, and since many P2P CDNs operate via JavaScript, they only work while the user has a web page open. However, this hasn’t stopped web giants like Yahoo from scooping up developers in attempts to boost their content delivery abilities. Combining a P2P CDN with a traditional CDN could add reach to your website’s presence at a much lower cost, but those savings come with substantial trade offs. Content delivery is at the mercy of your users, and while many P2P CDNs will automatically fall back to your main CDN if performance levels drop, they are much more susceptible to delays and interruptions. P2P CDNs work well for delivering large media files, but they’re not a complete replacement for existing solutions. An example of a peer-to-peer CDN is Peer5. i Up Next... Integration Tutorials In this section you will learn how to integrate another CDN into your existing strategy with the load balancing service NSONE. You will also learn how to integrate P2P CDN using Peer5. 7 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 Multi-CDN Tutorial A simple way to use another CDN as a failover is to use DNS load balancing through NSONE. We review this load balancing service and others in this blog post. For this tutorial, we assume you want to ensure uptime by using MaxCDN as your main CDN and have a second failover CDN in case there are issues. Using NSONE’s advanced DNS hosting, you can build a multi-CDN system and fallback when required. 1 Get Account You can sign up for a free NSONE account here. 2 Change Name Servers Change the name servers of your domain to NSONE’s. For instance, if you want to use cdn.yourwebsite.com, you need to change the name server for yourwebsite.com. Make sure you recreate the DNS records correctly to not break your website. Once the domain is with NSONE, you can start working on getting multi-CDN set up. In this example, we assume you are using cdn.yourwebsite.com as your content delivery hostname. 3 Create Uptime Monitor You need to create uptime monitors of your two CDN providers. NSONE supports monitors like Pingdom, but in this case we will use their own monitoring system. (Note: It’s more reliable to use a third-party monitoring system as the NSONE monitor doesn’t support HTTP requests and other advanced features you might need.) 8 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 For each CDN, create a monitoring job with the following settings: Job type: TCP Job name: MaxCDN (or other) Regions: the more, the better Frequency: 60 Enable rapid check: Enabled Hostname: pull-zone.user.netdna-cdn.com (your CDN hostname) TCP port: 80 The result is two monitors checking your two CDNs: 4 Create New Data Source To make the monitors you created available inside the DNS itself, go to “Data sources.” From the existing data sources, select the first one called “Monitoring.” Give it a name and you are ready to go. (see Screenshot 2.1) Screenshot 2.1 9 maxcdn.com Screenshot 2.2 Have Questions? Chat Now or Call 1-877-629-2361 5 Connect Monitoring Jobs Once the data sources are created, you need to connect monitoring jobs to it. To do this, press “Add New Data Feed From This Source” under the newly created data source. Then add each monitor you created. (see Screenshot 2.2) 6 Create CDN “Answers” After connecting monitoring jobs to data sources, go to your DNS records and select the “cdn.domain.com” record you have created. Under “Answers” you need to create an answer for each CDN that you have. In the case of MaxCDN, that will look something like this (with your unique CDN hostname and username). Now for each answer you created, use the “Connect feed” button next to it to connect the uptime monitor to each answer. We also need to add a few metadata values to each answer: • Click “Add field” and add the “Up/down” parameter with default settings. • Then add the “Priority tier value” parameter and set it to 1 for MaxCDN and 2 for the second CDN. The finished Answers section can be seen in Screenshot 3.1. Screenshot 3.1 10 maxcdn.com Screenshot 3.2 Have Questions? Chat Now or Call 1-877-629-2361 7 Apply Failover Logic Now that the answers are ready, you need to apply failover logic on top of it. To do this, go to the “Filter Chain” section and: • Create a new UP filter: UP ensures the system only selects the providers that are currently online. It uses the “Up/Down” metadata parameter you previously added to each answer, along with the data feed connected to it. It instantly updates if a data feed reports downtime. • Add the PRIORITY filter: PRIORITY ensures that, under normal conditions, only MaxCDN is served to all users. • Add the “SELECT_FIRST_N” and set a value of 1: SELECT_FIRST_N makes sure that DNS responds only with one answer at a time. The finished filter chain will look like this ... The end result is this: cdn.domain.com uses MaxCDN to serve all traffic. If MaxCDN goes down, then all traffic is automatically switched to the second CDN provider. Once MaxCDN comes up, the system resets and serves MaxCDN again. 11 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 P2P CDN Tutorial Before integrating P2P CDN into your content delivery mix, it’s important to remember that P2P CDNs can’t be used solely as a CDN failover solution. While they do serve as such, they can’t be load balanced like a second CDN in the multi-CDN tutorial. From day one, the P2P CDN becomes part of your active content delivery mix. For some companies, this may be suitable. But for others it may not. To better understand if using a P2P CDN is right for you, read the pros and cons we outline in the “Using Peer-to-Peer CDNs” section in this PDF, or contact Peer5 at contact@peer5.com. Here we will show you the simple process of integrating Peer5, one of the best P2P CDN solutions. While targeted at video streaming, the service also supports file downloads and audio. 1 Get Account You can sign up for a free Peer5 account here. After getting an account, go to the integration page: 2 Enable CORS Make sure your server has CORS enabled. (To get a better understanding of CORS, check out this visual glossary article.) 12 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 3 Add JavaScript Add the JavaScript code provided by Peer5 to your website’s header. 4 Add Tags to Links To actually enable the P2P functionality, you simply need to add the HTML “peer5-download” attribute to all <a> tags you want to be downloaded using Peer5’s services. It is recommended to add this tag to links pointing to larger file downloads. Conclusion Creating a CDN failover strategy means knowing what impact downtime has on your bottom line. While CDNs are being depended on by businesses and users more and more each day, having a solid CDN backup plan could prevent you from losing the customers that CDN services help generate and retain. If you have questions about anything in this paper, contact Robert Gibb at rgibb@maxcdn.com. Based on your questions and our follow-up, we will update this paper accordingly. 13 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361 About MaxCDN MaxCDN is a next-generation content delivery network based in Los Angeles. Our entire team is obsessed with speed, automation, real time reports and implementation. At MaxCDN, the mission is simple: Provide the best possible CDN experience for today’s DevOps teams and the users they serve. MaxResponse MaxControl Ticket response times under two View CDN stats in real time, then minutes and 24/7 access to CDN provision changes instantly support engineers. through the API or control panel. No Request Charges No Extra Charge for HTTPS Traffic Rollover Bandwidth MaxArchitecture Anycast technology routes traffic to the fastest server for your users. No Contracts Necessary Great Prices Worldwide Have any Questions? Chat Now Or speak to someone by calling 1-877-629-2361 14 maxcdn.com Have Questions? Chat Now or Call 1-877-629-2361