C++: A language for next generation web apps

On Monday, I was pleased to be an uninvited speaker at Waterloo Devhouse, hosted in Postrank's magnificent office. After making some surreptitious alterations to their agile development wall, I gave a tongue-in-cheek talk on how C++ can fit in to a web application.

There were other cool presentations there too. Check it oot! Er, out!

During this presentation, I hope to convince you that the C++ programming language is ideal for developing your next web application.

You might be asking, Steve, Why C++? Why would I subject myself to this horrible language where you have to manage your own memory?

One reason is efficiency. Not only is C++ inherently fast. It also forces you to think differently. It discourages the use of overly complicated data structures that are built in to other languages. In C++, nesting more than one or two structures results in something that is simply too awkward to use. Instead, the you are forced to seriously consider using the simplest possible representation.

In addition, just about any library that you'd want to use has already been written, and has a free implementation available on the internet. Json, and CGI decoders are freely available. Also, as often overlooked, you have to handle UTF8 to Wide-character conversion, but this is easily achieved in a 10 line function.

The only thing you do have to be careful with is SQL database access. MySql is GPL'd, so you can't even link to its client library in a closed source app. SQLite seems to be free, except if you do business in Germany it is $1000 because they have a different definition of public domain.

Here is the first strategy that you might use to incorporate C++ into your web app. In this diagram, there are two things between the browser and your application -- the web server, and the php script. It is not as efficient as it could be, and there are also security implications with calling command line programs from php.

In this model, you write your C++ program as a CGI script directly, using one of the freely available query string parsing libraries. It is efficient and clean. When your browser requests information, the webserver starts your program, which spits out the response in json format. This response is then relayed back to the browser.

I use the above strategy for RhymeBrain. It includes advanced statistical algorithms that let it sound out any word that you put in. For example, you can enter the word "postrank" and see that it rhymes with such gems as "blank, prank, drank, tanked, and stank".

Because it's written in C++, I can run it on my super-powerful datacenter. It sits atop my sock drawer at RhymeBrain headquarters.

This powerful 1GHz beast can load the entire 2.6 million word database and fire back the response in about 50 milliseconds. It does this from a cold start, for each request.

But there is a third strategy: If you write your own webserver, you can cut out the middleman and serve the request directly. Your javascript code makes a request, and all your web server has to do is call a function to send back the results.

Writing a webserver isn't that hard. Here is the complete implementation of the Hibachi web server. It supports virtual hosts, and perl and php scripting, among other things. It was written by former Waterloo-ite Anthony Howe, and won the 2004 International Obfuscated C Coding Competition.

Inspired by Hibachi, I wrote my own webserver and built WebSequenceDiagrams (which runs in a real data center..). Doing it this way reveals a new business model. It is possible to package up the entire web application into a single installer that runs on Windows and Linux. Since it's all integrated, there is no need for customers to fuss around with Apache or the numerous other moving parts that could break if I shipped separate components. (You can run it in your organization for as little as $99).

The only disadvantage is that the final executable is really small -- around 700K. It may be a little smaller than some customers are expecting. During the presentation, it was suggested that I ship it as an appliance, in a huge box, to compensate.

Conclusion

I hope that I've convinced you of the benefits of C++ in your next web application:
  • Reduced hardware costs
  • Readily available libraries for web tasks
  • Portability
  • Extreme flexibility in deployment

Comments