08 / 10

What is a goroutine, and how does it differ from an OS thread?

Difficulty: 5/10
goroutine lifecycle, runtime scheduler, OS thread comparison

Goroutines are lightweight user-space threads managed by the Go runtime, starting at ~2KB of stack and multiplexed onto a small pool of OS threads through the M:N scheduler.

Goroutines vs OS threads
  1. 1

    OS thread stack: 1-8MB fixed. Goroutine stack: starts at 2KB, grows and shrinks dynamically up to 1GB

  2. 2

    Context switch: OS thread switch requires kernel mode transition (~1μs). Goroutine switch is in user space (~100ns)

  3. 3

    Creation cost: spawning an OS thread takes ~10-100μs. Launching a goroutine takes ~1μs

  4. 4

    Scheduling: OS scheduler is preemptive and unaware of Go semantics. Go scheduler is cooperative with preemption at safe points

  5. 5

    Scale: practical limit for OS threads is ~10,000. Go programs routinely run 100,000+ goroutines

Goroutine basics

Scenario Questions

0-2 years experience

  1. 1If you need to fetch data from three independent HTTP endpoints concurrently in a small Go program, how would you use goroutines to do that, and what would you add to ensure the main function waits for all responses?
  2. 2What would happen if you called a blocking function directly in a request handler without a goroutine, compared to spawning a goroutine for it?

2-5 years experience

  1. 1You notice a Go service sometimes hangs under load and suspect a goroutine leak. How would you investigate, confirm the leak, and which runtime tools would you use?
  2. 2A recent refactor replaced a pool of OS threads with goroutines and latency increased. What could cause this slowdown, and how would you tune the runtime to address it?

5-8 years experience

  1. 1Design a worker pool that processes jobs from a channel but limits the number of concurrent OS threads. Explain how you would balance the number of goroutines versus GOMAXPROCS and why.
  2. 2In a high‑throughput microservice handling thousands of connections, discuss the trade‑offs between spawning a goroutine per connection versus using a fixed set of OS threads with event‑driven I/O.

8+ years experience

  1. 1Your organization is migrating a legacy C++ service that uses a thread‑per‑request model to Go. What architectural changes would you make to leverage goroutine scheduling, and how would you ensure predictable performance and resource usage across teams?
  2. 2When defining a company‑wide concurrency policy, how would you decide when to expose goroutine‑based APIs versus higher‑level abstractions, considering maintainability, debugging, and cross‑service contracts?

Follow-up Questions

  • What happens if you start millions of goroutines in a process?
  • How does the Go scheduler decide when to move a goroutine onto a different OS thread?
  • Can you control the number of OS threads the runtime uses, and how?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.