Intermittent Cloudfront Content Delivery Network (CDN) disruptions (monitoring) - Implementing CDN Fail

Over the past two months, I've been dealing with sporadic issues involving Amazon Cloudfront. These failures occur 2-3 times a week, where the page will load from my web server but assets from the CDN linger in pending status for minutes at a time. It seems to vary depending on the edge location, particularly in London. Despite reporting the issue to Amazon, their response has been dismissive, essentially stating that they won't investigate unless numerous complaints are received.

Given limited resources to switch to a different CDN, I've decided to implement a script in the HTML header to alert us when similar incidents occur. The plan is to attempt downloading a small gif from the CDN and if the request takes longer than a specified time frame, trigger a call to a particular URL within our domain for monitoring purposes.

Now, onto the question: What is the most reliable method, compatible across all major browsers, to request a file with a callback on timeout?

  • Using AJAX to request the file from the CDN may not work due to cross-domain restrictions.
  • Will setting a setTimeout function with a callback be effective, or could it get blocked by a pending HttpWebRequest request?

Are there any other approaches worth considering?

Thank you.

Answer №1

After conducting brief tests on multiple browsers such as IE 7 & 8, Firefox, Chrome for both Windows and OSX, I recommend trying it out yourself and minifying the code. If you have any better suggestions or improvements, please feel free to share them with me. I did consider using a script instead of an image but ultimately decided against it due to my lack of knowledge in that area.

In the upcoming version, a cookie will be written upon timeout and subsequent requests will be handled server-side using relative asset paths. The cookie will expire after 30 minutes and will be renewed with each consecutive timeout. As for the initial failover, I am considering a redirect as a simple solution, although I might explore more elegant yet complex alternatives as well.

<script type="text/javascript">
//<![CDATA[
// Absolute path to a picture on your CDN to be monitored
cdnImagePath        = "http://YOURCDNADDRESS.net/empty.gif";
//this is relative path (cross domain limitation) 
//will be followed by "timeout" or "other" as a reason i.e. /cdnMonitor.php?message=timeout
cdnMonitoringPath = "/cdnMonitor.php?message=";
// Recommended 3000 for 3 second(s) timeout
cdnTimeoutMilisec = 3000;
// Set to true to be notified after timeout (provides extra information)
cdnNotifyAfterTimeout = false;

// Handler methods
cdnOK = function(){
     if (!cdnTimer && cdnNotifyAfterTimeout) cdnNotify('success');
}

cdnFail = function(reason){
     if (reason != "timeout") {
          if (cdnTimer) clearTimeout(cdnTimer);
          message = "error"
     } else {
          message = reason;
     }
     cdnNotify(message);
}

cdnTimeout = function() {
     cdnTimer = false;
     if (cdnImage.complete == false) {
          cdnFail("timeout");
     }
}

cdnNotify = function(message) {
     if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", cdnMonitoringPath + message, true);
        xmlhttp.send();
     } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
}

// Load test image and define event handlers
cdnTimer = setTimeout("cdnTimeout()", cdnTimeoutMilisec);
cdnImage = new Image(); 
cdnImage.onload  = cdnOK;
cdnImage.onerror = cdnFail;
cdnImage.src = cdnImagePath + "?" + Math.floor(Math.random()*1000000);
//]]>
</script>

Additionally, here is the code snippet for ad hoc monitoring on the server side with cdnMonitor.php:

error_log(date('Y-m-d H:i:s.') .next(explode('.',microtime(1))). ' - '. $_GET['message'] . ' - '. $_SERVER['HTTP_X_REAL_IP']. ' - ' . $_SERVER['HTTP_USER_AGENT'] ."\n", 3, '/tmp/cdnMonitor.log');

You may need to adjust the "HTTP_X_REAL_IP" to REMOTE_ADDR or any other variable depending on your requirements, especially if you are using a reverse proxy like I do.

Finally, I made some last-minute changes in the post editor so there may be some issues. Hopefully everything works smoothly!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display issue with loading image in Internet Explorer using jQuery AJAX

I am facing an issue where my page has links on the left and data displayed on the right side. Each time I click on a link, I utilize a jQuery AJAX call to load the corresponding data. To enhance user experience, I display a loading image while fetching th ...

Securing ajax requests in Laravel: Best practices to keep your data safe

Utilizing ajax, I am able to store, update, and delete resources that are linked to authenticated users. The routes for these actions make use of the web middleware, allowing access to cookies, sessions, and more. This project is built on the Laravel frame ...

JavaScript and Angular are used to activate form validation

Update: If I want to fill out the AngularJS Forms using the following code snippet: document.getElementById("username").value = "ZSAdmin" document.getElementById("password").value = "SuperSecure101" How can I trigger the AngularJS Form validation befo ...

