Live data from Hacker News

Ask HN: Event loop vs. Threads

news.ycombinator.com

1–10 of 36 posts

Ask HN: Event loop vs. Threads

#1
Hello, At my work place we have a lot of legacy C++ code that uses threads to parallelize things. We are considering to switch to Node.js and searching for sources comparing the Event loop model vs. a threads based one from perspectives such as the following: efficiency, ease of maintenance, etc. Would be thankful for any references to good constructive material.

EDIT: more details about the application - it is a trading app that communicates with multiple sources at high rates to gather information and send commands, but it also does quite alot of number crunching.

Re: Ask HN: Event loop vs. Threads

#3
It depends on many variables, including but not limited to anticipated workload, the nature of the hardware, and parallelism of the underlying workload. It would help if you gave more information about the task at hand.

Re: Ask HN: Event loop vs. Threads

#4
Hi, I work at a company that makes heavy use of event loops. I'll try to accurately convey what I know:

From an efficiency standpoint, using event loops requires much less memory, but marginally more CPU time than threaded approaches.

In terms of maintenance, you're essentially writing your programs in continuation passing style which means error handling is explicit everywhere. There are tools that allow you to hide this like streamlinejs, Haskell's continuation monad transformer and to a lesser degree, promises/futures/deferrables. If you decide to stick with callback passing, then a good knowledge of functional programming is useful as all loops require recursion. If your application is single threaded, that tends to make finding race conditions easier, but on the whole I'd say it's harder to write good asynchronous code than it is to write good synchronous code.

Re: Ask HN: Event loop vs. Threads

#5
Most developers with no background in electroncis do not understand asynchronuous paradigm : Transitions = factorial(state). In best case if they don't confuse states and transitions (wich is common) you'll end up with a spaghetti code where goto are replaced with callbacks on events. In common case they will make intricated state models without making the docs (state transition diagrams are a MUST have (like RFCs on network protocol)). In the multi-threading context, most devs don’t fully grasp the concurrency problem (which is still an asynchronous problem). So if you want to stay safe, use multithreading with disjoint data. Map Reduce is actually a pretty idiot proof paradigm for multi-threading. It only requires your data to be smartly shardable.

Executive note : - if event model : have all state transition models DOCUMENTED ; - if multi-threading : once you have shared a context (config), uncouple all the data passed to your thread (and handle a SIGHUP to reload the conf safely).

Event model done wrong will cry havoc on your code maintability the same as multitreading done wrong.

Re: Ask HN: Event loop vs. Threads

#6
I think a lot has been written on why threads, locks etc. are hard to program (= i.e. hard to maintain) and are considered the "assembly of parallel programming".

As for efficiency: Threads and Processes are what the OS offers. Any parallel programming model will have to use these in one way or another, so does Node.js. The discussion is kind of similar to assembly vs. high level programming languages. You can always write an assembly program that is as fast as the program compiled from a high level language. However, it will take you a lot more time to write it.

In the end, it comes down to choosing the right tool for the job. Therefore you should try find out what people with a similar job choose and what their experiences are; and also for which job a framework has been created.

E.g. Node.js is good at a job where there are lots of events and I/O is involved. If your job is number crunching, then Node.js is the wrong tool.

Re: Ask HN: Event loop vs. Threads

#8
Current implementation of node.js does not ensure in any way a parallelism mechanism like the system threads. Please do note that node.js event loop works within a context of a single event que.

What does that result in?

- If you have lots of code that is blocking on IO operations (like file/socket) then you will see some improvements in performance. - If your code utilizes your cpu, then node.js will be slower then your current thread implementation.

Re: Ask HN: Event loop vs. Threads

#10

I think a lot has been written on why threads, locks etc. are hard to program (= i.e. hard to maintain) and are considered the "assembly of parallel programming". As for efficiency: Threads and Processes are what the OS offers. Any parallel programming model will have to use these in one way or another, so does Node.js. The discussion is kind of similar to assembly vs. high level programming languages. You can always…

+1, if your job is number crunching, stay the hell away from JavaScript as it will bite you in the ass. http://stackoverflow.com/questions/307179/what-is-javascript...
Post reply on HN