
In part two of two of my “Federal Election” series we are going to talk about two items. The first, which is the brain child of David Raso, is the postal code/riding look up. For that, I’m going to hand the keyboard over to him:
My Ridings
Hi, I’m David Raso a Senior Architect of Front-end Development at CBC.ca and Blake asked me if I could explain how one of our Election modules work. We wanted to build a module which would show you information and results for the riding you live in, so the My Riding module was created. This module allows you to enter your postal code and then displays information about your riding - it’s on every page of the Canada Votes site.

No Database Needed!
Sounds simple enough right? The simplest solution would have been to hook up the form to some sort or dynamic backed code written in Java or PHP which would search though the postal code database we got from Statistics Canada and display your riding info. Well because we get so much traffic on election night we have to build everything to be extremely stable and scalable. So using a dynamic application was out of the question - so then how do you provide a postal code look up database without a database? We could use some cloud computing (like Amazon or Google) or big complicated Akamai cached edge applications - but we like to keep things simple.
Everyone in meetings would say: “It can’t be done”, then you sit and stare at the white board. Few minutes later we came up with one of those “It’s so crazy it just might work” ideas.
When you enter your postal code into the form and hit search - we don’t connect to a database and find that postal code - we change your postal code into a url and using AJAX (a JavaScript programing technique) we fetch a flat HTML file which contains the ID of your riding. We then use that ID and AJAX, again, to fetch a flat HTML of your riding info. This is all done by your browser so all our web servers have to do is return those tiny HTML files your and computer does all the heavy lifting.
How It Works
So if you enter M9A2X1 we change that into “http://www.cbc.ca/news/canadavotes/myriding/postalcodes/m/m9a/2×1.html” we then download that file using AJAX. The files contents are stored as JSON (JavaScript Object Notation) which the My Riding JavaScript can easily understand and obtain the ID for your riding. Once we have your riding ID we then use AJAX again and download the file for your riding “http://www.cbc.ca/news/canadavotes/myriding/ridings/130.html” then all we have to do it display that HTML file in the My Riding module. Easy Peasy, Lemon Squeezie.
That’s all good and it works but you might be asking your self: “Does that mean you have a HTML file for each and every postal code in Canada?” - yup you’re correct. Over eight hundred thousand - 827,018 files to be exact. We took the database from Statistics Canada and created a script that would turn each postal code into a flat HTML file that contains the name and ID for the ridings in that postal code - some postal codes have multiple ridings, try out K0A1W0. We then uploaded the 3GB folder to our webservers - and because they don’t change we can cache all those files using Akamai.
On election night when the hundreds of thousands of users try to find their riding there is no database to go down, or application server to be bogged down. Just one small cached HTML file to download!
Comments and the Black Out
This is the first Election where commenting was allowed on CBC.ca. One of the issues we faced were users posting election related results in their comments.
This was an easy solution. We asked the 3rd party company that is responsible for moderating our comments (called ICUC) to not approve any comments for publication until the blackout period ended.
By: Blake Crosby
Categories: CBC.ca web site Under the Hood |
Email This Post
Add Comment »