What makes Goroutines highly efficient

Arpit Bhayani

Arpit Bhayani

Mar 21, 2024 • 1 min read


What makes Goroutines highly efficient? ⚡

Goroutines are not OS-level threads but are threads that are managed by the Go runtime as user-level threads. These user-level threads (goroutines) are then scheduled on OS-level threads and then executed.

OS-level threads have a stack size of 2MB while Goroutine starts with a stack size of 2KB, this means it is very lightweight for Go runtime to create multiple goroutines efficiently allowing a large number of goroutines to exist concurrently.

Apart from minimal stack size, Go’s runtime

  1. is asynchronous in nature and does not block on I/O.
  2. schedules goroutines such that it maximizes the business of CPU cores.
  3. passes messages to coordinate goroutines, instead of using shared memory; this reduces lock contention and improves throughput.

⚡ I keep writing and sharing these engineering nuggets, so if you are keen on learning them, follow along.

youtube.com/c/ArpitBhayani

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses