Friday, April 12, 2013

Pushing live updates to CDN (Akamai)

If you ever tried to live-updated your site which is hosted on a CDN, you realize it's take time for the update to take affect.
On big networks like Akamai it's can take really long minutes. Clearing the cache on all the network servers, can take ~40 min. Sometimes you need a live update in a matter of one minute, top (or even less). For example, on newspaper website, More than that, and your readers are gone.
One approach we conceived and found useful is working like this:

Using the advantage which CDN consider query-strings as new page and save it to the cache while the origin server consider it as the same page. 

On the server side:
When an editor is update an article it send a request to node.js server which saves a record to a MongoDB (or Redis) with the article ID, current version (increase number) and a timestamp.

On the client side:
On the article load there an AJAX requesting the node.js server, who querying to our MongoDB with it's article ID asking for version No. If any, there a content switch occurs by requesting for the new version using query-string e.g. example.com/article/32?version=3. Now there two scenarios, if this path is already cached its just grabs it from there. If not, the request hit the origin server which ignore the query-string and get the new, updated article. 

With this system we were be able to updated live articles in a real-time, and still manage an aggressive chach with an high TTL (Time To Live) of approximately a week long. While most of the articles are never changing or updated.