What Can Lua do for You?


You’ve chosen the best content delivery network (CDN), configured your property, and your users are loving their experience on your site – what more can we do? How can you ensure that you are taking full advantage of what a CDN has to offer? Well, if your site makes use of JSONP (JSON with padding), we can utilize our advanced Lua engine to have the CDN cache that content for you, further decreasing load on your origin, increasing your cache-hit ratio, and squeezing even better performance out of your site. Let’s dig deeper.

What is JSONP?

JSONP is a method to have a code snippet interpreted by the browser despite originating from a different domain than your primary domain. Thus, this avoids conflict with browsers’ same-origin policy. JSONP relies on the <script> tag, which is not subject to the same-origin policy and can execute content retrieved from different domains. The JSON payload is wrapped in a function call (or, ‘padded’), allowing the browser to execute the function using the JSON object returned. The object’s padding is determined through a query-string, usually with ‘callback’ as the parameter and the function name as the value.

What is the Lua Engine?

Lua is a widely used lightweight, high-performance scripting language, used for all sorts of applications from games to web applications and image processing.  All of our edge servers across are capable of executing Lua scripts. One of the core benefits of Lua is that it is fully asynchronous. Asynchronous simply means that our edge servers can interrupt Lua scripts while certain operations are completing and allow another incoming request to take place. All the Lua code is written in a sequential format while all the asynchronous logic is hidden to the user. In addition, Lua is blazingly fast, leveraging the LuaJIT interpreter.

How can the Edgecast Content Delivery Network Help?

Using our Lua functionality, we will have the CDN make a request for a generic version of the wrapper, one in which the CDN can cache. Then, we replace the generic padding around the JSON object with the parameter in the query-string. This updated content is returned to the user. The next time the CDN receives a request, it doesn’t have to go far for the JSONP object as we already have it cached. A quick swap of the wrapper with what’s in the query-string and your users are receiving their content faster than ever before. Let’s take a look at this step-by-step :

The original request:


The Lua Engine requests this instead :


The cacheable response will be :

genericwrapper({“Foo”:”Bar”, “Number”:1, “Delivery”:”Fast”})

The Lua Engine swaps the generic wrapper for the query string value, and the CDN returns :

wrapper1({“Foo”:”Bar”, “Number”:1, “Delivery”:”Fast”})

For the next user, the request is:


Lua requests the generic object, which is in cache, then swaps the wrapper for the query string value and the CDN returns:

wrapper2({“Foo”:”Bar”, “Number”:1, “Delivery”:”Fast”})

What are the benefits?

The challenge in this scenario is the content varying for each request, thus preventing the object from being cached, increasing origin load, decreasing response times, and severely limiting the cache-hit ratio. With the above implementation, we should expect to see improvements in three areas:

  1. Performance. Without Lua all requests would go to the customer’s origin which is slow. Lua speeds up end user response times and improves performance as we pull the object from the cache.
  2. Origin load. Lua significantly lightens that number of requests to the origin.
  3. Cache-hit ratio. Goes from zero to much higher with Lua.

With our Lua engine the CDN is tailored to take better advantage of  caching. We’re not merely a simple proxy solution, we are an active piece in creating the best, most efficient way to meet your needs. We are here to help you implement this or any number of CDN customizations to your account.

Please do not hesitate to reach us for help on something similar or any of your existing challenges. Our knowledgeable engineers look forward to working with you!