An interactive course

Concurrency in Go

An interactive course on how concurrent programs work, how Go does it, and how real projects and other languages do it.

Start with Chapter 0See all 57 chapters

How a chapter works

Every chapter is long and is cut into short sections you can read one at a time. Each section builds on the one before.

  1. The problem. A small story with a visible bug.
  2. Break it. A simulator you control: sliders, seeds and fault buttons.
  3. The idea. An animated explanation of the fix.
  4. In Go. The real standard library and runtime source, then a small program with tests.
  5. In the wild. How open-source projects solve the same problem, and what their own documents say about why.
  6. Other languages. The same problem in C++, Java, Rust and Python.
  7. Pitfalls. The classic mistakes, reproduced.
  8. Check. Questions and an exercise.

Every claim about another project or language links to the page it came from. When the reason behind a design is not written down in a source we could open, the chapter says so instead of guessing.

Part 0 Start

Why concurrency is hard, and the vocabulary for the rest of the course. 1 of 1 published

  1. 00Why concurrency is hardWhat goes wrong when two things touch the same data, and the vocabulary for talking about it.Published

Part 1 Synchronization primitives

Locks, semaphores, signals and atomic operations: the building blocks. 1 of 15 published

  1. 01MutexMake one goroutine at a time enter a critical section, and see how Go's mutex stays fair.Published
  2. 02RWMutexLet many readers in together while a writer gets the data alone.Planned
  3. 03Reentrant LockWhy a lock that you already hold can block you, and why Go has no reentrant mutex.Planned
  4. 04SemaphoreCount permits so that only a set number of goroutines proceed.Planned
  5. 05Bounded SemaphoreCap concurrent work at N with a semaphore, and what breaks the cap.Planned
  6. 06Condition VariableSleep until a condition becomes true, without burning CPU.Planned
  7. 07EventBroadcast that something happened to everyone waiting for it.Planned
  8. 08BarrierMake a group of goroutines wait for each other at a meeting point.Planned
  9. 09WaitGroupWait for a known set of goroutines to finish.Planned
  10. 10Countdown LatchA one-way gate that opens when a count reaches zero.Planned
  11. 11OnceRun an initialisation exactly once, however many callers arrive.Planned
  12. 12Atomic OperationsChange a shared value in one indivisible step without a lock.Planned
  13. 13Compare-and-Swap (CAS)Update a value only if nobody changed it since you looked.Planned
  14. 14SpinlockA lock that waits by looping, and when that is a good idea.Planned
  15. 15Ticket LockA fair lock that serves callers in the order they arrived.Planned

Part 2 Concurrency patterns and communication

Channels, queues, pools, cancellation and pipelines. 0 of 16 published

  1. 16Unbuffered ChannelHand a value directly from one goroutine to another.Planned
  2. 17Buffered ChannelLet senders run ahead of receivers by a fixed amount.Planned
  3. 18Channel SelectWait on several channels at once and take the first that is ready.Planned
  4. 19Blocking QueueA queue that makes producers wait when full and consumers wait when empty.Planned
  5. 20Priority QueueServe the most important item first, and avoid starving the rest.Planned
  6. 21Work QueueShare a backlog of jobs among workers, including work stealing.Planned
  7. 22FutureA value that will exist later, and who waits for it.Planned
  8. 23PromiseThe writing end of a future: who completes it, and how.Planned
  9. 24Thread PoolReuse a fixed set of threads for many tasks.Planned
  10. 25Worker PoolRun jobs on a fixed crew of workers and shut down cleanly.Planned
  11. 26Task GroupStart a group of tasks and stop all of them when one fails.Planned
  12. 27Cancellation TokenAsk running work to stop, and make it listen.Planned
  13. 28Context / DeadlineCarry cancellation and deadlines through every call.Planned
  14. 29Fan-out / Fan-inSpread work across workers and gather the results.Planned
  15. 30PipelineChain stages so each one works while the others do.Planned
  16. 31SingleflightCollapse many identical requests into one call.Planned

Part 3 Advanced concurrency and memory models

What the hardware and the language promise about who sees what, and when. 0 of 12 published

  1. 32Memory Barriers / FencesWhy the CPU reorders memory operations, and how fences stop it.Planned
  2. 33Happens-BeforeThe rule that says when one goroutine is guaranteed to see another's write.Planned
  3. 34Acquire/Release SemanticsOne-way ordering guarantees that are cheaper than full fences.Planned
  4. 35Sequential ConsistencyThe strongest ordering: everyone agrees on one timeline.Planned
  5. 36Lock-Free AlgorithmsAlgorithms where some thread always makes progress without locks.Planned
  6. 37Wait-Free AlgorithmsAlgorithms where every thread finishes in a bounded number of steps.Planned
  7. 38ABA ProblemWhen a value changes from A to B and back, and a compare-and-swap is fooled.Planned
  8. 39False SharingIndependent variables that slow each other down by sharing a cache line.Planned
  9. 40Read-Copy-Update (RCU)Let readers run without locks while a writer publishes a new copy.Planned
  10. 41Hazard PointersAnnounce which memory you are reading so nobody frees it.Planned
  11. 42SeqlocksVersion-checked reads for data that is read far more than written.Planned
  12. 43Optimistic vs. Pessimistic LockingDetect conflicts after the fact, or prevent them up front.Planned

Part 4 Distributed concurrency and coordination

Leases, fencing tokens, database locks and idempotency across machines. 0 of 13 published

  1. 44Distributed MutexOne owner for a resource across many machines.Planned
  2. 45LeaseOwnership that expires unless it is renewed.Planned
  3. 46Fencing TokenReject work from an owner who has already been replaced.Planned
  4. 47Leader ElectionChoose one active coordinator among many replicas.Planned
  5. 48Optimistic ConcurrencyDetect conflicting updates with a version number, as Kubernetes does.Planned
  6. 49Compare-and-Swap (database)A conditional update in a database.Planned
  7. 50Advisory LockApplication-level locks held by the database.Planned
  8. 51Row-Level LockLock the rows you are about to change.Planned
  9. 52SKIP LOCKEDLet many workers claim different jobs from the same table.Planned
  10. 53Idempotency KeyMake repeated requests safe by naming each logical operation.Planned
  11. 54Distributed SemaphoreLimit concurrent work across many replicas.Planned
  12. 55Version / EpochNumbers that let you tell a current owner from an old one.Planned
  13. 56Transactional OutboxRecord a change and the work it triggers in one transaction.Planned