React: The error message "p is not defined" is showing up in the component file due to the no-undef

In my React application, I am looking to display a list of menu items from an array and show the detailed description of each item upon clicking. The array of menu items is stored in a separate file called dishes.js. The menu items are rendered using a Me ...

Creating Your Own Image Hosting Website: Learn how to consistently display an HTML file with a specific image from the URL

I'm currently in the process of developing my own image hosting site at Everything is functioning as intended, but I am looking to make a change. Currently, when a shared image link is opened, it only displays the image. However, I would like it to ...

What steps should I take to export a function from a React functional component in order to create a reusable library?

Currently, I am in the midst of developing a React component library and one of my components contains a function that I want to export. The purpose of the addParticle function is to enable users of the library to dynamically insert particles into a cont ...

Encountering issues with Monaco Editor's autocomplete functionality in React

I'm facing an issue with implementing autocomplete in the Monaco Editor using a file called tf.d.ts that contains all the definitions in TypeScript. Despite several attempts, the autocomplete feature is not working as expected. import React, { useRef, ...

JQuery code causing issue with properly closing toggle menu

I am currently working on implementing a closing toggle menu for mobile devices and facing a minor issue. What I aim for is to allow users to close the toggle menu by tapping anywhere on the screen of their device while it is active. I have almost achieved ...

What is the most efficient way to automatically start the Webpack-dev-server every time a file is

Is there a way to automatically run and refresh webpack-dev-server when I am using the Atom package called AutoSave OnChange while running my application? This is my configuration for webpack-dev-server: devServer: { contentBase: './src/inde ...

Loading pages asynchronously using Ajax with added interactivity through custom JavaScript scripts

Using jQuery Masonry in conjunction with another JavaScript for voting, here is the code: <div id="contain"> <?php $result = $mysqli->query("SELECT * FROM posts ORDER BY id DESC LIMIT 0, 20"); while($row = mysqli_fetch_array($result ...

Is there a more effective way to structure my code than using multiple "IF/ELSE" statements?

Here is the current code snippet that I have: (category=="Ljud & Bild") ? byId("nav_sub_ljud_bild").style.display='block' : byId("nav_sub_ljud_bild").style.display='none'; (category=="Datorer") ? byId("nav_sub_datorer").style.disp ...

What is the process for adding color to an Object3D Object in ThreeJs?

My project involves importing Objects from a file, and I want to be able to color them by clicking on them. After attempting the following code: let mat = (this.scene.children[4].getObjectByName(intersects[0].object.name) as THREE.Mesh).material.color.set ...

Scraping dynamic content with Python for websites using JavaScript-generated elements

Seeking assistance in using Python3 to fetch the Bibtex citation produced by . The URLs follow a predictable pattern, enabling the script to determine the URL without requiring interaction with the webpage. Attempts have been made using Selenium, bs4, amon ...

Issues encountered with ajax functionality in header.php

Currently, I am delving into Ajax in WordPress and using a tutorial as my reference. I have created an Ajax script that includes a simple form with a name field. This script is being called in the header.php file. However, upon implementation, I am encount ...

Determine if Param is empty or not within the context of express.js

I am looking to create a table and populate it with all the parameters that are not empty in the parameter list provided below: http://localhost:5002/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="407535732b7008121931190539142 ...

What do you notice about interactions involving 'input type=text' in HTML and JavaScript?

When a new binding is created for the value property on an input, any manual modifications by the user no longer update the value. What happens when the binding is altered? Does regular user interaction involve key press listeners? I've modified the ...

Input the selected checkbox options into the designated text field

My form consists of 3 arrays of checkboxes - hummus[], mayo[], jam[]. Currently, the jQuery functions disable the remaining checkboxes once the required number is checked. Query: I am attempting to transfer the values of the checked checkboxes into text f ...

Retrieving information from an openDatabase using AngularJS

Encountering an issue while trying to retrieve data from openDatabase and display it in a listview. Following advice from a thread, I added $scope.$apply(); after $scope.items = $data;, but this resulted in an error: [$rootScope:inprog] $apply already in p ...

Modifying the background color using highcharts.js

I am attempting to customize the background colors of a highcharts scatter plot. While I can easily change the color of a specific section using the code provided, my goal is to apply multiple colors to different ranges within the same plot. Specifically, ...

Is it possible to apply the active class to the current list menu item in Mark using server-side techniques instead of client-side JS

When a menu item is clicked, I want to add the active class to that specific menu item. For example, if someone clicks on the contact page, the contact option in the menu should receive the active class. I am currently working with NodeJS and Koa. Below is ...