6 ms·
Suppose I wanted to write a C++ based web application...
What should I use? Ideally I would either have a Python or Ruby application with all its bottlenecks coded as external libraries in C. But what if I just wanted to write plain C++? How can I run it without having to resort to CGI or running it on top of a webserver library (eg. the app is the server)? Are there any event-driven servers that allow for easy plugging in of C++ applications?
- fh973 18y agoYes, there is exactly what you are looking for: Yield. http://yield.sf.net/ http://yield.sf.net/ It is an event-driven, staged (threads communicating via message passing) application server, written in C++. It has stages for HTTP server and client, disk IO, gzip, logging (basically all CPU-intensive or IO tasks). Messages between stages are defined in IDL. You can send those over the network via Sun-RPC or JSON-RPC. Not directly related to your request, but still handy: it also has a stage that wraps the Python VM, so you can write parts of your app in Python.
- benkant 18y agoYou could write an Apache module. Or use FastCGI. Or libasync: http://pdos.csail.mit.edu/6.824-2004/async/6.html http://pdos.csail.mit.edu/6.824-2004/async/6.html Let me know what you end up doing.
- ra 18y ago+1 for an Apache module. Build on a mature best-of-breed product that provides a quality programming model and all required HTTP and logging services. Also easily take advantage of other apache mods such as memcached or wackamole. Good luck.
- wheels 18y agohttp://www.webtoolkit.eu/wt http://www.webtoolkit.eu/wt That said, even as a hard-core C++ guy, I can't see why you'd really want to write a web application in C++. If you want C++-like syntax, you'd be better off with Java for web stuff.
- alecco 18y ago(Can't help it) And start saving money for memory and large amount of servers...
- quasimojo 18y agothe time you save is definitely worth $3k
- jgalvez 18y agoHmm, I'm not sure if $3k is all you'll ever need for Java hardware. If the application is big enough you're likely to have keep on spending on vertical scaling. But then again, that's what big enterprises like to do: spend money on big boxes.
- jgalvez 18y agoWhich leads me to point that out that Java is far from suitable for a start-up. I'm beginning to think that Python plus C/C++ might be the golden bullet for startups. Ruby's C integration is easier than Python, but there's something to be said about the overall language model and performance. I tend to have a main RoR app for the website and a few Python daemons in the background for the really heavy stuff, and now it's finally the time to think about offloading some of that load even further closer to the machine, in C/C++. I'd really love to come up with a standardized way of deploying small, context-specific RESTful API servers in either C or C++. I'm starting to think that a possible C++/Python bridge via Thrift is likely the best option. But then again, if I just managed to keep it all in a C++ application without needing to run the Python interpreter, it could totally rock (for an application that is supposed to serve 100k+ users on cheap hardware). It's cool to see cloud computing going mainstream and now applications written in any language can be reasonably fast with the help of EC2, a load balancer and memcached. But I still think there's great value to coming up with truly lightweight solutions that really use nearly all computing power a single node can possibly offer.
- 18y ago
- bbb 18y agoWhat is wrong with "the app being the server?" There are plenty of C/C++ HTTP libraries and frameworks out there. Just proxy your homebrew app server behind squid, lighttpd, apache, nginx, or $YOUR_FAVORITE_PROXY and you are set. In fact, it makes it really easy to develop and test your app that way. There is no web server to configure and you can easily stick in printfs all you want. I use CherryPy (standalone HTTP framework for Python) that way exactly for these reasons. I'd do the same with C++.
- whalesalad 18y agoTalk to the boys at 37Signals ;) Quite a bit of Basecamp is C++, not Rails.
- ROFISH 18y agoI've always wondered what the speed of Rails would be if ActiveRecord and friends was in C (or ASM for the insane).
- jhancock 18y agoWonder no more ;). If you want to see how much better an ORM in ruby/C can be, look at datamapper http://datamapper.org http://datamapper.org. This is mainly used with merb. The new datamapper uses data_objects which has been completely rewritten with C underpinnings to be much more efficient than previous datamapper. I haven't seen any recent benchmarks against ActiveRecord. But I do believe the datamapper team met their goals of being faster.
- dkubb 18y agoDataMapper is about 60% faster overall than ActiveRecord, and as much as 20x faster in a few key benchmarks at the moment: http://is.gd/3xwR http://is.gd/3xwR In fact we, the core DM contributors, have spent very little time optimizing the library. Up until now we've been mostly focused on creating a clean API, and keeping the internals simplified. I would expect the gap to widen much further once the API stabilizes, and the spec coverage gets nearer 100% (it was in the low 90's last I checked).
- ntoshev 18y agoDo you attribute the performance to the implementation language or the different design pattern / approach to ORM?
- jgalvez 18y agoI can't seem to find any mention of it in their blog. Any links?
- quasimojo 18y agowhy do you want to do this??? there are plenty of c/c++ web apps written out there, i have written some myself. its pain central. you do it only when you cannot spare cycles or memory per session and cut the safety nets. just buy another server. in man-hours you will be out ahead.
- jhancock 18y agoThere are two approaches: 1 - build your app as an extension of an existing http server. Start with lighttpd or nginx source and build out from there. nginx has a nice module structure. Perhaps your app can be just a new module. Both of these code bases are C and use event driven sockets. You might be able write your extensions in C++. 2 - Use a known solid http C++ library to roll your own server. Depending on the complexity of your app, you may end up spending a lot more energy going this route. My relay server for http://shellshadow.com http://shellshadow.com is C++. I had my C++ programmer look at the structure and methods used in one of the two http servers named above. We ended up not re-using their source (our relay isn't http protocol) but did get some useful insight for developing our server daemon.
- WALoeIII 18y agoPremature Optimization FTL.
- wayne 18y agoIt's not totally crazy. I think Meebo's mostly written in C++, for instance, since they're always looking for C++ and JavaScript people but they never mention scripting in their job ads: http://www.meebo.com/jobs/openings/server/ http://www.meebo.com/jobs/openings/server/ I think they mention in one of these blog posts that they wrote a lighttpd module. I've written a C++ Apache module for a simple web app before. It's definitely harder than using a scripting framework though, not so much because of the language, but more that frameworks have functions that help you do just about anything (e.g. sanitize input, construct safe SQL). In C++ land you're more on your own since most people aren't crazy enough to build a web app in C++. Also, if you go down this route and you're particular about C vs. C++, check out mod_cpp for building Apache modules: http://sourceforge.net/projects/modcpp/ http://sourceforge.net/projects/modcpp/ I echo the advice that if you have something performance-sensitive, build it in C/C++, but proxy it through something built with a better-known framework. Thrift is great for this: http://incubator.apache.org/thrift/ http://incubator.apache.org/thrift/
- jgalvez 18y agoThanks a lot, Thrift looks very cool. That's exactly the kind of thing I would see myself doing a lot. Offload stuff out of Python altogether and communicate with a C++ daemon. I heard about Thrift when Protocol Buffers came along but didn't bother to look into it. Is it any better than PB? Does anyone have any experience on this to share?
- wayne 18y agoHere's the big Thrift/PB comparison table: http://stuartsierra.com/2008/07/10/thrift-vs-protocol-buffers http://stuartsierra.com/2008/07/10/thrift-vs-protocol-buffer... Personally, I like Thrift better. More languages and greater community participation.
- wheels 18y agoMeebo's in Java. Or at least so said the founder when on a recent TechCrunch meetup panel. Specifically, he was making cracks at Ian (from Songkick) for Songkick being in Ruby. ;-) http://uk.techcrunch.com/2008/09/21/techcrunchtalk-video-startup-young-guns-and-old-hands/ http://uk.techcrunch.com/2008/09/21/techcrunchtalk-video-sta...
- known 18y agoYou may want to check http://www.kegel.com/c10k.html http://www.kegel.com/c10k.html
- dburger 18y agoPlease lay down for a while and it will pass.
- jgalvez 18y agoI don't mean to go about writing my web applications in C++. I do plan to write at least a couple for my job that are really performance sensitive. I'd like to use this opportunity to learn and educate myself as I believe a good web software engineer needs to have this kind of knowledge. What if I ever need to hack some code in or out of my web server (nginx, for example)? What if I need to rewrite some really slow code in a C++-based background daemon that communicates with my main application through protocol buffers or something? That's kind of thing I'm thinking about.
- mark_h 18y agoI haven't used this personally, but I remember reading about the web server behind okcupid.com: http://www.okws.org/ http://www.okws.org/ It uses c++: http://www.okws.org/doku.php?id=okws:tutorial http://www.okws.org/doku.php?id=okws:tutorial
- froo 18y agoThanks for linking it - that was the first thing I was thinking of aswell when I read the guys post.
- IsaacSchlueter 18y agoThat's exactly what I was going to suggest. If you're doing something computationally intensive (like, a dating or quiz site, for example, with the impressive statistical analysis that OkCupid and HelloQuizzy deliver up very quickly), then it would pay to look at an approach that's working well right now.
- codesink 18y agoDisclosure: I'm one of KLone developers. Use KLone http://koanlogic.com/klone/ http://koanlogic.com/klone/ . It lets you write web pages with embedded C/C++ code (php-style escaping). It's VERY fast and provides all common functions/features you need to parse incoming HTTP variables, sessions, file uploads, etc. If you need more features you can link it against any C/C++ library and call library functions from within web pages.
- RobGR 18y agoI have taken extensive PHP applications and re-coded them as a C/C++ module to the PHP language. It worked out successfully. A simple loop in PHP that just does some math and prints our a value, can be 1,000 times faster in C; if a PHP object is created and deleted in the loop, and you do the same in C++, you can get 100,000 times faster. Your speedup will vary according to what it does, of course -- if it is mostly waiting for a database, you won't see much speedup at all. Contact me if you are looking for someone to do that kind of work. If I were starting a new web app from scratch today, I would code it in C using Thomas Boutelle's cgic library. Of course, it seems one rarely starts anything from scratch these days.
- jgalvez 18y agoWell, we're constantly looking for talented folks at Côdeazur Brasil (http://codeazur.com.br http://codeazur.com.br), even for remote work. We actually have an open position for a Rails senior developer at the time but we haven't managed to find anyone yet, or haven't looked hard enough. Please contact me at jonas@codeazur.com.br with IM info, I'd love to introduce you to some of the stuff we want to do (so maybe you can help).
- jamongkad 18y agoYou say you're from Brazil? How bout giving Lua a chance. If speed is your concern Lua might be your ticket. You can also check out the Kepler Project as well.
- alecco 18y agoI've been thinking about this too. The main problem would be making it easy to extend. The traditional HTTP side of it is trivial (just use nginx or something like that.) For template engine you have already Clearsilver and it's the fastest that I know of by an order of magnitude. The key is solving how the application's developers could extend the web application. You can't expect them to code in C++ and compile every little change to their site. That's the beauty of CMF systems like Zope, you can put plain Python code in there with pages and components. Doing a CMF in any lower level language and having to switch to a higher level language VM for every snippet would kill your original performance savings. Find an element to fix that and you'll have a strong new contender in the CMF arena. Stuff like OpenMP and all that comes bundled in the new C++ libraries, transparently. (BTW IMHO don't even think of starting without memorizing the Essential C++ books and all it's need-to-know patterns.)
- alecco 18y agoOh, and with decent VPS hosting at 30 bucks a month the need to have a high performance CMF is diminished.
- ii 18y agoTry to rewrite something simple written in Python in C++ first and measure the differences in speed/dev time first. Chances are you may end up with a story like this: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a63979a6f18bf37/b926deb737ab71c6 http://groups.google.com/group/comp.lang.python/browse_threa... Albeit micro-benchmarks may indicate that Python is 100-200 times slower than C++, they may not be applicable to the real world. Python can be very efficient. And when combined with libraries like NumPy, beating it's performance with hand-crafted C++ is difficult. Emperor has no clothes.
- hugh 18y agoInterestingly, I tried rewriting one of my C++ codes in Python one day, using numpy. It was mostly manipulation of largish (~1000 x 1000) matrices. On the upside, it was 100 lines instead of several thousand. On the downside, it was several hundred times slower. I didn't bother trying to write serious simulation codes in Python after that.
- riobard 18y agoevery tried NumPy? guess that's what you need for the matrices stuff ...
- ii 18y agoOf course C++ will beat Python in speed if you invest enough time in it. And assembly will beat C/C++ if you invest even more time. My point is that some people assume that if they would just sit and write something in C++ without putting enough time and effort and skill in it, it would be somehow magically faster and better than high-level language, just because it's C++. They are mistaken. Python standard library is extremely fast. For example 'list.sort()' in Pytohn could run 3 times faster than C++ 'sort(vector<string>)'. Here are few examples when simple Python code runs faster than the same code in C++: http://groups.google.com/group/comp.lang.python/browse_thread/thread/70f9bd389473207b/74807f553ae12b8c http://groups.google.com/group/comp.lang.python/browse_threa...
- salaam 18y ago
- 1gor 18y agoC++ will drown you in complexity. There are many ways to look at complexity as an abstract concept. You could look at code length. Or you can count how many 'degrees of freedom' your code allows you to have, and be sure that you need to control each of those. C++ affords too much freedom for developer. The trend meanwhile is towards reducing complexity at system/core level and focusing on application unique added value. Today, in large web applications, complex behavior is build using simple components. Those components are normally done in C, and then wrapped by some scripting language. C is not too flexible. The components are rock-solid as a result, they lack unexpected behaviour. Their code's 'degrees of freedom' are easier to control. On top of that, there is trend towards frameworks in scripting languages which reduces 'degrees of freedom' at core/foundation level even further. Finally, there is this REST thing that wants to standardize API of a typical web app, offering us even larger blocks to code to work with. Again, all these trends have one objective - allowing to focus on unique application functionality, on interaction with other systems etc and hiding underlying complexity from the developer. By using C++ you are likely to get complex behaviour in your code very early, at a very fundamental level. As you add functionality, the complexity/behaviour quirks will grow exponentially and you will fail. I am convinced that Microsoft failure with Vista is mostly due to them using C++. They were simply defeated by mounting complexity, not by lack of engineering talent. Meanwhile, Unix success (Apple OS X included) is determined by relative 'dumbness' of underlying C language, Unix tradition of small single-purpose tools etc.
- jgalvez 18y agoInteresting, thanks for sharing. So do you think I should avoid C++ altogether and stick to C? What about Google? Google uses C++ a lot and they seem pretty fine/stable to me? Maybe C++ can be good web architectural stuff (not as complex as an operating system)?
- quasimojo 18y agotreat the c++ standard as an assembly language of sort, from which higher level langs are built, like boost. if you look at it this way, you can carve out a sane section and deal with it. if you try to hold the entirety of it in your head and code, its hell
- drhowarddrfine 18y agoI do everything in C. I have 3 e-commerce customers and their sites are written completely in C. I have no issues whatsoever. One reason I started doing it this way is because I was concerned with speed. Having done so in C, they are blazingly fast.
- gord 18y agoId have to tip my hat to this... Ive written a lot of C++, it was 'the one true way'.. but then I did see alot of people using python, ruby.. I learned a bit of lisp, and kind of de-conditioned myself. If your not gonna use Lisp/arc/scheme/python/ruby... then, actually, I would go with C and not C++. You are just above the metal and you see exactly whats going on, and you dont spend time making cleverly convoluted constructs of infinite generality... In C, you just get shit done. With a bit of discipline you can get a lot of the benefits of OO and even some of the benefits of FP in your C code. Heresy, I know. I do love C++ still, its nice to see good RNG and regex APIs going into the language.. but its a bit verbose on balance. gord.
- wdu 18y agoWt (http://www.webtoolkit.eu/ http://www.webtoolkit.eu/) makes developing C++ web applications dead easy. It is similar to Qt: you work with object oriented widgets and events are delivered through a signal/slot mechanism. You get Ajax, javascript, cross-browser portability and all the nifty stuff for free.
- known 18y agoYou may want to Google ISAPI, NSAPI
- mdakin 18y agoI have done something like this. I interfaced with Apache however rather than also writing the web server by hand. I also leveraged libraries when I could. You may want to investigate the following libraries: fcgi++: Interface with the web server using fast cgi; keep the program resident in memory between requests. botan: For generating cryptographic hashes. cgicc: For handling input from the web browser. xerces-c: For implementing a templating system and generating the output which gets sent to the web browser. libpqxx: Interfacing with Postgres. cppunit: Unit-testing critical or difficult-to-test bits. It's not as hard as everyone likes to make it sound (assuming you are on the right side of the C++ learning curve). The real question is whether you want to spend your time writing a framework rather than actual applications.
- pcsearcewg 18y agoBack in the late 90s, before .NET was released, I used to know a number of groups that would use C++ to create "ASP Generators", since ASP was such a beast. A OO code generator is much better than scripted spaghetti code. Now, the concept is moot...a good OO web platform is so much more robust.
- davo11 18y agoIf you're running windows have a look at Delphi / C++ builder. There are a number of ways to do this, intraweb components are one, or you can roll your own. You can write your events in C++ or Delphi or both. (flame on)
- tlrobinson 18y agoAll I have to say is: make sure you know what a buffer overflow is.
- jgalvez 18y agohttp://google-styleguide.googlecode.com/svn/trunk/cppguide.xml http://google-styleguide.googlecode.com/svn/trunk/cppguide.x... This seems like a good guide on how not to fuck things up with C++.
- salaam 18y agoAgain I see nobody is mentioning http://www.tntnet.org/ http://www.tntnet.org/
- scumola 18y agolibmicrohttpd