C++ for high performance web applications
C++ is a fast, powerful object oriented programming language typically used for tasks such as systems programming, desktop application software, device drivers, embedded software, high-performance server and client applications, language processing and data mining, complex scientific and mathematical programs, and graphics intensive games programming ... in short, anywhere where fast, intensive data processing is required. C++ is known for producing programs that are not only fast (arguably the fastest of any general purpose programming language), but which also have a small memory footprint and which manage resources very tightly and efficiently. Importantly, C++ is also a cross-platform programming language that can be compiled for most common operating systems.
One field where C++ usage isn't common is web application programming, currently dominated by interpreted languages such as PHP, Perl and Python along with more complex solutions such as Java and .NET. As web applications are expected to perform increasingly complex tasks, faster, in greater volume, and more efficiently, is there a place for C++ web application frameworks? It seems that interest in such a proposition may be growing.
Back in 2006, Craig Tataryn asked none other than Bjarne Stroustrup himself:
... why hasn't C++ penetrated the web application frontier? Is it lacking an entity to write a specification for such a thing? In my googling, I can't even find commercial web application frameworks for C++. I just don't get it because it would produce some pretty fast, resource savvy webapps.
Craig pointed out how Sun's support for the development of the Java Servlet and Java Server Pages specifications ultimately led to the widespread adoption of Java as a language of choice for web application development. He also asked several C++ programmers what type of framework they used to build web applications. Their response was that either they a) don't write web applications, b) wrote their own framework, or c) used Ruby on Rails (this last option being described as "use the right tool for the job"). Anyway, Stroustrup's response was:
You have the answer in the first line "Java has Sun" or maybe more correctly "Sun has Java". That is, there was an organization willing to pay dozens of millions of dollars for development supported by more dozens of dollars for marketing. The C++ community never had that. Instead, many organizations built tools for areas they found important for their own customers.
In consequence, the C++ community don't have massive frameworks (unless you count CORBA), just applications (usually massive applications, such as amazon, google, ebay, and amadeus).
There is a lot of C++ "behind the scenes", e.g. financial software, embedded systems, games, infrastructure (e.g. CORBA, JVM), OS. See my applications page. There just isn't a mechanism for that to be seen (like Java's little coffee cup).
In other words, while C++ does not have the backing of a large corporation pushing a recognisable brand, it is in fact already providing critical backend infrastructure for many very large web enterprises. At this, the big end of enterprise web application engineering, C++ is a major player: not in the form of pre-packaged frameworks, but in the form of custom code.
For performance challenged applications that need to scale, the old adage that hardware is cheaper than developer time is still true in many cases - in other words, rather than spend thousands in developer time writing C++ code, why not spend a few hundred on physical resources such as more RAM or faster hard disks to achieve the same end? The case for C++ makes more sense as the size of the project increases. As Andre BarYudin puts it:
However, when we start talking about doubling performance of a very busy web site, with a lot of heavy activity, things could look a little bit different.
For example, imagine you are running an extremely successful advertising campaign. As an outcome, your current farm of 30 2U IBM xSeries servers does not provide you the performance you want anymore, and you think that it needs at least doubling. Therefore, in the end you will have 60 servers. Let's ignore their acquisition cost for a minute (but it will be a hefty $60,000-$100,000 or more). What if you don't have enough floor space in your data center to accommodate them? Will you have to move to a new one, increasing your rent bill, incurring the cost of removals and suffering service disruption? What about extra system administrators you might need to hire in order to support them? There are other components in your system, which might require upgrade, databases for instance.
The conclusion is that doubling a large computer farm is not as simple as buying two servers instead of one. Therefore, for large and business-critical application it makes sense to consider investing into performance, even if it means using less popular technologies.
C++ is a good fit for web applications which:
- service very large numbers of visits
- demand strict management of memory and other resources
- are highly dynamic and contain a significant proportion of content that cannot be cached effectively
- perform complex computational tasks
- provide a web interface to existing C++ applications and systems
- are constrained by limited resources (eg. embedded web applications)
Taking a much wider and more speculative view, if more efficient web applications and platforms meant that less servers were required to power the internet, this would represent a significant global energy saving measure.
What is holding C++ back? One of the main problems appears to be a lack of code infrastructure - suitable libraries to abstract common, laborious tasks into the background. While C++ has a large number of libraries capable of many and varied complex computational tasks, specifically web-related tasks have not been well supported until recently at least. The following links indicate that development of those libraries may well be underway, and that several C++ web application frameworks have appeared recently. Another significant event on the horizon is the pending publication of new C++ standards (unofficially referred to as C++0x) in 2010/2011.
There appears to be growing interest in the use of C++ as a web programming language, primarily for reasons of performance. Developers have discovered that ready-made CMS applications such as Joomla and Drupal (and many other applications and frameworks written in several interpreted languages) simply do not scale well - they tend to use an inordinate amount of resources, especially when overladen with many sometimes poorly written contributed modules. Drupal developers have benchmarked 'full-featured' Drupal installations which result in Apache processes of around 95MB each! Try running that in a VM with 128MB RAM and see how it scales.
Joseph Montanez on the other hand did some C++ vs PHP benchmarking and determined that his home-grown C++ system could outperform Apache+PHP by a factor of 6 to 10 on most measures. Artyom Beilis has produced similar benchmarking which also shows a performance improvement factor of around 10 comparing C++ and PHP (WordPress) wikis. Clearly, C++ has the potential to perform far better delivering web content efficiently than interpreted languages such as PHP running on Apache. This doesn't matter much to many people (though if nothing else it is simply inefficient and a waste of energy, which adds up across millions of sites), but to some improvements such as this can mean significant savings in hardware and infrastructure. Taking the wider view again, how much energy could be saved globally if internet applications generally were 10 times more efficient than they currently are?
See the C++ web application software page for more details of open source software and projects aiming to provide C++ web solutions.
