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.
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.
- The problem. A small story with a visible bug.
- Break it. A simulator you control: sliders, seeds and fault buttons.
- The idea. An animated explanation of the fix.
- In Go. The real standard library and runtime source, then a small program with tests.
- In the wild. How open-source projects solve the same problem, and what their own documents say about why.
- Other languages. The same problem in C++, Java, Rust and Python.
- Pitfalls. The classic mistakes, reproduced.
- 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
Part 1 Synchronization primitives
Locks, semaphores, signals and atomic operations: the building blocks. 1 of 15 published
- 01MutexMake one goroutine at a time enter a critical section, and see how Go's mutex stays fair.Published
- 02RWMutexLet many readers in together while a writer gets the data alone.Planned
- 03Reentrant LockWhy a lock that you already hold can block you, and why Go has no reentrant mutex.Planned
- 04SemaphoreCount permits so that only a set number of goroutines proceed.Planned
- 05Bounded SemaphoreCap concurrent work at N with a semaphore, and what breaks the cap.Planned
- 06Condition VariableSleep until a condition becomes true, without burning CPU.Planned
- 07EventBroadcast that something happened to everyone waiting for it.Planned
- 08BarrierMake a group of goroutines wait for each other at a meeting point.Planned
- 09WaitGroupWait for a known set of goroutines to finish.Planned
- 10Countdown LatchA one-way gate that opens when a count reaches zero.Planned
- 11OnceRun an initialisation exactly once, however many callers arrive.Planned
- 12Atomic OperationsChange a shared value in one indivisible step without a lock.Planned
- 13Compare-and-Swap (CAS)Update a value only if nobody changed it since you looked.Planned
- 14SpinlockA lock that waits by looping, and when that is a good idea.Planned
- 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
- 16Unbuffered ChannelHand a value directly from one goroutine to another.Planned
- 17Buffered ChannelLet senders run ahead of receivers by a fixed amount.Planned
- 18Channel SelectWait on several channels at once and take the first that is ready.Planned
- 19Blocking QueueA queue that makes producers wait when full and consumers wait when empty.Planned
- 20Priority QueueServe the most important item first, and avoid starving the rest.Planned
- 21Work QueueShare a backlog of jobs among workers, including work stealing.Planned
- 22FutureA value that will exist later, and who waits for it.Planned
- 23PromiseThe writing end of a future: who completes it, and how.Planned
- 24Thread PoolReuse a fixed set of threads for many tasks.Planned
- 25Worker PoolRun jobs on a fixed crew of workers and shut down cleanly.Planned
- 26Task GroupStart a group of tasks and stop all of them when one fails.Planned
- 27Cancellation TokenAsk running work to stop, and make it listen.Planned
- 28Context / DeadlineCarry cancellation and deadlines through every call.Planned
- 29Fan-out / Fan-inSpread work across workers and gather the results.Planned
- 30PipelineChain stages so each one works while the others do.Planned
- 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
- 32Memory Barriers / FencesWhy the CPU reorders memory operations, and how fences stop it.Planned
- 33Happens-BeforeThe rule that says when one goroutine is guaranteed to see another's write.Planned
- 34Acquire/Release SemanticsOne-way ordering guarantees that are cheaper than full fences.Planned
- 35Sequential ConsistencyThe strongest ordering: everyone agrees on one timeline.Planned
- 36Lock-Free AlgorithmsAlgorithms where some thread always makes progress without locks.Planned
- 37Wait-Free AlgorithmsAlgorithms where every thread finishes in a bounded number of steps.Planned
- 38ABA ProblemWhen a value changes from A to B and back, and a compare-and-swap is fooled.Planned
- 39False SharingIndependent variables that slow each other down by sharing a cache line.Planned
- 40Read-Copy-Update (RCU)Let readers run without locks while a writer publishes a new copy.Planned
- 41Hazard PointersAnnounce which memory you are reading so nobody frees it.Planned
- 42SeqlocksVersion-checked reads for data that is read far more than written.Planned
- 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
- 44Distributed MutexOne owner for a resource across many machines.Planned
- 45LeaseOwnership that expires unless it is renewed.Planned
- 46Fencing TokenReject work from an owner who has already been replaced.Planned
- 47Leader ElectionChoose one active coordinator among many replicas.Planned
- 48Optimistic ConcurrencyDetect conflicting updates with a version number, as Kubernetes does.Planned
- 49Compare-and-Swap (database)A conditional update in a database.Planned
- 50Advisory LockApplication-level locks held by the database.Planned
- 51Row-Level LockLock the rows you are about to change.Planned
- 52SKIP LOCKEDLet many workers claim different jobs from the same table.Planned
- 53Idempotency KeyMake repeated requests safe by naming each logical operation.Planned
- 54Distributed SemaphoreLimit concurrent work across many replicas.Planned
- 55Version / EpochNumbers that let you tell a current owner from an old one.Planned
- 56Transactional OutboxRecord a change and the work it triggers in one transaction.Planned