Well yes, C ++ sites
A couple of topics about the web in C ++ slipped through and many comrades misunderstood the meaning of using C ++ on the web. Therefore, I will tell you how I used this in my experience. True, I used Python, but only because libraries like WebToolKit were not at hand.
To be honest, I understand C ++ as a hacker in female lipsticks - like many, I mostly use PHP (and more and more Python, but rarely for the web - more for OpenGL), but nonetheless it does not bother me (C ++ ) use. This is because do not be alarmed by the complexity of the language - there is not everything as bad as it seems.
But, what's important: "Premature optimization is the root of all evil." And that is important. It is pointless to write any complex system immediately in C ++ (I'm talking about sites, of course). Optimize, as a rule, you need only 3% of the code. I’ll tell you about these 3 percent.
I had a website and on it on all pages users could write something (explain for a long time) and in the corner there was a “stream” in real time (via AJAX update every 5 seconds) who wrote what and where. It was done, as it should be in PHP / MySQL - a table, in it we select the last (relatively speaking: WHERE id> MAX (id) -10 ORDER BY id DESC) records. Periodically delete everything except the last ten. It worked great. So far, TechCrunch has not written about the site. The server died because of this part. As soon as I turned it off (remember about 3%?) - all the tens of thousands of people that they sent me - they began to see the site very well. I solved it clumsily - made memcache. PHP fetched the last 10 entries and wrote in memcache, and upon request, returned a line from memcache. However, this decision as a result began to die.
I wrote a simple Python http server (from the standard library) that had an array of 10 lines and gave it an AJAX request or added a new one and deleted the first (FIFO).
That is, PHP (on the server) processed the forms and submitted via file_get_contents ('http://mysite.com:9000/?put='.urlencode ($ _GET [msg])); And AJAX (in the sense of a client in JavaScript) was running on mysite.com : 9000 /? get - bypassing all Apaches, PHP interpreter, MySQL and even memcached (which still needs to be connected - and this time is an overhead).
Writing such an application in Python or C ++ with a good HTTP server library is no difference. C ++ already has quite a lot of tools for high-level programming. Even the average PHP programmer is able to write safe and serious applications in modern C ++.
But, and here it’s already interesting: a running Python HTTPd script eats up to 10-20Mb of RAM, and a running C ++ server with the recently published WebToolKit only 0.6-1.4Mb (we are talking about RES memory). So such small optimizations on the server can run the sea (unlike Python, which in pieces of 20Mb on a small VPS quickly eats up all the memory).
Similarly, make, for example, a real-time display of users on the site - who-on what page, from where.
As a result, the site is still in PHP, it is accelerated by dozens of times (due to optimization of the hardest piece to execute), the database is offloaded, no dog pile caching effects, etc., and in time, it took about 2-3 hours this optimization.
Another example is voting. Let's say that you need to add a voice for someone through AJAX and save the user's IP so that he no longer votes. In C ++, it will take in memory well, maybe byte 20, so you can safely store all the data about all users and votes over the past week with instant access to them. At the same time, if this is done in the usual way (PHP / Python + MySQL / SQLite / Postgres ...), the database will grow and access to it will slow down and if calls go on constantly, this can significantly slow down the server. I am not saying here that SQL will lose once in 10 memory, but I have had such cases (in particular, SQLite: memory: loses to file SQLite tens or even hundreds of times).
Another example is the user's GEO location. You can load information about all IPs into memory (of course in the form of range) and C ++ will go through one cycle in nanoseconds, at the same time, PHP + MySQL will dig for a long time choosing the desired range.
Also: a counter for something, let's say the number of votes for a post (or karma) - you really can keep the karma of all users in memory and let AJAX directly access the server, at the same time PHP / MySQL, and what Django will do: check it has changed whether the file is (PHP), we will do a regex check where we are going (django routes), if there is no optimizer in PHP - read the file again, parse it, initialize the connection to the database, request one digit, set the lock so that no one changes it yet, increase it, return back, remove the lock.
And this is if there is no ORM, and yet Django still creates objects in order to process the request and return it. This is all, of course, a problem, not a catastrophe, but then again - if you spend 99% of your time on useless things - isn’t it better to get rid of them in an hour?
At the same time, for C ++ it is almost a pair of processor clock cycles (+1 to Integer in memory and translate into a string). Simply put, while PHP + MySQL processes one such request, C ++ can do a thousand, maybe a million (if you do not take into account the time to process the HTTP protocol itself). Yes, and such a C ++ program will in fact be shorter (but hardly much longer) than PHP + MySQL.
In general, if there are tasks of the level of “add something to something”, “find something among a small set of something”, then pieces of C ++ sites can help you a lot. But the general idea is still better to do something higher level (such as PHP / Python) or look at languages such as Arc.
The disadvantages of this approach?
Well, more troubles.
1. Monitoring, for example. As an option - the simplest, let's say there is compiled_file.cpp, which compiles to compiled_file (without an extension). We go into crontab (crontab -e) and append:
* * * * * / bin / pgrep compiled_file || / path / to / compiled_file
The script will lie for a maximum of a minute if something goes wrong, then it will restart. Really - less than half a minute.
2. Update the working process. Yes, just by replacing the file, as PHP will not do, you have to kill the process (killall compiled_file) and restart it.
So do not immediately discard the idea of sites in C ++, but you don’t need to suffer fanaticism either. You can write a whole site in C ++, but if you have half a person a day, then why?
Just using this language should be justified. If your server is 100% loaded and you know that 90% of requests go to one place, which could be very simply expressed as a small server with a memory print of 1 MB and which would not require databases, interpreters, etc. - then why buy more expensive equipment, instead of replacing this piece with a compiled one?
And if C ++ is really scary - then look at what Cython is for Python.
Yoi Haji,
view from Habr
To be honest, I understand C ++ as a hacker in female lipsticks - like many, I mostly use PHP (and more and more Python, but rarely for the web - more for OpenGL), but nonetheless it does not bother me (C ++ ) use. This is because do not be alarmed by the complexity of the language - there is not everything as bad as it seems.
But, what's important: "Premature optimization is the root of all evil." And that is important. It is pointless to write any complex system immediately in C ++ (I'm talking about sites, of course). Optimize, as a rule, you need only 3% of the code. I’ll tell you about these 3 percent.
I had a website and on it on all pages users could write something (explain for a long time) and in the corner there was a “stream” in real time (via AJAX update every 5 seconds) who wrote what and where. It was done, as it should be in PHP / MySQL - a table, in it we select the last (relatively speaking: WHERE id> MAX (id) -10 ORDER BY id DESC) records. Periodically delete everything except the last ten. It worked great. So far, TechCrunch has not written about the site. The server died because of this part. As soon as I turned it off (remember about 3%?) - all the tens of thousands of people that they sent me - they began to see the site very well. I solved it clumsily - made memcache. PHP fetched the last 10 entries and wrote in memcache, and upon request, returned a line from memcache. However, this decision as a result began to die.
I wrote a simple Python http server (from the standard library) that had an array of 10 lines and gave it an AJAX request or added a new one and deleted the first (FIFO).
That is, PHP (on the server) processed the forms and submitted via file_get_contents ('http://mysite.com:9000/?put='.urlencode ($ _GET [msg])); And AJAX (in the sense of a client in JavaScript) was running on mysite.com : 9000 /? get - bypassing all Apaches, PHP interpreter, MySQL and even memcached (which still needs to be connected - and this time is an overhead).
Writing such an application in Python or C ++ with a good HTTP server library is no difference. C ++ already has quite a lot of tools for high-level programming. Even the average PHP programmer is able to write safe and serious applications in modern C ++.
But, and here it’s already interesting: a running Python HTTPd script eats up to 10-20Mb of RAM, and a running C ++ server with the recently published WebToolKit only 0.6-1.4Mb (we are talking about RES memory). So such small optimizations on the server can run the sea (unlike Python, which in pieces of 20Mb on a small VPS quickly eats up all the memory).
Similarly, make, for example, a real-time display of users on the site - who-on what page, from where.
As a result, the site is still in PHP, it is accelerated by dozens of times (due to optimization of the hardest piece to execute), the database is offloaded, no dog pile caching effects, etc., and in time, it took about 2-3 hours this optimization.
Another example is voting. Let's say that you need to add a voice for someone through AJAX and save the user's IP so that he no longer votes. In C ++, it will take in memory well, maybe byte 20, so you can safely store all the data about all users and votes over the past week with instant access to them. At the same time, if this is done in the usual way (PHP / Python + MySQL / SQLite / Postgres ...), the database will grow and access to it will slow down and if calls go on constantly, this can significantly slow down the server. I am not saying here that SQL will lose once in 10 memory, but I have had such cases (in particular, SQLite: memory: loses to file SQLite tens or even hundreds of times).
Another example is the user's GEO location. You can load information about all IPs into memory (of course in the form of range) and C ++ will go through one cycle in nanoseconds, at the same time, PHP + MySQL will dig for a long time choosing the desired range.
Also: a counter for something, let's say the number of votes for a post (or karma) - you really can keep the karma of all users in memory and let AJAX directly access the server, at the same time PHP / MySQL, and what Django will do: check it has changed whether the file is (PHP), we will do a regex check where we are going (django routes), if there is no optimizer in PHP - read the file again, parse it, initialize the connection to the database, request one digit, set the lock so that no one changes it yet, increase it, return back, remove the lock.
And this is if there is no ORM, and yet Django still creates objects in order to process the request and return it. This is all, of course, a problem, not a catastrophe, but then again - if you spend 99% of your time on useless things - isn’t it better to get rid of them in an hour?
At the same time, for C ++ it is almost a pair of processor clock cycles (+1 to Integer in memory and translate into a string). Simply put, while PHP + MySQL processes one such request, C ++ can do a thousand, maybe a million (if you do not take into account the time to process the HTTP protocol itself). Yes, and such a C ++ program will in fact be shorter (but hardly much longer) than PHP + MySQL.
In general, if there are tasks of the level of “add something to something”, “find something among a small set of something”, then pieces of C ++ sites can help you a lot. But the general idea is still better to do something higher level (such as PHP / Python) or look at languages such as Arc.
The disadvantages of this approach?
Well, more troubles.
1. Monitoring, for example. As an option - the simplest, let's say there is compiled_file.cpp, which compiles to compiled_file (without an extension). We go into crontab (crontab -e) and append:
* * * * * / bin / pgrep compiled_file || / path / to / compiled_file
The script will lie for a maximum of a minute if something goes wrong, then it will restart. Really - less than half a minute.
2. Update the working process. Yes, just by replacing the file, as PHP will not do, you have to kill the process (killall compiled_file) and restart it.
So do not immediately discard the idea of sites in C ++, but you don’t need to suffer fanaticism either. You can write a whole site in C ++, but if you have half a person a day, then why?
Just using this language should be justified. If your server is 100% loaded and you know that 90% of requests go to one place, which could be very simply expressed as a small server with a memory print of 1 MB and which would not require databases, interpreters, etc. - then why buy more expensive equipment, instead of replacing this piece with a compiled one?
And if C ++ is really scary - then look at what Cython is for Python.
Yoi Haji,
view from Habr