youri babakhanians - Fotolia

Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

New CDNs bring edge JavaScript to the app performance world

With content distribution networks loaded with edge JavaScript, Cloudfare promises to improve application performance and reducing resource consumption.

A new CDN offering from Cloudflare Inc. enables developers to use edge JavaScript to weave logic into applications....

With the Cloudflare Workers tool, developers gain flexibility in how they build applications. They'll have the ability to assemble webpages closer to the core of the internet, cache what is possible and then stream these pages, pre-assembled using edge JavaScript, to browsers.

"It is an implementation of the Service Workers API build on the V8 JavaScript rendering engine," said John Graham-Cumming, CTO of Cloudflare. This enables edge JavaScript applications to run on servers around the world rather than the browser. "We are saying there is a place you can run applications within a few milliseconds from the user," he added.

Developers are using this new architecture to advertise for A/B testing or to take data from low-powered IoT devices for server processing. Graham-Cumming even sees future AI uses.

One client is classifying images at the edge and enriching it with metadata. Cloudfare Workers could also be used for facial recognition in devices such as smart TVs.

Edge JavaScript logic revamped

This idea with edge JavaScript is not entirely new. Many developers have been able to do watered-down development with the open source Varnish Configuration Language (VCL) and Edge Side Includes (ESI). However, these lack functionality compared to JavaScript, not to mention the fact that VCL is just another language to master. Cloudflare will provide tools that automatically convert VCL and ESI scripts into JavaScript.

The core idea of a content distribution network (CDN) is to cache frequently used content -- JavaScript assets, images and video clips -- closer to the users. This reduces the load on back-end servers, allowing those to focus on application logic. Modern websites, however, are often dynamically assembled and customized from content pulled from multiple back-end servers. This edge JavaScript approach aims to make it easier to aggregate and deliver this content in one gulp to the user. And it will reduce the number of open sockets required to assemble the final page. The end result is increased performance with fewer resources used.

[JavaScript] solves the problems of building a webpage from a variety of back-end services at the edge.
C.J. SilverioCTO, NPM

In a sense, the new environment is akin to function-as-a-service offerings such as AWS Lambda for JavaScript, but it runs on the edge rather than a server. Developers can focus on the application logic without having to deploy and manage the underlying server. Another benefit of this architecture is that it provides a standard API without the need to deploy a node server at the edge -- a more heavyweight approach. It's also easy to update these apps. One change is quickly reflected across the various edge servers, a feature eagerly embraced by DevOps teams.

A more programmable edge

"CDNs are important to us," said C.J. Silverio, CTO at NPM Inc., which delivers JavaScript packages as a service. Over the last four years, its usage has exploded, with about 50 million downloads per week in 2014 to about 5 billion today.

"Effective use of a CDN is how we satisfy that load," Silverio said.

In the middle of a migration from Fastly, another CDN that offers VCL, to Cloudflare, NPM wants to take advantage of JavaScript at the edge.

"Effective use of Fastly has been key to us," Silverio said. "One of the reasons we are moving to Cloudflare is we need to make use of CDNs more than before to keep up with the growth, and I need to be more clever at the edges."

Programmability at the edge has been important for NPM. The use of VCL allowed NPM to perform tasks like matching on URLs and then requesting data from various back-end services. It also made it possible to make multiple requests and decorate these with headers before sending them off to the back-end servers to be fulfilled. To Silverio, VCL was a little clunky.

"JavaScript lets us use a full-featured programming language that we as a company are deeply expert in to write logic at the edge of the network. We can run a program out there written in JavaScript to take a request from somebody and fulfill it as quickly as possible using logic to figure out whether authentication is required or if we can fulfill it from the CDN. It looks like every other node program we write, and it solves the problems of building a webpage from a variety of back-end services at the edge."

At NPM, they use needle, a lean HTTP client, to cache and build the page on the fly from disparate data sources in Cloudflare Workers. They can also cache the different elements of a webpage separately and apply different caching policies. This makes the user experience faster.

Silverio cautioned that Cloudflare Workers is not a full-fledged edge JavaScript environment, because the APIs are limited and defined. There is no access to a file system either.

"You just have a function that is a Service Worker that is a standard JavaScript object, and it has to write data in a particular standardized way," she said.

Dig Deeper on Ajax Web development and Java client programs