System Design Academy

Saving…

Lesson 1 of 10 in Module 40 of 45 lessons complete

Processes, Threads & Goroutines

Module 4, lesson 1Concept12 min

Definition

Concurrency

In plain English
Concurrency means structuring a program so several tasks can make progress during the same period.
Think of it like
It is like a chef juggling soup, toast and eggs by switching between them.

Parallelism

In plain English
Parallelism means tasks literally run at the same instant on different processor cores.
Think of it like
It is like three chefs each cooking one dish at the same moment.

Goroutine

In plain English
A goroutine is a lightweight task managed by the Go runtime, started by writing go before a function call.
Think of it like
It is like handing a small job to a kitchen helper who works alongside you.

Why it exists

Five shelter feeds each take 400 milliseconds. Fetching them one after another takes about two seconds.

Overlap those waits and the total can approach 400 milliseconds. Network delays and available resources still affect the result.

Go runtime and stack

In plain English
The Go runtime schedules goroutines, and each goroutine has a stack holding its active function calls.
Think of it like
Like a dispatcher giving each helper a small notebook for the current job.

How it works

Process and thread

In plain English
A process is a running program with its own memory, and a thread is a path of execution inside a process that shares that memory.
Think of it like
Like a restaurant and the staff working inside it.

sync.WaitGroup

In plain English
A WaitGroup lets one goroutine wait until a set of other goroutines has finished.
Think of it like
Like a tour guide counting everyone back onto the coach.
TaskMemoryStarting costManaged by
ProcessSeparate memoryRelatively highOperating system
ThreadShares process memoryUsually higher than a goroutineOperating system
GoroutineShares process memoryStarts with a few kilobytes of stackGo runtime

Main starts three goroutines, which all lead to WaitGroup Wait and then Finish.

Wait for every helper before returning.

Main starts three goroutines, which all lead to WaitGroup Wait and then Finish.

Before (exits too early): main never waits for the helper.
Go
package main

import (
	"fmt"
	"time"
)

func main() {
	go func() { time.Sleep(20 * time.Millisecond); fmt.Println("ready") }()
}
After: Add before starting; Done reports completion; Wait keeps main alive.
Go
package main

import (
	"fmt"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	wg.Add(1)
	go func() { defer wg.Done(); fmt.Println("ready") }()
	wg.Wait()
}

Examples

Fetch five shelter lists

You need pet lists from five shelters. Each request can wait independently.

After: simulated shelter requests write separate slice elements.
Go
package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	pets := make([]string, 5)
	var wg sync.WaitGroup
	for i := range pets {
		wg.Add(1)
		go func(i int) { defer wg.Done(); time.Sleep(20 * time.Millisecond); pets[i] = fmt.Sprint("shelter ", i) }(i)
	}
	wg.Wait()
	fmt.Println(pets)
}

Each helper owns one existing slice element. Main reads the slice only after Wait returns.

The insight

Concurrency turns waiting time into overlap.

Trade-offs

Pros

  • Overlap network and disk waits.
  • Use multiple cores when available.
  • Start lightweight tasks.

Cons

  • Shared data needs coordination.
  • Completion order can vary.

Use when

  • Tasks have independent inputs.
  • Several tasks wait for external work.

Avoid when

  • Tasks finish faster than coordination.
  • Work must happen in a strict sequence.

Quick check

A program starts goroutines and main returns immediately. What happens to them?
Concurrency vs parallelism?

Takeaways

  • Concurrency structures overlapping work; parallelism runs work simultaneously.
  • Use go before a function call to start a goroutine.
  • Use a WaitGroup to wait for helpers.

Try the quick check above whenever you feel ready.

Previous: Choosing a Pattern