24 ms·
Hard-won lessons: Five years with Node.js
- garysieling 10y agoThe biggest hard lessons I've had with Node.js have been around handling errors - when I first deployed https://www.findlectures.com https://www.findlectures.com, I was shocked to realize that uncaught errors could take down the whole site. Not handling error callbacks is also a big problem - TypeScript has been an awesome solution though, because it can show compilation errors if you screw up function arguments.
- always_good 10y agoThat's not Node.js. That's Express or a similar abstraction. You can execute everything inside a promise like with Koa, which amounts to something like: http.createServer((req, res) => { handle(req) .then((response) => res.send(response)) .catch(() => res.send(500)) }) Where `handle` is an async function and thus all your application logic is in async/await space.
- turdnagel 10y agoBut you're still error handling with `.catch()`. Without that, you'd have an uncaught Promise rejection, which could take down your Node process.
- always_good 10y agoIt's common for people to complain about something like a syntax error taking down their whole process, so that's how I read it -- that you're trying to do "too much" catching.
- Waterluvian 10y agoI'm not an expert with deploying application servers but shouldn't part of handling lifecycle include the ability for a service to crash and be respawned? Ie. Be okay if a syntax error crashes your server.
- smokeyj 10y agoYes you should be using a process manager like PM2.
- garysieling 10y agoIf you're comparing to J2EE, ASP.NET, Apache, etc, application servers tend to be big monoliths with lots of features, but Node is much lighter / more modular, so you end up setting that up yourself (that also surprised me)
- phamilton 10y agoIn a "one request per process" model, sure. But if you are handling 1000s of concurrent connections, a syntax error crashing your server means all of those connections error.
- garysieling 10y agoYeah, I'm happy to admit that I figured out how Node/Express work entirely by doing things wrong, and then fixing my mistakes :) I came to this after building C#, Scala, and Java webapps, so it surprised me that this these class of coding errors could take down a site (both screwing up the try/catch or not handling promises). There is probably some room for Node/the language/Express to make the experience better, either way it's something you'd need to know if you're new to the platform :)
- stickfigure 10y agoIt's Node.js - the root of the problem is also Node's strength, which is that there's effectively no stack. You can manage zillions of concurrent connections, but there's no easy way for the system to unwind bad behavior by one of those connections. In a Java app you except all the way to the request response. In a Go app you return all the way to the request response. In a Node app, you have to call your way back to the request response. Or you don't, potentially leaving the whole process in an inconsistent state. The amazing concurrency of node comes at a price.
- jcheng 10y agoDomains were Node's answer to that, and it seemed to work pretty well. I adopted the approach for my own web framework (not JS based) and it has been an extremely useful abstraction. They're soft-deprecated in Node now though, and AFAICT work on a replacement has stalled. https://nodejs.org/api/domain.html https://nodejs.org/api/domain.html
- int_19h 10y agoAnd in async C# - which has the same "amazing concurrency" - it just works. Because the language will take that exception, and wrap it into a faulted task (promise) for you. So the stack may be spaghetti, but exceptions navigate it correctly same as everything else.
- alleycat 10y agoThat's why you use a production manager like pm2
- AlexeyBrin 10y agopm2 will restart the app on crash if I understood correctly how if works.
- thibaut_barrere 10y agoSomething I found surprising was the 1.5 GB limit per process when used on Heroku (https://devcenter.heroku.com/articles/node-concurrency https://devcenter.heroku.com/articles/node-concurrency), which afaik can be circumvented elsewhere (https://futurestud.io/tutorials/node-js-increase-the-memory-limit-for-your-process https://futurestud.io/tutorials/node-js-increase-the-memory-...). This can be problematic at times if you need to open a very large number of concurrent connections (crawlers, slack connections etc), and can force you to organize a "cluster" of processes before you really want it.
- jononor 10y agoYou can have quite a number of concurrent connections in 1GB of RAM, 10k at least? That seems allright for a single node, and scaling horizontally after that. For something like crawling, which has a lot of time spent on CPU intensive parsing and I with unpredictable times (hitting external service) I would throw that in a background worker from the start.
- thibaut_barrere 10y agoI do not have the numbers handy, but it was below 10k (by quite a margin), unfortunately.
- didgeoridoo 10y agoThese read almost like Zen koans. Such good stuff.
- savanaly 10y agoI agree. I wouldn't quite call it koan-level but the breezy style is definitely attractive to me.
- Daishiman 10y agoReading up on all these Node.JS posts I'm surprised at just how many gotchas the platform has and how there's no single standardized way to solve them. It's not encouraging stuff. On paper the platform looks decent but aside from a few use cases it seems that there's more hype than it merits.
- jimmywanger 10y agoYeah. I've had production experience with node. It's been the single least productive coding environment of my life. When you're taking care of large systems of backend servers, Javascript is pretty far down on the list of languages I'd pick to use. Add to that Node's explicit handling of asynchronous operations instead of just blocking and waiting. Also, it seems to get worse the more code you have. I'm sure it's possible to write really good readable javascript in large codebases, and the same for Node, I just haven't seen it done. Another subject the author brings up is "the ecosystem". Javascript has so many libraries that keeping up with them, their updates, and using them in a canonical way throughout a large codebase is a full-time job for at least one engineer.
- cnp 10y ago> Another subject the author brings up is "the ecosystem". Javascript has so many libraries that keeping up with them, their updates, and using them in a canonical way throughout a large codebase is a full-time job for at least one engineer. I love the language, but this is absolutely true.
- erikpukinskis 10y agoSame, and same. It's very research-y. It is an incredible place to work if you treat your development process like research anyway, but if you just want to jam through production cycles I would pick Python or something stable.
- Daishiman 10y agoSo, not development?
- wcr3 10y agoso... many... links...
- lasfter 10y agoI can't imagine choosing to write Javascript on the server, but considering its popularity I'm wondering if I'm wrong. So I'm curious as to the reasons people chose Node.js and whether you would recommend it, anybody willing to share their experiences?
- Scarbutt 10y agoSo I'm curious as to the reasons people chose Node.js Javascript of course ;) You can leverage the same language for server side stuff and browser programming.
- lasfter 10y agoBut how valuable is that? I hear this a lot but is it really that much of an inconvenience to write backend code in a different language? Especially if there are different engineers working on the frontend and backend.
- jdhawk 10y agoLanguage is never the problem. Languages (mostly) are easy. Frameworks are hard. Tooling can be hard (pick a language). Javascript is easy. Express, NPM, Bower, <insert framework of the month> are tough.
- chrisco255 10y agoIt's very valuable if you are bouncing back and forth between frontend and backend. There's no context switching as you do that. The cognitive overhead of context switching is higher than people generally accept. This is why stacks like the MEAN/MERN stack get used in hackathons. You've got a JSON-inspired database (Mongo) with a minimal server-side framework (Express) feeding a React/Angular front-end. You can use the same libraries client/server side like Mocha for unit testing, Lodash for collection manipulations, you can even share the same validation files for server & client.
- mgkimsal 10y ago
- flukus 10y agoI would have like more detail on what they were doing with node. Building lots of small apps or one big one? Green fields or brown?
- garysieling 10y agoI've only worked on a large side project, but from that experience I have a theory that it is one of the triggers for microservices as a pattern, because the pain of debugging a Javascript server application seems to scale a lot more than linearly.
- scottnonnenberg 10y agoFeel free to reach out. :0)
- jack9 10y ago> Verify All Assumptions When you have to worry about the environment unexpectedly reusing internal variables, when is it considered foolish to use a framework where you have to question every relationship between every concept? Just stop using it.
- thesmallestcat 10y agoOr one could learn how `this` works, which is that example is really talking about. I was unimpressed by these lessons. There are no dark corners of Node, or JS in general, here. It's more of a narration of the author's growth as an engineer. The "call the callback last" recommendation is particularly odd.
- ninkendo 10y agoThe counterintuitive behavior of "this" in JavaScript doesn't need any defending. I've never heard a justification for this or any other of JavaScript's quirks that wasn't just a thinly veiled lecture on how it works.
- savanaly 10y agoYeah...I think we have to use some context clues in interpreting this particular piece of advice. Clearly, he's not suggesting you verify all assumptions as it would take millenia to verify literally all the assumptions that one makes when writing a complicated program. Personally, I interpret it as meaning he believes that the average developer's baseline calibration for how much is to be assumed is too trusting and people should verify more assumptions than they do-- starting with the most dubious ones of course.
- crimsonalucard 10y agoToo many lessons learned and arbitrary rules to follow equals flaws with design/language design
- Safety1stClyde 10y agoWhat language does not have these kinds of problems? I have encountered them with every programming language.
- iopq 10y agoFor example, Rust distinguishes between PartialEq and Eq https://is.gd/kRvoMm https://is.gd/kRvoMm you can't derive Eq (transitive equality) on f64 https://is.gd/FjTcA5 https://is.gd/FjTcA5 but you can derive PartialEq (intransitive equality)
- Safety1stClyde 10y agoUnfortunately I do not yet know very much about Rust, but I wasn't referring to the specifics, but to the point that every programming language seems to have quirks and hidden rules.
- iopq 10y agoSo just use a programming language with less annoying quirks and hidden rules. Especially when those quirks are issues at compile time, not issues at 3 AM in production. "And I found a whole lot of references to req.randomThing and even some req.randomFunction() calls. I then proceeded to search back through every single middleware function which had run before, to figure out what exactly was going on." In a statically typed languages names are bound at compile time, so you can actually jump to the definition of each name using tooling. A JS tool can only make an educated guess.
- Safety1stClyde 10y ago"I wasn't referring to the specifics, but to the point that every programming language seems to have quirks and hidden rules."
- ambicapter 10y agoWow, working with Node sounds like a huge pain in the ass.
- senorjazz 10y agoThis is always my take away. Quoting from an above post >>Ie. Be okay if a syntax error crashes your server. Really? I need to set something up to stop a syntax error not crashing the server These rapid prototype arguments don't hold either imo. I can knock out quick and dirty java / c# apps plenty quick enough that work fine for their needs. In my forays into dynamic languages there is no rapid development due to all the errors, the lack of IDE capability, the compiler replacing tests I have to write
- linkmotif 10y agoSuper interesting the part about converting to SSR and React synchronicity. Thank you!
- ausjke 10y agospent 2+ years learning js/nodejs etc I finally turned to PHP for server side, it just seems easier for me to get the job done with php, or it's just me.
- goatlover 10y agoNot a big fan of PHP, but it has certainly improved it's performance. Wouldn't be surprised if PHP 7.1 isn't competitive with Node.
- frik 10y ago> Re: "New Relic doesn’t get it" Can someone recommend a Nodejs performance monitoring solution that works great?
- cdbattags 10y agoThe sheer number of responses/comments here (hell, even the article itself) justify Node by community alone in my opinion. I like the fact that I've been able to follow along as it's matured and this gradual bit by bit learning process has made me a better developer because of it.
- sandGorgon 10y agoI have a hypothesis - I think elixir,golang and java+vertx are carefully designed, highly productive and high performance frameworks. However with Reactjs + React Native, you have an unavoidable component of your company that is in javascript (or will have soon). Why would you not do JS based server runtime ? With typescript/flow, you actually have an excellent language with no compile steps. Plus, and probably the most important, you have a cross pollination of engineering talent and time . I think its the same case with data science - it is much more productive to build a python based data science startup, because of flask+sqlalchemy+jupyter+pyspark+numpy+tensorflow+keras. Yes, you can use scala or java... but you lose that one-language-to-rule-them-all productivity multiplier. From a quick prototyping point-of-view, i think golang would have theoretically killed nodejs.
- joaodlf 10y agoI can't help but feel like this reads as Stockholm syndrome. Everything about Node is hard, but I'll keep on struggling.
- dolzenko 10y agoAuthor calls it 'emotional' :)
- rusk 10y agoI have had occasion to work on a Node.js based app in the last while. Well actually mostly a React/Redux-based app served out of Node.js, so 90% client-side but ... I think it's great! For quickly hammering something together and getting something bootstrapped and up and running it is so easy and straightforward. But .. I have this niggling feeling .. as my code develops I'm noticing the expressiveness of Javascript lets me do things that look nice, and impressive but that I know will come back to bite me when it comes to enhancement and maintenance. In this respect Javascript kind of feels like perl with OO and functional semantics built in from the ground up. It really is a lovely language but I wonder how well a JS-based system will scale over time. I've heard people say "never use ruby in production" and I wonder does the same reasoning apply here. The argument goes that building apps that use the same language client-side/server-side carries the virtue that you can write "pure" apps - i.e. that share code front-end and back-end. That is definitely something that makes sense, especially if you need shared libraries you won't need to write the same code twice for each end. But beyond that, I feel the expertise required at both ends is quite different. As a systems programmer I feel that strongly-typed rigid languages are very much the way to go because they give you a better sense of how robust the code is. Typically on the back-end you want to get something to work once, and leave it, so you need to be sure it's right. My more limited experience working client-side suggests that you need far more flexibility there. Issues are far more easy to spot front-end and the key virtue is to be able to make changes and enhancements quite quickly and you don't have the same stringent code-confidence needs as you do on the backend because the quality requirements are different. Niggles and gotchas can be spotted, diagnosed and worked around on the front-end but on the back-end they become a royal pain. So this, is what I think Javascript is great for on the front-end. I do love the simplicity of bootsrapping Node.js on the backend and the actual reactive framework for building apps is really cool. But yeah, for systems code "at large" I see limitations there for sure: The same we had with Perl, Ruby and Python. Perhaps what we need is some alternative language support e.g. Haskell or something that gives the same code-confidence, but which can interoperate perhaps with the "pure" javascript libraries similar to how say Scheme and Java can interoperate.
- z3t4 10y agoThe article mentions synchronous performance, but there are no mentions of optimizations. You can do a lot of optimizations in JavaScript and the VM itself is already well optimized. If your naive implementation is slow, my experience says you can speed it up 100x. Then it's not really worth switching to C/C++ assembly for another 10x performance. Then it's better to take the step to pure hardware.
- z3t4 10y agoRegarding throw and NaN, when the program throws an error, make sure it's restarted! When JavaScript throws, it stops doing whatever it was doing. But the rest of the program will continue to run like if nothing happened. NodeJS does the right thing by "crashing" / exit. When you asume something in JavaScript, for example that the second parameter in the function is a callback function make sure it is! if(typeof callback != "function") throw new Error("Expected callback=" + callback + " to be a callback function!"); if(isNaN(someNumber)) throw new Error("someNumber=" + someNumber + " is NaN! someState=" + someState); if(something == undefined) throw new Error("something=" + something + " is undefined!");
- kuon 10y agoUse elixir on the server side and elm on the client side, done.
- josteink 10y agoThis looks like how I used to work with BASIC back in the 80s: > I finally added extremely verbose logging ... started adding logging ... Verbose logging to the rescue ... I had the right logging in place ... My first step was to jump in and add some key logging statements ... added extremely verbose logging Weird to see people happy to be limited to such stone-age work-flows when fully capable debuggers have existed for almost all proper languages out there the last 30 years. Based on this, it's hard not to state that current Node-developer are the new PHP-developers: Unsophisticated, completely unable to see when the tools at hand are lacking, and happy with a with whatever they can get running.
- nilliams 10y agoIn some cases here, he's talking about fixing bugs in a production. It's not so simple to attach a debugger to a live application serving 100s of requests. Break and you just 500'd all users. In others granted he's working locally and probably could have found the problems without logging, but that just comes down to preference IMO. Node does have a debugger though and recently added official (though experimental) support for attaching the Chrome DevTools debugger [1]. Doing so was doable via 3rd party packages previously. Edit: updated as author wasn't only talking about in-production. [1] https://nodejs.org/api/debugger.html#debugger_v8_inspector_integration_for_node_js https://nodejs.org/api/debugger.html#debugger_v8_inspector_i...
- ovi256 10y agoThis is a case of developper ignorance, not missing tools: v8 has an integrated debugger since 2009, and you can interact with it using the Chrome developer tools, probably the most used debugger frontend of all. There are also debuggers for PHP :) No horse in this race, I write Python.
- scient 10y agoYeah this is pure ignorance. There is a time and place for advanced profiling/debugging tools, and there are times where using simple logging to debug things quickly is perfectly fine. You applying debuggers as a hammer to all the problems and all the people is the thing thats weird imho.
- 10y ago
- Kiro 10y agoIsn't the whole section about Classes just due to the cache mechanism when you use require? I.e. all modules are singletons automatically. Is it really CoffeeScript causing it?
- traf68 10y agoSorry to say. Node.js isn't the problem. It is a symptom. Devs these days are craptastic coming from dev->systems with devops nonsense. Just stop forcing this terrible language into server and js is as fine as it will ever be client side.
- jkchu 10y agoI just want to thank the author for a very well-written piece. I found it incredibly interesting and really connected with the format/flow of the post. I would love to see similar posts for various frameworks and languages.
- ralphc 10y agoI'm seeing a lot of Node hate here, especially for large projects in Enterprises, but I've been watching a lot of Node Interactive talks, and companies such as Walmart, Netflix, GoDaddy, Paypal are switching. so why are they happy with it?