Slide 1

Slide 1 text

Thread-Safety in Go An Overlooked Concern P. Mihaylov Software Engineer, Tech. Trainer, Speaker [email protected] Uber Ungineering Sofia, 2021

Slide 2

Slide 2 text

👋 🚗 Software Engineer @ Uber 🎤 Tech. Trainer & Speaker ✍ Blogging at pmihaylov.com 󰞵 Open-sourcing at github.com/preslavmihaylov

Slide 3

Slide 3 text

Why not a talk on concurrency?

Slide 4

Slide 4 text

Most courses/books are incomplete…

Slide 5

Slide 5 text

Not enough ❤ for thread-safety

Slide 6

Slide 6 text

Concurrency without thread-safety -> 200 mp/h 🏎 without safety measures

Slide 7

Slide 7 text

Goals → 1) Learn the fundamentals 2) Get guidance on next steps

Slide 8

Slide 8 text

What’s covered 1. What does “thread-safety” mean? 2. Should you care about it at all? 3. Thread-safety problems & solutions a. When is there a potential thread-safety issue? b. Atomic access to shared state c. Delegating thread-safety d. Atomic compound actions e. Visibility & stale data 4. Where to go from here?

Slide 9

Slide 9 text

What does “thread-safety” mean?

Slide 10

Slide 10 text

Safe classes → behave correctly

Slide 11

Slide 11 text

Thread-safe classes → behave correctly while bombarded by a gazillion goroutines

Slide 12

Slide 12 text

Should you care about it at all?

Slide 13

Slide 13 text

“I don’t need thread-safe classes as I’m typically writing web apps without spawning any threads…” the average web dev

Slide 14

Slide 14 text

You’re already writing multi-threaded applications...

Slide 15

Slide 15 text

...and no. Channels can’t save you every time

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Thread-safety problems & solutions

Slide 18

Slide 18 text

When is there a potential thread-safety issue?

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Is it thread-safe? Join at slido.com #thread-safety-uber

Slide 21

Slide 21 text

Shared mutable state

Slide 22

Slide 22 text

Non-shared state Stack #1 Thread #1 Non-shared state #1 Non-shared state #2 Shared memory aka heap Shared state Non-shared state Stack #2 Thread #2

Slide 23

Slide 23 text

Atomic access to shared state

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

What can go wrong?

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Using Mutexes is Go’s standard way to add critical sections/locks

Slide 29

Slide 29 text

Delegating thread-safety

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Delegated thread-safety → relying on the thread-safetiness of objects you use

Slide 33

Slide 33 text

But also (third-party) packages...

Slide 34

Slide 34 text

Atomic compound actions

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Actions from the same invariant need to be synchronized together

Slide 38

Slide 38 text

Visibility & stale data

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Is it thread-safe? Join at slido.com #thread-safety-uber

Slide 42

Slide 42 text

Thread-safety is not only about concurrent writes...

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Remember 3+1 things

Slide 45

Slide 45 text

1

Slide 46

Slide 46 text

Stateless methods → thread-safe methods

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

2

Slide 49

Slide 49 text

Thread-safety is about both reads and writes...

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

3

Slide 52

Slide 52 text

Prefer delegation over explicit synchronization

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

4

Slide 55

Slide 55 text

Be careful with compound actions...

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

Is that all I need to know?

Slide 59

Slide 59 text

What’s not covered 1. Initialisation safety & Immutable objects 2. Thread-confinement 3. Concurrency tooling - atomic variables/collections, synchronizers 4. Goroutine Management 5. Channels 6. Managing goroutine lifecycle via cancellation/interrupts 7. Dealing with performance/liveness issues (i.e. deadlock, livelock, etc.) 8. The Go Memory Model

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Get the mats → http://bit.ly/thread-safety-uber

Slide 62

Slide 62 text

Thread-Safety in Go An Overlooked Concern P. Mihaylov Software Engineer, Tech. Trainer, Speaker [email protected] Uber Ungineering Sofia, 2021