Concurrency

in Go

Agenda

General Concepts

  • Threads vs. Green Threads
  • Concurrency & Parallelism

 

Goroutines

  • Implementation, Channels
  • Concurrency Patterns

Threads

Smallest sequence of programmed instructions that can run independently

Managed by OS

  • Userspace
  • Kernel Space

Threads

Smallest sequence of programmed instructions that can run independently

Managed by OS

  • Userspace
  • Kernel Space

 

Context Switch by swapping out data at CPU level

  • Expensive Process

Threads

C10k is the problem of concurrently handling ten thousand connections.

 

Handling many concurrent connections is a different problem to handling many requests per second.

 

The latter requires high throughput
(processing them quickly)

 

While C10K does not have to be fast,
but requires efficient scheduling of connections.

Green Threads or Coroutines

Smallest sequence of programmed instructions that can run independently
Smallest sequence of programmed instructions that can run independently

Managed by

  • Program Runtime
  • Virtual Machine

 

Context Switch by swapping out data at runtime level

  • Lightweight process

Green Threads or Coroutines

Threads

  • Managed by OS
  • Hardware level
  • Expensive Process
  • Example: Apache Server

Coroutines

  • Managed by Runtime
  • In Program
  • Lightweight Process
  • Example: Nginx

Threads

  • Managed by OS
  • Hardware level
  • Expensive Process
  • Example: Apache Server

Coroutines

  • Managed by Runtime
  • In Program
  • Lightweight Process
  • Example: Nginx
  • Java
  • C/Cpp
  • Node/Javascript
  • Python
  • Ruby

Threads

  • Managed by OS
  • Hardware level
  • Expensive Process
  • Example: Apache Server

Coroutines

  • Managed by Runtime
  • In Program
  • Lightweight Process
  • Example: Nginx
  • Java
  • C/Cpp
  • Node/Javascript
  • Python
  • Ruby

I/O Bound

vs.

CPU Bound

CPU Bound

Processor is the only component being used for execution

I/O Bound

Execution is dependent on the input-output system and its resources, such as disk drives and peripheral devices

Boundaries define if a program is sequential or concurrent/parallel

Tasks

  • Email
  • Call
  • Dinner

Tasks

  • Email
    • Write Email
    • Review/Send Email
  • Call
    • Person 1
    • Person 2
  • Dinner
    • Prepare Food
    • Eat Food

Concurrency vs. Parallelism

Call 1

Write Email

Prep Food

Call 2

Send Email

Eat Food

You

Concurrency

Concurrency vs. Parallelism

Call 1

Write Email

Eat Food

You

Call 2

Prep Food

Eat Food

Friend

Call 3

Send Email

Parallelism

Single Threaded Concurrency

  • Only one task at a time
  • Runs on a single core
  • Uses an Event Loop

Call 1

Email 1/2

Prep Food

Call 2

Email 2/2

Eat Food

Event Loop

Pop Quiz

Parallelism

Parallelism + Event Loops = Goroutines

  • Tasks executed parallelly == number of CPU cores
  • Uses modified/extended event loop

Call 1

Email 1/2

Prep Food

Call 2

Email 2/2

You: Core #1

Friend: Core #2

Global

Run

queue

Goroutines

  • # of Event Loops = # of processors
  • Boundaries using `go` keyword
  • Boundaries on I/O calls

Pop Quiz

Goroutines (Rabbit Hole)

  • Go runtime/scheduler will switch active goroutine every 10ms
  • N:M threading model
  • Work Stealing Scheduler
  • Hoare's Communicating Sequential Processes (CSP)

Concurrency Patterns

Do not communicate by sharing memory; instead, share memory by communicating.

Communicate via Channels

See Sample 7

Channels for concurrency

  • Unbuffered Channels to Synchronize
  • Buffered Channels to Parallelize

 

Fan-Out, Fan-In

See Sample 9

Concurrency & Parallelism in Go

By last-ent

Concurrency & Parallelism in Go

  • 240