@Ajit5ingh

Concurrency vs Parallelism

Two different ways to handle multiple tasks

What's the Difference?

Concurrency is about managing multiple tasks at once by switching between them. Parallelism is about actually doing multiple tasks at the same time. They sound similar but work differently and solve different problems.

Think of it like: A chef juggling multiple dishes on one stove is concurrency. Two chefs cooking different dishes on two stoves is parallelism.

The Three Approaches

Sequential (One at a Time)


gantt
    title Tasks Running One After Another
    dateFormat X
    axisFormat %s
    
    Task 1 :0, 3
    Task 2 :3, 6
    Task 3 :6, 9

How it works: Do one task completely, then move to the next. Like reading emails one by one - read, reply, next email.

Concurrent (Switching Between Tasks)


gantt
    title Tasks Interleaved on One Core
    dateFormat X
    axisFormat %s
    
    section Core 1
    Task 1 :0, 1
    Task 2 :1, 2
    Task 3 :2, 3
    Task 1 :3, 4
    Task 2 :4, 5
    Task 3 :5, 6

How it works: Switch between tasks quickly. While one task waits (like loading data), do another task. Feels like multitasking on a single core.

Parallel (Truly Simultaneous)


gantt
    title Tasks Running at Same Time on Multiple Cores
    dateFormat X
    axisFormat %s
    
    section Core 1
    Task 1 :0, 3
    section Core 2
    Task 2 :0, 3
    section Core 3
    Task 3 :0, 3

How it works: Multiple tasks actually run at the exact same time on different CPU cores. Like three people each working on their own project simultaneously.

Key Differences

Concurrency: Dealing with multiple things

About: Managing multiple tasks that don't need to run at the exact same moment.

One CPU core can: Handle 1000s of concurrent tasks by switching between them super fast.

Good for: Tasks with lots of waiting (web requests, file I/O, user input).

Parallelism: Doing multiple things

About: Actually executing multiple tasks at the exact same time.

Needs: Multiple CPU cores - each core does real work simultaneously.

Good for: Heavy computation (video encoding, data processing, scientific calculations).

Code Examples

Here's how they look in practice:

Concurrency (JavaScript/Node.js)

// Handle multiple requests without blocking
async function processRequests() {
  // All three start at once, switch while waiting
  const result1 = fetch('/api/users');
  const result2 = fetch('/api/posts');
  const result3 = fetch('/api/comments');
  
  // Wait for all to finish
  const [users, posts, comments] = await Promise.all([result1, result2, result3]);
}

One thread manages three tasks. While waiting for network, it does other work.

Parallelism (Python)

from multiprocessing import Pool

# Heavy computation - calculate sum of squares
def compute_squares(numbers):
    return sum(x ** 2 for x in numbers)

# Split work into 4 chunks
chunks = [range(0, 25), range(25, 50),
          range(50, 75), range(75, 100)]

# Each chunk runs on a different CPU core
with Pool(4) as pool:
    results = pool.map(compute_squares, chunks)
    total = sum(results)  # Combine results

Four CPU cores calculate different chunks simultaneously, then combine results. Much faster than doing it sequentially.

When to Use Each

Use Concurrency When

  • Handling many I/O operations (network, disk)
  • Building web servers with many connections
  • Tasks spend time waiting for responses
  • Managing user interactions in UI
  • Running scheduled background jobs
  • You have one or few CPU cores

Use Parallelism When

  • Heavy CPU-bound calculations
  • Processing large datasets
  • Video or image processing
  • Scientific simulations
  • Machine learning training
  • You have multiple CPU cores available

💡 Can you use both? Yes! Many apps use concurrency to handle I/O and parallelism for heavy computation. A web server might handle 1000 concurrent connections while using parallel workers for image processing.

Common mistake: Don't use parallelism for I/O tasks - you'll waste resources. Concurrency is much more efficient when tasks are just waiting around.

← Back to All Explainers