Skip to content

Concurrency vs Parallelism: A Coffee Shop Guide

I used to use the words "concurrent" and "parallel" interchangeably. I thought they both just meant "doing more than one thing at the same time."

But in computer science, they have very specific meanings. As Rob Pike (one of the creators of Go) says: "Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once."

Let's look at how that works at my favorite coffee shop.

The Coffee Shop Analogy

Concurrency (Dealing with many things)

Imagine a coffee shop with one barista. 1. The barista takes an order from Alice. 2. While the milk is steaming, the barista starts taking an order from Bob. 3. Then they pour Alice's latte. 4. Then they finish Bob's order.

The barista is concurrent. They are dealing with two orders at once, but they are only ever doing one physical task at any single moment. They are switching back and forth.

Parallelism (Doing many things)

Now imagine the shop hires a second barista. 1. Barista A makes Alice's latte. 2. Barista B makes Bob's espresso.

The work is now happening in parallel. Two different people are physically performing two different tasks at the exact same time.

graph TD
    subgraph Concurrency
    T1[Task A] --> T2[Task B]
    T2 --> T3[Task A]
    T3 --> T4[Task B]
    end
    subgraph Parallelism
    P1[Barista 1: Task A]
    P2[Barista 2: Task B]
    end

Wait, but why does it matter?

Your computer works the same way!

  • A single-core CPU can only do things concurrently. It switches between your browser and your music player so fast that it feels like they are running at the same time, but it's just really fast context switching.
  • A multi-core CPU can do things in parallel. Core 1 runs your browser, and Core 2 runs your music player simultaneously.

Common gotchas

  • I always forget that you can have concurrency without parallelism. You can write a concurrent program that runs on a single core!
  • Watch out for "Race Conditions": This happens in parallel systems when two baristas both try to grab the last carton of oat milk at the exact same time and spill it.

Try it yourself

Think about your own day: - When you are cooking dinner and checking your phone while the pasta boils, are you being concurrent or parallel? (Hint: Unless you have four arms, you're concurrent!)


Further reading

— Nadeem ☕

Comments