C++ is primarily leveraged in Web Development for two reasons:

  1. Fine control over every aspect of the code.
  2. Performance

The History of C++ for Web Development

A lot of early web functionality was written in C++ using the Common Gateway Interface (CGI). Which is a specification that enables web servers to execute an external program, typically to process HTTP requests. An early use of CGI programs was to process forms.

The name CGI comes from the early days of the Web, where Web developers wanted to connect legacy information systems such as databases to their Web servers. The CGI program was executed by the server that provided a common “gateway” between the Web server and the legacy information system.

For each incoming HTTP request, a Web server creates a new CGI process for handling it and destroys the CGI process after the HTTP request has been handled. Creating and destroying a process can consume much more CPU and memory than the actual work of generating the output of the process, especially when the CGI program still needs to be interpreted by a virtual machine. For a large number of HTTP requests, the resulting workload can quickly overwhelm the Web server.

One way to improve the performance of CGI programs is to use precompiled C++ programs rather than CGI scripts, e.g. Perl, PHP or Python programs, which need to be interpreted by a virtual machine.

This “one new process per request” model makes CGI programs very simple to implement, but limits efficiency and scalability. At high loads, the Operating system overhead for process creation and destruction becomes significant. Also, the CGI process model limits resource reuse methods, such as reusing database connections, in-memory caching, etc.

To address the scalability shortcomings of CGI, Open Market developed FastCGI, a variation of the Common Gateway Interface, and first introduced it in their webserver product in the mid-1990s.

FastCGI allows long-running application processes hosted externally to the Web server to handle more than one request. Each application process listens on a socket; the Web server handles an HTTP request and sends it via the FastCGI protocol to the socket only for dynamic content, while static content is usually handled directly by the Web server.

Each individual FastCGI process can handle many requests over its lifetime, thereby avoiding the overhead of per-request process creation and termination.

A common modern alternative to using CGI or FastCGI programs is to use Micro Services or Web Services having their own HTTP server components behind a reverse proxy server, such as Nginx, which is optimized for serving static content, load balancing and security.

The Advantages of Using C++ for Web Development

There’s a good reason some of the most visited websites in the world use C++ for their backend code and dynamic content. Sites such as:

  • Google
  • Facebook
  • YouTube
  • Amazon
  • Twitter

Using C++ greatly improves the performance of their Web applications and reduces the overhead on their servers. Just as important, C++ enables fine control over every aspect of their applications.

C++ is used in many industries and can be used to write almost type of software application. It particularly excels in delivering performance and using system resources efficiently. The control C++ gives a programmer over system resources enables a skilled coder to write programs that are faster, more powerful and more efficient than similar programs written in another programming language.

Since C++ can out perform programs written in other programming languages, enterprises often use C++ to develop functions that have a critical reliance on speed and resource usage.

See 18 Powerful, Proven Benefits of Modern C++

C++ Libraries for Web Development

There are a number of C++ libraries used for Web development. Of these, Boost and POCO are generally the most highly regarded. They both have many high quality, well documented and well tested functions that are useful for a wide range of tasks. Since C++ 11, the Boost library project has made a considerable impact on the C++ Standard, and some of the Standard Library modules were derived directly from the corresponding Boost libraries.

The POCO libraries include a comprehensive set of C++ libraries that cover many modern-day programming needs.

The POCO network functionality includes:

  • stream, datagram, multicast, server, Unix domain and raw sockets
  • TCP Server framework (multithreaded)
  • reactor server framework
  • HTTP(S) client and server framework
  • HTTP Basic and Digest authentication
  • NTLM authentication
  • JWT (JSON Web Token) support
  • C++ server page compiler for embedding C++ code into HTML pages
  • FTP client
  • SMTP and POP3 client for sending and receiving email
  • URI and UUID handling HTML
  • forms processing
  • HTML template compiler
  • MIME multipart messages
  • SSL/TLS support based on OpenSSL
  • WebSocket (RFC 6455) client and server

The POCO libraries also have built in support for XML, JSON and database access. Written in modern, standard ANSI C++, using the C++ Standard Library, the POCO libraries contain clean, easy-to-use code.

Using an early use case of CGI programs to process forms. The following is a simplified example of an email form processor using the POCO Libraries.

Web development with C++ : Sending email

 if (checkInput(name) && checkInput(email) && checkInput(message) == true) {
  std::string contact_res = sendMail(name, email, message);
  ostr << "{\"type\":\"success\",\"message\":\"" << contact_res << "\"}";
 }

The result: Web development with C++ : Backend form processor

Conclusion

Regardless of what you may hear or read online, C++ is used for Web development. We’ve been using C++ for high performance Web development for years (20+). It’s a pleasure to work with and the results are outstanding.

How you can improve the performance of your web applications with modern C++

Learn how you can improve the performance of your web applications with modern C++

Related services: C++ Software Development

References: