Concurrency Fundamentals

Concurrency versus parallelism, processes versus threads versus async, and the bugs shared state causes: a lost update and a deadlock reproduced on purpose, then fixed with a lock and a lock order.

Overview

Concurrency means a program has several tasks in progress during the same stretch of time. It does not mean they run at the same instant; that is parallelism. Almost every backend is concurrent: a server handles many requests at once, a worker runs background jobs, a cache is read and written from several threads.

Interviewers ask about it because the bugs are subtle. Code that looks right on one thread can lose updates, hang forever, or behave differently on every run once two threads share data. A strong candidate names the failure, shows the interleaving that causes it, and fixes it with the smallest tool that works.

Concurrency is not parallelism

Rob Pike's short version: concurrency is dealing with many things at once, parallelism is doing many things at once. Concurrency is how you structure the program; parallelism is how it happens to run on the hardware.

Picture two tasks, A and B, over four time slots. On one core, which is concurrent but not parallel, the slots run task A, task B, task A, task B. On two cores, which is concurrent and parallel, core 1 runs task A and core 2 runs task B in slots 1 and 2. In slot 3, task A finishes on core 1 while core 2 keeps running task B, and in slot 4 task B finishes on core 2.

On one core the tasks take turns, and the switching makes them look simultaneous. On two cores they really overlap. The same program can run either way: in Go, the GOMAXPROCS setting limits how many OS threads run Go code at the same time; in CPython, the global interpreter lock (GIL) lets only one thread run Python bytecode at a time, so threads give concurrency but not CPU parallelism (the optional free-threaded build of Python 3.13 and later removes that limit).

Interview questions

  • What is the difference between concurrency and parallelism?
  • What are the four conditions for deadlock, and how do you prevent it?
  • When should you use processes, threads or async?
  • Show a race condition and fix it.
  • What is the difference between deadlock, livelock and starvation?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio