How does the C++ layer work on the Web system?

Asked

Viewed 6,202 times

7

I see that many complex web systems say in their "API" that they use C++ as the "lower" layer, I don’t know if the question is too wide, but briefly I believe it is possible to explain to me how this layer works in C++.

I see that the Mega[old Megaupload] uses this layer as security. This would be the only example I know, I was told that the United States stock exchange also uses C++ in the bottom layer of the web system.

How would the request and response?

Who would execute the script C++?

What C++ does, which a Python cannot do for example?

2 answers

9

Because he is good?

In essence there are no specific reasons to use C++ for web. It’s rare and a lot of people divulge it to advertise, nor is it true. So don’t trust information you can’t verify.

Of course, there is one thing that C++ does better than almost all the others and mainly it does much better than the Python you mentioned: it is faster! This can be an advantage in many scenarios, especially when compared to interpreted and dynamic languages. But the real gain is the processing economy, which is confused with speed, but it’s different. It can be useful because it is possible to fulfill more requests with higher server density. This generates lower costs, especially in the cloud (although those who want lower cost do not use cloud, this is a product that gives many advantages but it costs expensive).

But if you compare C++ to C#, for example, with well-written code, good architecture, using the right technologies, you can achieve a very similar result with more productivity, security, reliability and robustness.

The speed itself is not so important because in general, even in slower languages, the processing time will be less than the access time to the database and the transmission time to the client. The time to send from the application to the HTTP server can be much longer. It may help a little, but it’s not essential. We are not talking about something that will take such a heavy load. Often the neck will be somewhere else.

To take the example of Mega, I doubt that they have more processing than access to "disk", which is much slower. Their bandwidth cost is so absurdly more expensive that I doubt it makes up for the savings on servers that I think they have the same way to handle so much disk. And another, neither they nor Stackexchange (I’ll talk below) save enough to pay the additional cost of programmers to have high server density.

Has techniques to make up the languages of script faster.

Another great reason to use C++ is to have a team available that knows this language and not others. They’ll probably do something better to her than in Python they’ve never seen.

One reason not to use is the lack of infrastructure in the standard language and library to work with. C++ even has a good handling of string for those who deal with it a lot. You will need to develop a lot of basic things or use libraries that help, not all will be suitable. It is much more difficult to program in C++, it is much easier to make mistakes. I do not know if this is an advantage, but for the naive can be better by having a string changeable, some people may not realize that their language uses a string immutable and in HTML manipulations it is common to touch them a lot, what needs a different technique.

There are some other advantages such as portability, ease of deployment, but that today is not very true anymore. In fact the need and build time of C++ is so disadvantageous that the difficulty of some languages to deploy (no longer occurs with C#, for example) is compensated. Some languages, again C# is an example, but the languages of stript are usually also very portable. As far as C++ if considering platforms mainstream where web servers are used.

I’m a performance fanatic and yet I admit that C++ for web it’s almost always a mistake.

Of course having a small part in C++ where the processing really is very heavy, can bring advantages, is rare but happens. Mega might use something very simple that you can’t even say is programming web, is something infrastructure. An HTTP server is usually written in C or C++, but this is not programming web, strictly speaking. A complex analysis of data to deliver on a page may benefit, but again we cannot call this a program web, how the result is delivered web, but so what? Programming web only take a request, query data, do a small processing to adapt the needs of the page, assemble the string page and send to HTTP server. If you have to do other things heavier is not a system need web in itself.

You can get even without C++

Are there any examples of website that did this way? Have this site here. Of course the Portuguese version of it alone is not too heavy, but when it adds up all the sites of the network and mainly the original site in English that originated everything, you will see what is possible to do. Has a page that gives details and makes me want to know more. One more. Other outdated data can be obtained in blog of whoever deals with this.

It’s just a shame they use techniques to save processing that harm the user experience.

How it works

The processed content should be done like any other application. You will access database, build the page with lots of manipulation strings, and will only change with the communication with the web server where you will receive the data of the requests and send the page or content (maybe JSON) through a protocol, such as the CGI or the Fastcgi or ISAPI if using Windows.

Hello world "web":

#include <iostream>
using namespace std;

int main() {
    cout << "Content-type: text/plain" << endl << endl;
    cout << "Hello World!" << endl;
}

I put in the Github for future reference.

  • A basic tutorial using CGI.
  • Wt - framework that helps in most tasks.
  • CSP - Programming based on templates (seemingly abandoned).
  • Cppcms - Full CMS serving as a framework for developing other applications or even to adapt what you need.
  • One more tutorial.

8


The reason they use C, C++ or even Assembly is when they need performance, not security because these languages give more control of the processor, so you can better plan the execution. But unless it’s something very specific, we can hardly overcome the optimization done by the compiler.

As for the implementation, it varies too much, often the staff mix other more common languages (PHP, ASP.Net, etc...) with external executables made in C/C++, but the focus is always the performance.

This is not to say making a website in these languages will automatically make your site ultra fast, ASP.Net and PHP have a cool performance and unless you handle millions of orders per second (stock exchange as you mentioned), you probably won’t feel any difference.

If you still want to learn, there are some frameworks for C++: http://cppcms.com/wikipp/en/page/main/

http://www.treefrogframework.org/

http://www.webtoolkit.eu/wt

It’s also possible to do it from scratch (recommend only if you want to understand how these frameworks communicate with apache or other daemon) where you literally respond to CGI or Fastcgi requests:

http://www.fastcgi.com/drupal/node/6?q=node/21#S1

http://www.rudeserver.com/vc_tut.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.