February 17, 2025

Concurrency and parallelism

I wanted to explore the differences between concurrency and parallelism. I know Concurrency ≠ parallelism but I wanted to refresh my memory and visualize each term. They refer to executing multiple tasks, but they represent different approaches.

Tasks communicate with each other by using a shared memory or by message passing. It's about switching between tasks really quicky. Concurrent tasks (or modules) come in two different kinds: processes and threads.

💡Example of concurrency

There are three main ways to achieve concurrency:

Distributed

Multiple independent processes with no shared memory, communicating only via message passing.

Multi-Threaded

Multiple threads share memory, so require locks. Generally, a single process can have multiple threads.

Event Queue

Only a single thread exists! A single loop reads from the event queue and invokes the handlers.

Unlike concurrency, parallelism is about separating tasks into smaller parts to be performed independently and simultaneously using more than one processor. It's when tasks actually run at the same time, without just giving the illusion of it. The two most common ways to achieve parallelism:

Data Parallelism

Split data into chunks and process them simultaneously (e.g., rendering frames in a video).

Task Parallelism

Execute different tasks at the same time (e.g., downloading files while compressing others).

💡Example of parallelism

JavaScript, and anything built on it, like React, is single-threaded. This means that it can only execute one task at a time. However, JavaScript can simulate concurrency by using non-blocking asynchronous operations using an . This is done by using callbacks, promises, and async/await.

Parallelism speeds up tasks by leveraging multiple processors to improve performance. Concurrency keeps systems responsive by managing multiple tasks efficiently. Distributed systems rely on concurrency to handle thousands of requests and scale.

I/O bound tasks like reading files, network requests, and database queries can be parallelized and can benefit from multithreading. They benefit more from concurrency than parallelism, while CPU-bound tasks like image processing, video encoding, and data analysis can be parallelized and benefit more from true parallelism.

Email GitHub LinkedIn Spotify My CV