September 9th, 2024

Understanding Concurrency, Parallelism and JavaScript

The article explains concurrency and parallelism in JavaScript, highlighting Node.js's use of worker threads for efficient task management and the importance of understanding race conditions and performance implications.

Read original articleLink Icon
Understanding Concurrency, Parallelism and JavaScript

The article discusses the distinctions between concurrency and parallelism, particularly in the context of JavaScript programming. Concurrency involves managing multiple tasks by interleaving their execution, while parallelism refers to executing multiple tasks simultaneously. The author uses everyday examples to illustrate these concepts, explaining that sequential execution occurs when tasks are completed one after another without overlap. In programming, concurrency can be achieved through kernel threads, which allow for multitasking, while parallelism depends on the hardware capabilities of the CPU. The article highlights Node.js as an example of user-space concurrency, where blocking tasks are delegated to worker threads, allowing the main thread to remain responsive. The author also provides code examples to demonstrate how asynchronous operations can lead to race conditions and emphasizes the importance of understanding these concepts to avoid potential pitfalls in concurrent programming. The conclusion stresses that there are various methods to achieve concurrency, each with its implications for performance and potential issues.

- Concurrency and parallelism are distinct concepts; concurrency involves interleaving tasks, while parallelism executes them simultaneously.

- Node.js manages blocking tasks using worker threads, allowing for efficient concurrency in JavaScript applications.

- Race conditions can occur in asynchronous programming, necessitating careful management of shared data.

- Understanding the underlying mechanisms of concurrency can help developers avoid common pitfalls in their code.

- The choice of concurrency method can significantly impact program performance and complexity.

Link Icon 8 comments
By @necovek - 6 months
The one missed distinction is that concurrent tasks can be executing in parallel, it just doesn't imply they are or aren't.

Basically, all parallel tasks are also concurrent, but there are concurrent tasks which are not executed in parallel.

By @duped - 5 months
If you prefer to learn by video, here's an excellent talk on the same subject by Rob Pike that I link all the time to people

https://www.youtube.com/watch?v=oV9rvDllKEg

By @rdtsc - 5 months
I like to think of them as different levels. Concurrency is at a higher abstraction level: steps that can execute without needing to wait on each other. Parallelism is a bit lower and reflects the ability to actually execute the steps at the same time.

Sometimes you can have concurrent units like multiple threads, but a single CPU, so they won’t execute in parallel. In an environment with multiple CPU they might execute in parallel.

By @CalRobert - 5 months
I just learned something! I realize now I was talking about parallelism in a recent interview question about concurrency. Oh well.
By @dragontamer - 5 months
Concurrency often is about running your I/O routines in parallel, achieving higher bandwidth. For example, one computer handling 50 concurrent HTTP requests simultaneously.

No single HTTP request uses all the CPU power or even your Ethernet bandwidth. The bulk of your waiting is latency issues. So while one task is waiting on Ethernet responses under the hood, the system should do something else.

Hard Drives are another: you can have random I/O bandwidths of 5MB/s or so, but every request always takes 4ms on the average for a 7200 RPM drive (aka: 120 rotations per second, or about 8 miliseconds for a complete rotation. So 4ms on the average for any request to complete).

So while waiting for the HDD to respond, your OS can schedule other reads or writes for the head to move to which improves average performance (ex: if 8 requests all are within the path of the head, you'll still wait 4ms on the average, but maybe each will be read per 1ms).

----------

Parallelism is often about CPU limited situations where you use a 2nd CPU (today called a core). For example, if one CPU core is too slow, you can use a 2nd, or 8 or even 128 cores simultaneously.

------------

Hyperthreads is the CPU designer (Intel and AMD) that the above concurrency technique can apply to modern RAM because a single RAM read is like 50ns or 200 clock ticks. Any RAM-latency problem (ex: linked list traversals) would benefit from the CPU core doing something else while waiting for the RAM latency to respond.

-----

Different programming languages have different patterns to make these situations easier to program.

By @bryanrasmussen - 5 months
thinking about this - is there a term for tasks which are partially parallel, that is to say X starts at 0.1 and ends at 1 and Y starts at 0.2 and ends at 0.9 - X and Y are not parallel, but they are something that I'm not sure what the technical term for is. (this is assuming they are not executed concurrently either of course)
By @bugtodiffer - 5 months
Learn Go and you will understand concurrency